|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
put control in a repeater cell from code behindHi;
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? 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. ==================================================
Other interesting topics
Major problem with FileUpload if bad filename
FormView - FindControl only works for default view Bind data to control inside a Repeater Master/Details cloning a record? Refreshing TreeView CrossPage Posting and the How Do I Webcast repeater control - change the background color depending on a value DataList question FileUpload name goes away Master Page - File References |
|||||||||||||||||||||||