Asp Button 不起作用,也不执行后面的功能

Asp Button doesn't work and doesn't execute function behind

提问人:issam merheb 提问时间:10/9/2018 最后编辑:Basissam merheb 更新时间:10/26/2018 访问量:85

问:

ASP 按钮不会触发,也不会执行 C# 代码,后面有代码,导致此问题的原因是什么

protected void searchhosp(object sender, EventArgs e) {
    string cityname = city.Value;
    DataBaseConnection db  = new DataBaseConnection();
    db .disconnect();

    string sql  = "select Hospital_ID , Hospital_Name From TBL_Hospitals where City like'%"+cityname+"%'";
    SqlCommand cmd  = new SqlCommand(sql , db.connect());
    hospSelect.DataSource = cmd.ExecuteReader();
    hospSelect.DataTextField = "Hospital_Name";
    hospSelect.DataValueField = "Hospital_ID";
    hospSelect.DataBind();
    hospSelect.Items.Insert(0, "--Choose Hospital--");             
}

<input type="text" class="input2"  id="city" runat="server" > 
    <select runat="server" class="chosen" name="slct"  onchange="myFunction2();"  id="hospSelect">
</select>

<asp:Button ID="Button2" OnClick="searchhosp" CssClass="btnn" runat="server" Text="Search" />
C# HTML asp.net .NET ASPBuject

评论

0赞 Chetan 10/9/2018
在您的代码中处于什么位置?asp:button
0赞 issam merheb 10/9/2018
<asp:Button ID=“Button2” AutoPostBack=“True” CssClass=“btnn” OnClick=“searchhosp” runat=“server” Text=“Save” />
0赞 Chetan 10/9/2018
当您单击按钮时,您可以调试吗?searchhosp
0赞 issam merheb 10/9/2018
是的,我想要这个

答:

0赞 Seano666 10/26/2018 #1

尝试将 AutoPostBack 属性添加到按钮。

 AutoPostBack = "true"

此外,请确保您的控件位于

<form>

元素。