简体   繁体   English

使用DataSource的ASP.net用户控件

[英]ASP.net user control with DataSource

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. 我的用户控件有多个组件,但是主要是CheckBoxList,我想从页面上用SqlDataSource加载它,从而使我可以将控件的多个实例与不同的源一起使用。

My first attempt was to expose the checkboxlist's DataSourceID in my user control: 我的第一次尝试是在用户控件中公开复选框列表的DataSourceID:

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: 并通过我的aspx进行设置,就像我对其他控件所做的那样:

<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. 即使找到了SqlDataSource,并且执行了DataBind(),我的复选框列表仍然没有加载。 Am I missing something obvious? 我是否缺少明显的东西?

Thanks in advance! 提前致谢!

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 . 所以,问题是,我的sqldatasource期望有一个参数,但我可以发誓从一开始就将其声明为Session参数,但是……我没有,我将其声明为SIMPLE参数(因为我没有给它赋值)一个值, 由于变量/参数为空sqldatasource正在取消选择

At the end, yes, just exposing the Checkboxlist's DataSourceID property is good enough! 最后,是的,仅公开Checkboxlist的DataSourceID属性就足够了!

In UserControl 在UserControl中

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

In aspx 在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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM