SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am creating a custom webpart for SP 2010. I need to use the SPDataSource control with a dropdownlist. I got this to work perfectly if the SP list is hardcoded in the node, but what if I dont know which list needs to be used and this information is passed by the user via the "Edit Web Part" of the custom web part?

Here's what works:

<SharePoint:SPDataSource runat="server" ID="FolderNameDataSource"  
DataSourceMode="List"     SelectCommand="<Query><OrderBy><FieldRef Name='Title'      
Ascending='true' /></OrderBy></Query>">
 <asp:Parameter Name="ListName" DefaultValue="Forms Library" /> 

<asp:DropDownList runat="server" ID="ddlFolderNames" CssClass="title"  
DataSourceID="FolderNameDataSource" DataTextField="Title" DataValueField="Title" 
AutoPostBack ="true">

DefaultValue is where the SP list is passed. How can I do all of this programmatically? I capture the user entered listname like this:

_listname = this.WebPart.FolderNameValue;

Now how can I pass this to the parameter?

Thanks, Ninel

share|improve this question
  • Remove the declarative binding, use the code behind
  • Create a WebPart property for List Name
  • Validate the ListName entered (Type of list , existence ..etc)
  • If valid, bind the DropDown on runtime and make it visible.
share|improve this answer
Hi Vamsi, Thanks for the response, but I was already able to create the webpart property. I'm stuck at creating the SPDatasource and dropdownlist on runtime. Thanks,Ninel – ninel Nov 7 '11 at 15:03
Creating SPDatasource and dropdownlist on runtime is possible but needs extra coding for state mgmt. So, I would suggest declare them declarative (I assume you are using ascx file) and then configure them on the runtime. – Vamsi Nov 7 '11 at 16:50
I got it working sort of... I need to use the DataSourceID from SPDataSource in the delcaration of the dropdown. Can you please help me with that? – ninel Nov 7 '11 at 21:40

Your Answer


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.