2

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>">
<SelectParameters> 
 <asp:Parameter Name="ListName" DefaultValue="Forms Library" /> 
</SelectParameters>

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

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

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

Your Answer

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

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