[英]RadioButtonList set to AutoPostBack = true but still not firing OnSelectedIndexChanged
[英]DropDownList OnSelectedIndexChanged not firing (AutoPostBack is “true”, no other issues found)
ListBox1連接到SQL數據庫,並將查詢的數據綁定到ddCountries(DropDownList)。 選擇DropDownList項目時,應該更新頁面上其他位置的標簽,但是由於某些原因,在應用程序運行時根本無法訪問ddCountries_SelectedIndexChanged方法。
是的,AutoPostBack設置為“ true”。
Default.aspx
ListBox1:
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection countriesConnection = new SqlConnection();
countriesConnection.ConnectionString =
System.Web.Configuration.WebConfigurationManager.ConnectionStrings["CSharpClass1ConnectionString"].ConnectionString;
SqlCommand cmd = countriesConnection.CreateCommand();
int ContID = Convert.ToInt32(ListBox1.SelectedValue);
try
{
string query = "SELECT * FROM Country WHERE ContinentId=" + ContID + ";";
SqlDataAdapter adpt = new SqlDataAdapter(query, countriesConnection);
DataTable dt = new DataTable();
adpt.Fill(dt);
ddCountries.DataSource = dt;
ddCountries.DataBind();
ddCountries.DataTextField = "CountryName";
ddCountries.DataValueField = "ContinentId";
ddCountries.DataBind();
}
catch (Exception ex)
{
}
finally
{
cmd.Dispose();
countriesConnection.Close();
}
}
下拉列表:
<asp:DropDownList ID="ddCountries" runat="server" Height="16px" Width="238px" OnSelectedIndexChanged="ddCountries_SelectedIndexChanged" AutoPostBack="True">
<asp:ListItem Text="None" value=""></asp:ListItem>
</asp:DropDownList>
Default.aspx.cs
protected void ddCountries_SelectedIndexChanged(object sender, EventArgs e)
{
lblThankYou.Visible = true;
lblThankYou.Text = "You have selected " + ddCountries.SelectedValue.ToString() + "!";
}
沒有錯誤消息,標簽(lblThankYou)根本不會被更新。 根據調試,該方法永遠不會被訪問。
您應該使用UpdatePanle和ScriptManager:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always" ChildrenAsTriggers="true" >
<ContentTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" onselectedindexchanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem>item 1</asp:ListItem>
<asp:ListItem>item 2</asp:ListItem>
</asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
throw new NotImplementedException();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.