单击按钮 - 无操作

时间:2013-09-21 04:14:09

标签: c# asp.net visual-studio-2012 sql-server-2012

我正在尝试从数据库中检索信息。用户输入他正在寻找ID文本框的人的ID,而不是按显示按钮。网格视图应显示结果。但按下按钮时,没有任何反应。任何人都可以帮助或告诉我应该检查什么? 按钮代码:

protected void btnDisplay_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection("Data Source="Name";Initial Catalog="Name";Integrated Security=True");
            SqlCommand cmd = new SqlCommand("displayData", conn);
            conn.Open();
            cmd.Parameters.Add("@ID", System.Data.SqlDbType.Int).Value = Convert.ToInt32(txtID.Text);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataReader rd = cmd.ExecuteReader();
            grvResults.DataSource = rd;
            grvResults.DataBind();
        }

这是存储过程:

USE ["Name"]
GO  
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[displayData] (@ID int)
as
begin
SELECT * FROM Customers WHERE ID = @ID
end

这是显示数据方法:

public List<Customer> displayData()
        {
            List<Customer> lst = new List<Customer>();
            SqlConnection conn = new SqlConnection("Data Source="Name";Initial Catalog="Name";Integrated Security=True");
            SqlCommand cmd = new SqlCommand("Select * From Customers", conn);
            conn.Open();
            SqlDataReader rd = cmd.ExecuteReader();
            while (rd.Read())
            {
                lst.Add(new Customer()
                {
                    ID = rd.GetInt32(0),
                    FName = rd.GetString(1),
                    LName = rd.GetString(2)

                });
            }
            return lst;
        }

aspx for button:

<asp:Button ID="btnDisplay" runat="server" Text="Display" OnClick="btnDisplay_Click" />

3 个答案:

答案 0 :(得分:0)

在代码隐藏中尝试使用以下代码。它应该正确绑定并填充gridview。如果有效,请从那里添加更多代码。如果它不起作用,请查看连接字符串之类的内容。让我知道。

        SqlConnection conn = new SqlConnection("Data Source="?";Initial Catalog="?";Integrated Security=True");

        SqlCommand cmd = new SqlCommand("SELECT * FROM Customers WHERE ID = @ID", conn);
        cmd.Parameters.Add("@ID", System.Data.SqlDbType.Int).Value = Convert.ToInt32(txtID.Text);

        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = cmd;

        DataTable dt = new DataTable();

            conn.Open();
            da.Fill(dt);
            conn.Close();

        grvResults.DataSource = dt;
        grvResults.DataBind();

答案 1 :(得分:0)

您是否尝试在按钮单击中设置断点。它是否打破了断点。如果没有,那么我会说删除

OnClick="btnDisplay_Click" 
从您的aspx页面

。然后通过选择控件及其事件从.cs页面添加默认点击事件。他们尝试逐行调试。

答案 2 :(得分:0)

我认为你让自己太复杂了 首先这样做: Select the first option: SQL statement or stored procedure Then select your stored procedure Select The source Control (your Textbox) then click next

Then enter your client Id and click the button. NOTE on the button click you need to write this: **SqlDataSource1.DataBind();**

这应该让它发挥作用。您已经有了一个存储过程,使其更容易。 (对不起,图片,我想确保你明白这一点)

相关问题