【问题标题】:DropDownList OnSelectedIndexChanged not firing (AutoPostBack is "true", no other issues found)DropDownList OnSelectedIndexChanged 未触发(AutoPostBack 为“true”,未发现其他问题)
【发布时间】: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) 永远不会更新。根据调试,该方法永远不会被访问。

【问题讨论】:

  • 尝试关闭视觉工作室并重新打开。有时它会起作用
  • 我试过了,可惜没用。

标签: c# html asp.net .net


【解决方案1】:

您应该使用 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();
}

【讨论】:

  • 如果这是一个个人项目,我会使用它,但遗憾的是它是用于我正在学习的课程,我认为 ScriptManager 超出了我们正在学习的范围。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-02-20
  • 2013-09-13
  • 2016-05-24
  • 1970-01-01
  • 2011-08-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多