UserControl中的下拉列表:未引发事件

本文关键字:事件 下拉列表 UserControl | 更新日期: 2023-09-27 18:20:10

我有一个名为"LettersDropControl"的用户控件。它有一个下拉列表。我在aspx页面中使用此控件。当我更改所选项目时,它不会调用ddlLetters_SelectedIndexChanged事件处理程序?我需要做什么更改才能执行ddlLetters _SelectedIndexChangd事件处理程序中的代码?

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

UserControl中的下拉列表:未引发事件

检查是否已为相关下拉列表添加了AutoPostBack属性。像这个

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

相关文章: