0

My user control has several components, but the main is a CheckBoxList, I want to load it with a SqlDataSource, from the page, allowing me to use multiple instances of my control with different sources.

My first attempt was to expose the checkboxlist's DataSourceID in my user control:

public String DataSourceID
{
   get { return myCheckList.DataSourceID; }
   set { myCheckList.DataSourceID = value; }
}

And set it from my aspx, just like I would do for any other control:

<asp:SqlDataSource ID="sdsTest" .... ></asp:SqlDataSource>
<uc1:MyControl ID="controlTest" runat="server" DataSourceID="sdsTest" .. />

Didn't work!... So, found in internet (and tried) the following..

public String DataSourceID { get; set; }

protected void Page_Load(object sender, EventArgs e)
{
    if ((this.DataSourceID ?? "") != "")
    {
        SqlDataSource datasource = Utils.FindControl(Page, this.DataSourceID) as SqlDataSource;
        if (datasource != null)
        {
            myCheckList.DataSource = datasource;
            myCheckList.DataBind();
        }
    }
}

Even when the SqlDataSource is found, and the DataBind() is executed, my checkbox list is still not loaded. Am I missing something obvious?

Thanks in advance!

2
  • When you break on myCheckList.DataSource = datasource, what does your datasource look like? Commented Mar 27, 2015 at 4:22
  • it is: System.Web.UI.WebControls.SqlDataSource, I think I found the problem.. totally my fault... Commented Mar 27, 2015 at 16:00

1 Answer 1

2

Well, after a good sleep, I found the problem, totally my fault and I will just post the correct code, just in case somebody needs to do something similar...

So, the problem was, my sqldatasource expected a parameter, and I could swear I declare it as a Session parameter from the beginning, BUT... I didn't, I declare it as a SIMPLE parameter (as I was not giving it a value, the sqldatasource was cancelling the select due to a null variable/parameter.

At the end, yes, just exposing the Checkboxlist's DataSourceID property is good enough!

In UserControl

public string DataSourceID
{
    get { return cbxOptions.DataSourceID; }
    set { cbxOptions.DataSourceID = value; }
}

In aspx

<uc1:MyControl ID="MyControl1" runat="server" DataSourceID="sdsTest" ... />
<asp:SqlDataSource ID="sdsTest" runat="server" 
                ConnectionString="..." SelectCommand="select [field] 
                                                      from [table] (nolock)
                                                      where customerid= @customerid">
   <SelectParameters>
        <asp:SessionParameter DbType="Int32" Name="customerid" SessionField="CustomerID" />
   </SelectParameters>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.