简体   繁体   English

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

[英]DropDownList doesn't Fire Event on SelectedIndexChanged Using Databind in C#

I have the following dropdownlist: 我有以下下拉列表:

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

And this code behind: 这个代码背后:

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

No matter what I do I can't populate the label. 无论我做什么,我都无法填充标签。 All works great if I replace the databind with static listitems! 如果我用静态列表项替换数据绑定,一切都很好用! I've tried pretty much all the suggestions around, but nothing works (well not for me anyway!). 我已经尝试了几乎所有的建议,但没有任何作用(反正不适合我!)。

Your code says onselecteddindexchanged , it should be OnSelectedIndexChanged . 你的代码说onselecteddindexchanged ,它应该是OnSelectedIndexChanged

The casing should not matter, but notice the extra "d" after onselected... 套管无关紧要,但在选择后注意额外的“d” onselected...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM