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

1 Answer 1

  • 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.
  • 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
    Commented Nov 7, 2011 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
    Commented Nov 7, 2011 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
    Commented Nov 7, 2011 at 21:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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