Niar
Niar

Reputation: 540

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'. 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.

                   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"

Upvotes: 0

Views: 319

Answers (1)

gzaxx
gzaxx

Reputation: 17600

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)

Upvotes: 3

Related Questions