【发布时间】:2019-12-06 04:48:57
【问题描述】:
ListBox1 连接到 SQL 数据库,并将查询到的数据绑定到 ddCountries(DropDownList)。当一个 DropDownList 项目被选中时,它应该更新页面上其他地方的标签,但是由于某种原因,当应用程序运行时根本没有访问 ddCountries_SelectedIndexChanged 方法。
是的,AutoPostBack 设置为“true”。
默认.aspx
列表框1:
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>
默认.aspx.cs
protected void ddCountries_SelectedIndexChanged(object sender, EventArgs e)
{
lblThankYou.Visible = true;
lblThankYou.Text = "You have selected " + ddCountries.SelectedValue.ToString() + "!";
}
没有错误消息,标签 (lblThankYou) 永远不会更新。根据调试,该方法永远不会被访问。
【问题讨论】:
-
尝试关闭视觉工作室并重新打开。有时它会起作用
-
我试过了,可惜没用。