I created a webpart in Visual Studio 2010. I have add a button, textbox, label and in my VisualWebPart1UserControl.ascx. When you click on the button the grid will be filled with some data from an external database. I would like to add the connection string and some other fields in the editor zone.

How can I implement this in my VisualWebPart1UserControl.ascx or VisualWebPart1UserControl.ascx.cs ?


There are two (main) ways:

First, leverage the out of the box attributes provided by the framework.

 WebDisplayName("Friendly Display Name"),
 WebDescription("Values: Whatever value you need)]
public string PropertyValue { get; set; }

(This was taken from the link in Arsalan answer). This way you will get a standard editor with no special behaviour attached.

Otherwise, you may develop a custom editor part. Basically, you will create a custom control that SharePoint will display side by side with the standard editor parts. In that control you are free to put whatever logic you may need. That said, you will need to implement the method that saves the provided data to the web part properties. Here you will find a sample from Microsoft.

EDIT: to further explain why you may chose one option over the another.

Marking a property with the framework attribute provides a very fast way of adding a setting. This has a cost: only very basic field types are supported, and the default editor are very simple too. That means that your connection string will be shown as a plain one line text field. Based on the situation, this may or may not be accetable for you.

On the other hand, a custom editor part leaves you with the freedom of developing whatever control you may need. For example, you could actually implement an editor that provides a popup wizard for setting the connection string, with "Test Connection" buttons and such. All for a price... you must implement all the code yourself.

To be honest there is still another approach. You can create some classes that extend the default editor showed when using the attributes. This approach can be considered an hybrid of the two: you still have to implement some custom logic (and usually you end writting more code than the corresponding custom editor part) BUT you will then have a library that you may reuse in the future (while a custom editor part instead tends to be tied to the web part). Again, you should evaluate if the additional effort is worth making based on the possible future reuse.

  • Is there some best solution if I have created a visual webpart?
    – HAKH
    Dec 7 '12 at 13:33
  • If you mean which of the two alternative would be better, the choice should be based on what you need to provide as an editor field for your custom setting. I will edit to provide more info.
    – SPArcheon
    Dec 7 '12 at 13:49

They are called Custom Visual Web Part Properties, have a look at following:

How To Create SharePoint 2010 custom visual webpart properties
How to create custom properties for visual webpart in SharePoint 2010

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.