Skip to content Skip to sidebar Skip to footer

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:Repeater id="rItems" runat="server" OnItemCommand="rItems_ItemCommand" OnItemDataBound="rItems_ItemDataBound">
    <HeaderTemplate>
        <table>
            <thead>...</thead>
    </HeaderTemplate>
    <ItemTemplate>
        <tbody id="itemRows" runat="server">
            ...
            <asp:Button ... CommandName="Edit" />
            ...
        </tbody>
        <tbody id="editItemRows" runat="server" visible="false">...</tbody>
    </ItemTemplate>
    <FooterTemplate>
        </table>
    </FooterTemplate>
</asp:Repeater>

.ascx.cs

private int m_editIndex = -1;

protected void rItems_ItemCommand(object sender, RepeaterCommandEventArgs e)
{
    switch (e.CommandName)
    {
        case "Edit":
            m_editIndex = e.Item.ItemIndex;
            break;
        ... // Cancel, Update, Delete, etc
        case "Cancel":
            m_editIndex = -1;
            break;
    }

    BindItemsDataSource();
}

protected void rItems_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"