在c#中使用数据绑定时,DropDownList不会触发SelectedIndexChanged事件

本文关键字:DropDownList 事件 SelectedIndexChanged 定时 绑定 数据 | 更新日期: 2023-09-27 18:11:47

我有以下下拉列表:

<%@ Page Title="" Language="C#" MasterPageFile="~/myMasterPage.master" AutoEventWireup="true" CodeFile="testdropdown.aspx.cs" Inherits="testdropdown" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server">
    <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:DropDownList ID="ddlDeliveryAddresses" runat="server" 
        DataTextField="delAddressShort" DataValueField="delID" 
        onselecteddindexchanged="ddlDeliveryAddresses_SelectedIndexChanged"
        AppendDataBoundItems="True" 
        AutoPostBack="True">
        <asp:ListItem Selected='True' Text='--select--' Value='-1'></asp:ListItem>    
    </asp:DropDownList>
</asp:Content>

后面的代码:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SqlConnection conn;
            SqlCommand comm;
            SqlDataReader reader;
            conn = new SqlConnection(iceConns.iconn);
            comm = new SqlCommand("Customers.sl_DeliverybyBillID", conn);
            comm.CommandType = CommandType.StoredProcedure;
            comm.Parameters.Add(new SqlParameter("@billID", SqlDbType.Int));
            comm.Parameters["@billID"].Value = 160;
            conn.Open();
            reader = comm.ExecuteReader();
            ddlDeliveryAddresses.DataSource = reader;
            ddlDeliveryAddresses.DataBind();
            reader.Close();
        }
    }
    protected void ddlDeliveryAddresses_SelectedIndexChanged(object sender, EventArgs e)
    {
        Label1.Text = ddlDeliveryAddresses.SelectedItem.Text;
    }

无论我做什么我都不能填充标签。如果我用静态列表替换数据绑定,一切都会很好!我已经尝试了几乎所有的建议,但没有一个工作(至少不是为我!)。

在c#中使用数据绑定时,DropDownList不会触发SelectedIndexChanged事件

你的代码写的是onselecteddindexchanged,应该是OnSelectedIndexChanged

大小写不重要,但要注意onselected...

后面的"d"