Home All Groups Group Topic Archive Search About

put control in a repeater cell from code behind

Author
21 Mar 2006 6:43 PM
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?

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

Author
22 Mar 2006 5:55 AM
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.

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

Bookmark and Share