单击按钮后显示网格

时间:2017-11-24 13:52:34

标签: c# html sql visual-studio

我正在尝试显示我的数据库中的记录网格,但每当我点击按钮时都没有任何反应 对不起要问这样一个非常愚蠢的问题,但我在这里总是菜鸟,我试过了 找到任何解决方案,但我找不到任何

HTML:

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            enter something</div>
    <p>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        </p>
    <p>
        <input id="Button1" type="button" value="Search" runat="server" onclick="Myp" /></p>
        <asp:GridView ID="GridView1" runat="server">
		</asp:GridView>
        test<input id="Hidden1" type="hidden" /></form>
    <p>
        &nbsp;</p>
    </body>
</html>

C#

 protected void Myp(object sender, System.EventArgs e)
{

    //Get the button that raised the event
    //Button btn = (Button)sender;
    string connStr = ConfigurationManager.ConnectionStrings["MyDbConn"].ToString();
    SqlConnection conn = new SqlConnection(connStr);
    conn.Open();
    SqlCommand cmd = new SqlCommand("SearchByName", conn);
    cmd.CommandType = CommandType.StoredProcedure;
    string name = TextBox2.Text;
    cmd.Parameters.Add(new SqlParameter("@name", name));
    GridView1.EmptyDataText = "No Records Found";
    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
   // SqlDataAdapter adapter = cmd.ExecuteReader();
    //adapter = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    adapter.Fill(ds);
    GridView1.DataSource = ds;
    GridView1.DataBind();
    GridView1.Visible = true;


}

1 个答案:

答案 0 :(得分:0)

尝试使用OnClick事件而不是onclick,这可能会针对Javascript函数(而不是服务器端Myp方法):

<input id="Button1" type="button" value="Search" runat="server" OnClick="Myp" />

此外,如果您的方法仍未,则应尝试查看Button1按钮的Events属性,以确保正确配置OnClick事件处理程序并指向Myp方法。