Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
314 views
in Technique[技术] by (71.8m points)

c# - Creating ASP.Net DataBound control that renders table with multiple tbodies

I need to create a data-bound control where each item renders <tbody><ItemTemplate></tbody>. Further, I'd also like to put the <HeaderTemplate> in a <thead> rather than a <tbody> row as the <asp:DataList> does. I need full control of the item template. It needs to be able to render an editable row, like the DataList does.

I believe I need to make a custom control for this (but if not, please let me know--it would save a lot of time). What's the best parent class to extend? My guess is that it's either DataList or DataBoundControl.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...