put control in a repeater cell from code behind

D

David Thielen

Hi;

I have a repeater block where the number of rows and the value of the first
column come from the data bound to the repeater control. But I need a second
column in the table where I place a control from my code behind.

I need to do this because the type of control depends on the data for that
row.

How can I do this?
 
S

Steven Cheng[MSFT]

Hi Dave,

As for the adding dynamic created controls into repeater rows depending on
the databound values, I think the "ItemDataBound" event is the one you're
looking for. we can dynamically create controls there and add them into
repeater row. Also, the event's parameter can let us check the current
databinding datarow's value do do some conditional customization. Here is a
simple example:

==========repeater template===
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1"
OnItemDataBound="Repeater1_ItemDataBound">
<ItemTemplate >
<hr style="width:100%" /><br />

</ItemTemplate>
</asp:Repeater>
======================

==========code behind=========
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs
e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{

string name = DataBinder.Eval(e.Item.DataItem,
"CategoryName","{0}");

if (e.Item.ItemIndex % 2 == 1)
{
TextBox txt = new TextBox();
txt.Text = name;
e.Item.Controls.Add(txt);
}else{
Label lbl = new Label();
lbl.Text = name;
e.Item.Controls.Add(lbl);
}
}
}
================================

Hope this helps.

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top