单击不同的按钮填充不同的gridview

时间:2012-03-26 14:18:18

标签: asp.net gridview

在我的网页标题中,有几个按钮。我想要的是单击每个按钮以在gridview中显示所需的表。

点击button1     在gridview 1中显示表。

点击button2 在gridview 2中显示表格,Gridview 1消失了。

标题和按钮在页面中保持不变。我不想将当前页面重定向到不同的页面。我只想在一个网页中实现这个功能。

4 个答案:

答案 0 :(得分:0)

C#

buttin1_Onclick(object sender, EventArgs e)
{ gridview1.visible = true; gridview1.DataBind(); gridview2.visible = false;}

buttin2_Onclick(object sender, EventArgs e)
{ gridview2.visible = true; gridview2.DataBind(); gridview1.visible = false;}

 buttin1_Onclick(object sender, EventArgs e)
{ gridview1.DataBind(); gridview2.Rows.Clear();}

buttin2_Onclick(object sender, EventArgs e)
{ gridview2.DataBind(); gridview1.Rows.Clear();}

答案 1 :(得分:0)

<强> ASPX

  <div>
         <asp:GridView ID="GridView1" runat="server">
         </asp:GridView>
         <asp:GridView ID="GridView2" runat="server">
         </asp:GridView>
         <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
         <asp:Button ID="Button2" runat="server" Text="Button" onclick="Button2_Click" />
     </div>

<强> C#

protected void Button1_Click(object sender, EventArgs e)
{
    GridView1.Visible = true;
    GridView2.Visible = !GridView1.Visible;
    GridView1.DataBind();
}

protected void Button2_Click(object sender, EventArgs e)
{
    GridView1.Visible = false;
    GridView2.Visible = !GridView1.Visible;
    GridView2.DataBind();
}

您可能需要传入.DataBind()调用,具体取决于您将数据绑定到网格视图的方式。

答案 2 :(得分:0)

点击每个按钮,只需根据需要绑定GridView即可。例如:

Button1点击了:

protected void Button1_OnClick(object sender, EventArgs e) 
{
    grdYourGrid1.DataSource = yourDataSource;
    grdYourGrid1.DataBind();
    grdYourGrid1.Visible = true;

    grdYourGrid2.DataSource = null;
    grdYourGrid2.DataBind();
    grdYourGrid2.Visible = false;
}

然后对Button2做相反的事情。我建议重新绑定和归零非可见网格,以便ViewState在不需要时不会变得臃肿。

答案 3 :(得分:0)

您可以将其放在Page_Load

button1.Click+=(s,ev)=>
    {
        gridview1.visible = true; 
        gridview1.DataBind();
    };
button2.Click+=(s,ev)=>
    {
        gridview2.DataBind(); 
        gridview1.visible = false;
    };