如何在 asp.net 中动态创建动态控件;下拉列表上的 IndexChanged 事件在回发后不会保留/触发

本文关键字:事件 IndexChanged 触发 保留 net asp 动态 创建 下拉列表 动态控件 | 更新日期: 2023-09-27 18:36:13

我有

几个动态下拉列表

将选定的索引更改事件附加到它们,

但发生的情况是,一旦首次触发索引更改事件并且回发完成,它们就会丢失该 INDEXCHANGE 事件,但不会丢失项目值

所以它保存了下拉列表的状态,但不附加 回发上的选定索引更改事件。

AspxPage:

 <div>
    <asp:PlaceHolder ID="mp_1" runat="server"></asp:PlaceHolder>
    </div>

C#:

public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Init(object sender, EventArgs e)
        {
        }
        private void ddl_IndexChanged(object sender, EventArgs e)
        {
            Response.Write("dsfsdf");
        }           
        protected void Page_Load(object sender, EventArgs e)
        {
 DropDownList ddl_1 = new DropDownList();
                ddl_1.ID = "ddl_1";
                ddl_1.AutoPostBack = true;
                ddl_1.EnableViewState = true;
                ddl_1.SelectedIndexChanged += new EventHandler(ddl_IndexChanged);
                this.mp_1.Controls.Add(ddl_1); 

            if(!IsPostBack)
            {
                DropDownList ddl_1 = (DropDownList)Page.FindControl("ddl_1");
                if (ddl_1 != null)
                {
                    List<ListItem> items = new List<ListItem>();
                    items.Add(new ListItem("Item 2", "Value 2"));
                    items.Add(new ListItem("Item 1", "Value 1"));
                    items.Add(new ListItem("Item 3", "Value 3"));
                    ddl_1.Items.AddRange(items.ToArray());
                }                
            }               
        }
    }

如何在 asp.net 中动态创建动态控件;下拉列表上的 IndexChanged 事件在回发后不会保留/触发

创建和绑定动态子控件的最稳定方法是重写 CreateChildControls 方法:

查看官方样本

AspxPage:

 <div>
    <asp:PlaceHolder ID="mp_1" runat="server"></asp:PlaceHolder>
    </div>

C#:

public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            DropDownList ddl_1 = new DropDownList();
            ddl_1.ID = "ddl_1";
            ddl_1.AutoPostBack = true;
            ddl_1.EnableViewState = true;
            ddl_1.SelectedIndexChanged += new EventHandler(ddl_IndexChanged);
            this.mp_1.Controls.Add(ddl_1); 
        }
        private void ddl_IndexChanged(object sender, EventArgs e)
        {
            Response.Write("dsfsdf");
        }           
        // add entries in page load method not in init method
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                DropDownList ddl_1 = (DropDownList)Page.FindControl("ddl_1");
                if (ddl_1 != null)
                {
                    List<ListItem> items = new List<ListItem>();
                    items.Add(new ListItem("Item 2", "Value 2"));
                    items.Add(new ListItem("Item 1", "Value 1"));
                    items.Add(new ListItem("Item 3", "Value 3"));
                    ddl_1.Items.AddRange(items.ToArray());
                }                
            }               
        }
    }