簡體   English   中英

使用DataSource的ASP.net用戶控件

[英]ASP.net user control with DataSource

我的用戶控件有多個組件,但是主要是CheckBoxList,我想從頁面上用SqlDataSource加載它,從而使我可以將控件的多個實例與不同的源一起使用。

我的第一次嘗試是在用戶控件中公開復選框列表的DataSourceID:

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

並通過我的aspx進行設置,就像我對其他控件所做的那樣:

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

沒用!...因此,在互聯網上找到了(並嘗試過)以下內容。

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();
        }
    }
}

即使找到了SqlDataSource,並且執行了DataBind(),我的復選框列表仍然沒有加載。 我是否缺少明顯的東西?

提前致謝!

好吧, 睡個好覺之后 ,我發現了問題,完全是我的錯,我將發布正確的代碼,以防萬一有人需要做類似的事情...

所以,問題是,我的sqldatasource期望有一個參數,但我可以發誓從一開始就將其聲明為Session參數,但是……我沒有,我將其聲明為SIMPLE參數(因為我沒有給它賦值)一個值, 由於變量/參數為空sqldatasource正在取消選擇

最后,是的,僅公開Checkboxlist的DataSourceID屬性就足夠了!

在UserControl中

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

在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