UserControl中的下拉列表:事件未被提升

时间:2012-01-11 09:59:48

标签: c# asp.net .net vb.net

我有一个名为“LettersDropControl”的用户控件。它有一个下拉列表。我在aspx页面中使用此控件。当我更改所选项时,它没有调用ddlLetters_SelectedIndexChanged事件处理程序?为了在ddlLetters_SelectedIndexChanged事件处理程序中执行代码,我需要做些什么改变?

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="LettersDropControl.ascx.cs" Inherits="MSAJAX1.LettersDropControl" %>

    public partial class LettersDropControl : System.Web.UI.UserControl
    {

      private string selectedLetter;
      public string SelectedLetter
     {
        get { return selectedLetter; }
        set { selectedLetter = value; }
    }


    // Delegate 
    public delegate void OnButtonClick(string strValue);
    // Event 
    public event OnButtonClick btnHandler;


    protected void ddlLetters_SelectedIndexChanged(object sender, EventArgs e)
    {
        throw new Exception("TEST");

        SelectedLetter = ddlLetters.SelectedItem.Value;
        // Raise event if there is a subscription
        if (btnHandler != null)
        {
            btnHandler(string.Empty);
        }
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ddlLetters.Items.Add("A");
            ddlLetters.Items.Add("B");

        }
    }


}

aspx页面是

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="MSAJAX1.Default" %>
<%@ Register TagPrefix="CP" TagName="LetterCtrl" Src="LettersDropControl.ascx" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
 </head>
<body>
    <form id="form1" runat="server">
   <div>
 <CP:LetterCtrl id="LetterCtrl1" runat="server" />
</div>
</form>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

检查您是否为相关下拉列表添加了AutoPostBack属性。像这样的东西

<asp:DropDownList 1" runat="server" AutoPostBack="True">