带参数到Gridview的ASP按钮

时间:2013-01-22 17:27:54

标签: asp.net vb.net gridview parameters

我有一个页面status.aspx,其中包含几个按钮,例如30天,60天和几天。 90天。我想根据点击的按钮将参数传递给现有的gridview。我的代码隐藏了gridview,如下所示:

    Dim cmd As SqlCommand = New SqlCommand("up_AcctStatus", conn)
    cmd.CommandType = CommandType.StoredProcedure

    Dim paramNumOfDays As New SqlParameter("@NumOfDays", SqlDbType.Int)
    paramNumOfDays.Value = "??? 30, 60 or 90 ???"
    cmd.Parameters.Add(paramNumOfDays)

GridView1.DataSource = cmd.ExecuteReader()         GridView1.DataBind()

1 个答案:

答案 0 :(得分:0)

如果你不想使用任何ORM而是sqlComand,你可以:

            protected void Button1_Click(object sender, EventArgs e)
            {
                Fill(60);
            }
            protected void Button2_Click(object sender, EventArgs e)
            {
                Fill(30);
            }

            private void Fill(int numberOfDays)
            {
                using (SqlConnection connection = new SqlConnection("connectionString"))
                {
                    connection.Open();

                    SqlDataAdapter sda = new SqlDataAdapter();
                    sda.SelectCommand = new SqlCommand("stored procedure name", connection);
                    sda.SelectCommand.CommandType = CommandType.StoredProcedure;
                    sda.SelectCommand.Parameters.AddWithValue("@NumOfDays", numberOfDays);

                    DataSet ds = new DataSet();
                    sda.Fill(ds, "result_table");

                    GridView1.DataSource = ds.Tables["result_table"];
                    GridView1.DataBind();
                }
            }

VB:

                Protected Sub Button1_Click(sender As Object, e As EventArgs)
                Fill(60)
                End Sub

                Protected Sub Button2_Click(sender As Object, e As EventArgs)
                    Fill(30)
                End Sub

                Private Sub Fill(numberOfDays As Integer)
                    Using connection As New SqlConnection("connectionString")
                        connection.Open()

                        Dim sda As New SqlDataAdapter()
                        sda.SelectCommand = New SqlCommand("stored procedure name", connection)
                        sda.SelectCommand.CommandType = CommandType.StoredProcedure
                        sda.SelectCommand.Parameters.AddWithValue("@NumOfDays", numberOfDays)

                        Dim ds As New DataSet()
                        sda.Fill(ds, "result_table")

                        GridView1.DataSource = ds.Tables("result_table")
                         GridView1.DataBind()
                    End Using
                End Sub