如何在 asp.net 中即时创建动态控件;下拉列表中的 IndexChanged 事件在 post 返回后不会 persist/fire
How to create dynamic controls on the fly in asp.net;IndexChanged Event on dropdowns does not persist/fire after post back
我有
several dynamic drop downs
将选定的索引更改事件附加到它们,但是一旦第一次触发索引更改事件并且一旦回发完成,它们就会丢失该索引更改事件,但不会丢失项目值
so its saving the state of the dropdowns but not attaching the
selected index change event on a post back.
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());
}
}
}
}
创建和绑定动态子控件的最稳定方法是覆盖 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());
}
}
}
}
我有
several dynamic drop downs
将选定的索引更改事件附加到它们,但是一旦第一次触发索引更改事件并且一旦回发完成,它们就会丢失该索引更改事件,但不会丢失项目值
so its saving the state of the dropdowns but not attaching the selected index change event on a post back.
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());
}
}
}
}
创建和绑定动态子控件的最稳定方法是覆盖 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());
}
}
}
}