在按钮单击时在单个网格视图中显示不同查询的结果

时间:2018-04-26 09:09:52

标签: c# asp.net gridview

我有一个网格视图'和我的网络表单中的三个按钮。现在当我点击按钮1'我想在'网格视图中显示query1的相应结果。并且对于' button2'它应该显示query2的结果。对于button3也是如此。

我的网络表单的HTML:

<%@ Page Title="" Language="C#" MasterPageFile="~/Master Page/Site1.Master" AutoEventWireup="true" CodeBehind="Dashboard.aspx.cs" Inherits="onlineshopping.Master_Page.WebForm1" %>

<asp:Content ID="Content1" runat="server" contentplaceholderid="BillingContent">

    <div>
        <table>
            <tr>
                <td>
                    <asp:Button ID="btn_dsbrdItems" runat="server" Text="Show Items" />
                </td>
                <td style="width:20px"></td>
                <td>
                    <asp:Button ID="btn_dsbrdShowInvoices" runat="server" Text="Show Invoices" />
                </td>
                <td style="width:20px"></td>
                <td>
                    <asp:Button ID="btn_dsbrShowUsers" runat="server" Text="Show Users" />
                </td>
                <td style="width:20px"></td>
            </tr>

        </table>
        <table>
             <tr>
                <td>
                    <asp:GridView ID="gv_dashboard" runat="server" AutoGenerateColumns="false">

                    </asp:GridView>
                </td>
            </tr>
        </table>

我知道我可以将查询结果绑定到&#39;网格视图&#39;我以前做过但我不知道如何做到这一点。我尝试了一些东西。我知道它的愚蠢和不会工作。我仍然尝试过,并没有像我预期的那样工作。这就是我的尝试。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace onlineshopping.Master_Page
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings.Get("connectionstring").ToString());
        string sql = "";
        DataSet ds = new DataSet();
        SqlDataAdapter da;
        protected void Page_Load(object sender, EventArgs e)
        {
            con.Open();
        }

        protected void btn_dsbrdItems_Click(object sender, EventArgs e)
        {
            if(con.State==ConnectionState.Closed)
            { con.Open(); }
            sql = "select * from item";
            da = new SqlDataAdapter(sql, con);
            da.Fill(ds);
            gv_dashboard.DataSource = ds;
            gv_dashboard.DataBind();
            con.Close();

        }

        protected void btn_dsbrdShowInvoices_Click(object sender, EventArgs e)
        {
            if (con.State == ConnectionState.Closed)
            { con.Open(); }
            sql = "select * from invoice";
            da = new SqlDataAdapter(sql, con);
            da.Fill(ds);
            gv_dashboard.DataSource = ds;
            gv_dashboard.DataBind();
            con.Close();
        }

        protected void btn_dsbrShowUsers_Click(object sender, EventArgs e)
        {
            if (con.State == ConnectionState.Closed)
            { con.Open(); }
            sql = "select * from tbl_users";
            da = new SqlDataAdapter(sql, con);
            da.Fill(ds);
            gv_dashboard.DataSource = ds;
            gv_dashboard.DataBind();
            con.Close();
        }
    }
}

This is my web form

我正在使用&#39;母版页&#39;对于side&#39; menu&#39;。如果之前询问过此问题,请帮我找到该链接。我无法找到。

0 个答案:

没有答案