Creating Asp.net Databound Control That Renders Table With Multiple Tbodies
I need to create a data-bound control where each item renders . Further, I'd also like to put the in a
Solution 1:
Looks like what I'm looking for is <asp:Repeater>
with a little extra code for editing since it doesn't have an <EditItemTemplate>
.
.ascx
<asp:Repeaterid="rItems"runat="server"OnItemCommand="rItems_ItemCommand"OnItemDataBound="rItems_ItemDataBound"><HeaderTemplate><table><thead>...</thead></HeaderTemplate><ItemTemplate><tbodyid="itemRows"runat="server">
...
<asp:Button...CommandName="Edit" />
...
</tbody><tbodyid="editItemRows"runat="server"visible="false">...</tbody></ItemTemplate><FooterTemplate></table></FooterTemplate></asp:Repeater>
.ascx.cs
privateint m_editIndex = -1;
protectedvoidrItems_ItemCommand(object sender, RepeaterCommandEventArgs e)
{
switch (e.CommandName)
{
case"Edit":
m_editIndex = e.Item.ItemIndex;
break;
... // Cancel, Update, Delete, etccase"Cancel":
m_editIndex = -1;
break;
}
BindItemsDataSource();
}
protectedvoidrItems_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item | e.Item.ItemType == ListItemType.AlternatingItem)
{
bool edit = e.Item.ItemIndex == m_editIndex;
HtmlGenericControl tbody = e.Item.FindControl("itemRows") as HtmlGenericControl;
tbody.Visible = !edit;
tbody = e.Item.FindControl("editItemRows") as HtmlGenericControl;
tbody.Visible = edit;
if (edit)
{
PopulateEditableFieldValues(e.Item);
}
}
}
Post a Comment for "Creating Asp.net Databound Control That Renders Table With Multiple Tbodies"