Multiple GridViews on one page

2019-05-07 14:51发布

问题:

I am creating an application in C#. I have created a page in which there are 5 links. Each link opens a different page where there is a GridView.

Can I make all these grids on one page - i.e. when I click on link1, then only the grid corresponding to that link will appear on the page and when I click on link2, then only the grid for link2 appears? Only the grid for the activated link should be visible and all other grids are hidden.

Can someone help me in how or where I should start?

回答1:

Since you specified webforms in your tag,the multiview control will do you wonders.Declare a multiview control with different views for each gridview.Hope this helps.

Usage

MarkUp

 <form id="form1" runat="server">


    <asp:LinkButton ID="LinkButton1"  Text="Link1" runat="server" OnClick="LinkButton1_Click">LinkButton</asp:LinkButton>
    <br>
    <asp:LinkButton ID="LinkButton2" runat="server" Text="Link2" OnClick="LinkButton2_Click">LinkButton</asp:LinkButton></br>

<asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="0">

    <asp:View ID="View1" runat="server" >

        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>

    </asp:View>

      <asp:View ID="View2" runat="server">
      <asp:GridView ID="GridView2" runat="server">
    </asp:GridView>
</asp:View>
</asp:MultiView>

</form>

Code Behind

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        //retrieve data
        GridView1.DataBind();
        MultiView1.ActiveViewIndex=0;
     }


    protected void LinkButton2_Click(object sender, EventArgs e)
    {
         //retrieve data
        GridView1.DataBind();
        MultiView1.ActiveViewIndex=1;
    }


回答2:

My solution is a not such elegant way to do this as Abide Masaraure pointed. I suggest you to follow his answer.

But you also can put each gridview inside a different div, and set the attribute "runat=server" in these divs.

And for each link you can use an asp.net linkbutton (because this element has a default look of an html link).

Your aspx code will look simillar to this:

<asp:LinkButton runat="server" id="lnkButton_1" text="Link 1" OnClick="lnkButton_1_Click"/>
<asp:LinkButton runat="server" id="lnkButton_2" text="Link 2" OnClick="lnkButton_2_Click"/>
<asp:LinkButton runat="server" id="lnkButton_3" text="Link 3" OnClick="lnkButton_3_Click"/>
<asp:LinkButton runat="server" id="lnkButton_4" text="Link 4" OnClick="lnkButton_4_Click"/>
<asp:LinkButton runat="server" id="lnkButton_5" text="Link 5" OnClick="lnkButton_5_Click"/>

<div runat="server" id="divGrid_1" Visible="false">
   <asp:GridView runat="server" id="grid_1"></asp:GridView>
</div>

<div runat="server" id="divGrid_2" Visible="false">
   <asp:GridView runat="server" id="grid_2"></asp:GridView>
</div>

<div runat="server" id="divGrid_3" Visible="false">
   <asp:GridView runat="server id="grid_3"></asp:GridView>
</div>

<div runat="server" id="divGrid_4" Visible="false">
   <asp:GridView runat="server" id="grid_4"></asp:GridView>
</div>

<div runat="server" id="divGrid_5" Visible="false">
   <asp:GridView runat="server" id="grid_5"></asp:GridView>
</div>

Your aspx.cs (codebehind) code will lool like this:

protected void lnkButton_1_Click(object sender, EventArgs E)
{
   divGrid_1.Visible = true;
   divGrid_2.Visible = false;
   divGrid_3.Visible = false;
   divGrid_4.Visible = false;
   divGrid_5.Visible = false;

   LoadGridView_1();
}

And for each link button event click you can set the visibility of the correspondent div to true and the other ones to false, and then you call the method that load the correspondent gridview.

I expect I've helped you.

Best regards.