在C#中使用Databind,DropDownList不会在SelectedIndexChanged上触发事件

时间:2011-10-19 14:53:01

标签: c# asp.net data-binding drop-down-menu

我有以下下拉列表:

<%@ 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;
    }

无论我做什么,我都无法填充标签。如果我用静态列表项替换数据绑定,一切都很好用!我已经尝试了几乎所有的建议,但没有任何作用(反正不适合我!)。

1 个答案:

答案 0 :(得分:4)

您的代码为onselecteddindexchanged,应为OnSelectedIndexChanged

套管无关紧要,但请注意onselected...

之后的额外“d”