SharePoint - dropdown of custom webpart with toolpart as a source URL

Asked By jc dls on 11-May-12 09:37 AM
Earn up to 10 extra points for answering this tough question.

i have a custom webpart with dropdownlist control. i want the items of the dropdown to come from a custom list..

also i need the url of the custom list to be available on the toolpane view, when we modify webpart. can you pls redirect me to correct URL or guide. thanks

here's my code

 public class DropDownWP : WebPart
      DropDownList ddlCountry;
      private string strMessage = "";
      [Personalizable(PersonalizationScope.Shared), WebBrowsable(true),
      WebDisplayName("Enter source URL List (http://servername/lists/Sample.aspx)"),
      WebDescription("Enter source URL List (http://servername/lists/Sample.aspx)"),
      Category("Custom Category")]
      public string Message
        get { return strMessage; }
        set { strMessage = value; }
      protected override void CreateChildControls()
        SPSite mySite = new SPSite("http://servername/en-US");
        SPList targetList = targetSite.OpenWeb().Lists["List of Country"];
        ddlCountry = new DropDownList();
        foreach (SPListItem myItem in myList.Items)

      protected override void RenderContents(System.Web.UI.HtmlTextWriter writer)
        writer.Write("<br />");
        writer.Write("<br />");


Jitendra Faye replied to jc dls on 14-May-12 01:27 AM
Try like this-

public class DemoCustomToolPart : Microsoft.SharePoint.WebPartPages.ToolPart
    DropDownList ddl;
    TextBox tb;

    protected override void CreateChildControls()
        ddl = new DropDownList();
        ddl.ID = "ddl";

        // Simply getting the lists of the current web, and displaying them in the dropdown-list.
        SPListCollection lists = SPContext.Current.Web.Lists;
        foreach (SPList list in lists)

        tb = new TextBox();
        tb.ID = "tb";


    public override void ApplyChanges()
        DemoWebPart wp = (DemoWebPart)this.ParentToolPane.SelectedWebPart;
        wp.ListName = ddl.SelectedValue;
        wp.ListValue = tb.Text;

refer this link for more details-