多个选项卡上的单个gridview

时间:2014-03-24 18:41:20

标签: asp.net gridview tabcontainer

是否可以使用单个gridview并将其显示在多个选项卡上(ajax tabcontainer)? 我想在不同的选项卡上将不同的数据源绑定到网格。我目前正在使用3个网格视图(用于3个选项卡)。

<asp:TabContainer ID="TabContainer1" runat="server" UseVerticalStripPlacement="True" AutoPostBack="true" OnActiveTabChanged="TabContainer1_ActiveTabChanged">
        <asp:TabPanel ID="tp1" runat="server">
            <HeaderTemplate>
                Tab 1
            </HeaderTemplate>
            <ContentTemplate>
                <p>test tab 1</p>
                <asp:GridView ID="GridView1" runat="server" Width="100%"
                    ViewStateMode="Enabled" OnRowEditing="GridView1_RowEditing">
                    <Columns>
                        <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
                    </Columns>
                </asp:GridView>
            </ContentTemplate>
        </asp:TabPanel>
        <asp:TabPanel ID="tp2" runat="server">
            <HeaderTemplate>
                Tab 2
            </HeaderTemplate>
            <ContentTemplate>
                <p>test tab 2</p>
                <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="True" Width="100%"
                    ViewStateMode="Enabled">
                </asp:GridView>
            </ContentTemplate>
        </asp:TabPanel>
        <asp:TabPanel ID="tp3" runat="server">
            <HeaderTemplate>
                Tab 3
            </HeaderTemplate>
            <ContentTemplate>
                <p>test tab 3</p>
                <asp:GridView ID="GridView3" runat="server" AutoGenerateColumns="True" Width="100%"
                    ViewStateMode="Enabled">
                </asp:GridView>
            </ContentTemplate>
        </asp:TabPanel>
    </asp:TabContainer>
代码背后的代码:

 protected void TabContainer1_ActiveTabChanged(object sender, EventArgs e) 
{ 

    try 
    { 
        if (TabContainer1.ActiveTabIndex == 0) 
        {

            //bind datasource 1;

        }
        if (TabContainer1.ActiveTabIndex == 0) 
        {

            //bind datasource 2;

        }  
    }etc.

1 个答案:

答案 0 :(得分:0)

用于在tabpanel 2中显示gridview1

tp2.Controls.Add(GridView1);

一般表格

<TabPanelID>.Controls.Add(<GridviewID>);