Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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 ?

share|improve this question

2 Answers 2

There are two (main) ways:

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

[WebBrowsable(true),
 Category("Configuration"),
 Personalizable(PersonalizationScope.Shared),
 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.

share|improve this answer
    
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. –  SPArchaeologist Dec 7 '12 at 13:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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