Multiple GridViews on One Page

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

Can I make all these grids on one page, that is, when I click link1 , then only the grid corresponding to this link will be displayed on the page, and when I click 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 on how and where should I start?

+4
source share
2 answers

Since you specified web forms in your tag, the multiview control will do you wonders. Create a multi-view control for different gridviews. Hope this helps.

Using

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 for

  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; } 
+3
source

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

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

And for each link, you can use the asp.net linkbutton link (because this element has the appearance of an html link).

Your aspx code will look like 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 look 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 click of the button of the link button, you can set the visibility of the corresponding div to true and the others to false, and then you call the method that loads the corresponding gridview.

I hope I helped you.

Sincerely.

0
source

All Articles