简体   繁体   English

如何找到ASP中继器控件并将数据绑定到它

[英]How to find asp repeater control and bind data to it

I have some asp repeater control with id like 'rpA','rpB' and so on upto 'rpZ'. 我有一些ID为'rpA','rpB'等直到'rpZ'的asp中继器控件。 What i want to do is i want to find the asp repeater in code behind and bind a datasource accordingly..the code so far i have written is is below,where 'i' is an int and it ranges from 65 to 90.And 'tb' is a DataTable. 我想做的是我想在后面的代码中找到asp中继器,并相应地绑定数据源。.到目前为止,我编写的代码如下,其中“ i”是一个int,范围从65到90。 'tb'是一个数据表。

                   string lblName = "lbl" + Convert.ToChar(i);
                   string repName = "rp" + Convert.ToChar(i);
                   FindControl(lblName).Visible = true;
                   FindControl(repName).Visible = true;
                   IDataBoundControl repID = FindControl(repName) as IDataBoundControl;
                   ITextControl lblID = FindControl(lblName) as ITextControl;
                   lblID.Text = (Convert.ToChar(i)).ToString();
                   repID.DataSource = tb;
                   repID.DataBind();

But i'm not able to to DataBind().The last line is giving an error"System.Web.UI.WebControls.IDataBoundControl does not contain a definition of DataBind" 但是我无法使用DataBind()。最后一行给出了错误“ System.Web.UI.WebControls.IDataBoundControl不包含DataBind的定义”

If you know the type then do this: 如果您知道类型,请执行以下操作:

var repID = FindControl(repName) as Repeater;
repID.DataSource = tb;
repID.DataBind();

No point to cast it to IDataBoundControl because this interface do not have method DataBind() (more info here ) 没有必要将其IDataBoundControlIDataBoundControl因为此接口没有方法DataBind() (更多信息,请IDataBoundControl 此处

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

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