创建ASP.Net DataBound控件,该控件可以呈现多个表格 [英] Creating ASP.Net DataBound control that renders table with multiple tbodies

查看:253
本文介绍了创建ASP.Net DataBound控件,该控件可以呈现多个表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个数据绑定控件,其中每个项目呈现< tbody>< ItemTemplate>< / tbody> 。此外,我还想将< HeaderTemplate> 放在< thead> 而不是< tbody> 行作为< asp:DataList> 。我需要完全控制项目模板。它需要能够呈现可编辑的行,例如 DataList

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.

我相信我需要一个自定义的控件(如果没有,请让我知道 - 这将节省很多时间)。什么是最好的家长班延长?我的猜测是,它是 DataList DataBoundControl

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.

推荐答案

看起来我正在寻找的是< asp:Repeater> 与一些额外的代码进行编辑,因为它不'有一个< EditItemTemplate>

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

这篇关于创建ASP.Net DataBound控件,该控件可以呈现多个表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆