动态添加 DropDownList
我正在尝试动态添加下拉菜单。 除了我看不到下拉菜单之外没有错误 这是代码:
protected void Page_Load(object sender, EventArgs e)
{
if(Ispostback)
{
DropDownList ddlPercent = new DropDownList();
ddlPercent .ID = "ddlDiscountPercent";
ddlPercent .AutoPostBack = true;
Panel1.Controls.Add(ddlPercent );
ddlPercent .ID = "ddlPercent " + 1;
ddlPercent .Items.Add(new ListItem("5", "5%"));
ddlPercent .Items.Add(new ListItem("10", "10%"));
ddlPercent .Items.Add(new ListItem("15", "15%"));
ddlPercent .Items.Add(new ListItem("20", "20%"));
ddlPercent .Items.Add(new ListItem("30", "30%"));
ddlPercent .Items.Add(new ListItem("50", "50%"));
ddlPercent.SelectedIndexChanged +=
new EventHandler(ddlPercent_SelectedIndexChanged);
ContentPlaceHolder cph =
(ContentPlaceHolder)this.Master.FindControl("MainContent");
cph.Controls.Add(ddlPercent );
// ddlPercent.SelectedIndex =(Int32) ViewState["ddl_index"];
}
}
protected void ddlPercent_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ComboBox=(DropDownList)sender;
ViewState["ddl_index"] = ComboBox.SelectedValue;
}
I am trying to add a dropdown dynamically.
No errors except that I don't see the dropdown
Here's the code:
protected void Page_Load(object sender, EventArgs e)
{
if(Ispostback)
{
DropDownList ddlPercent = new DropDownList();
ddlPercent .ID = "ddlDiscountPercent";
ddlPercent .AutoPostBack = true;
Panel1.Controls.Add(ddlPercent );
ddlPercent .ID = "ddlPercent " + 1;
ddlPercent .Items.Add(new ListItem("5", "5%"));
ddlPercent .Items.Add(new ListItem("10", "10%"));
ddlPercent .Items.Add(new ListItem("15", "15%"));
ddlPercent .Items.Add(new ListItem("20", "20%"));
ddlPercent .Items.Add(new ListItem("30", "30%"));
ddlPercent .Items.Add(new ListItem("50", "50%"));
ddlPercent.SelectedIndexChanged +=
new EventHandler(ddlPercent_SelectedIndexChanged);
ContentPlaceHolder cph =
(ContentPlaceHolder)this.Master.FindControl("MainContent");
cph.Controls.Add(ddlPercent );
// ddlPercent.SelectedIndex =(Int32) ViewState["ddl_index"];
}
}
protected void ddlPercent_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ComboBox=(DropDownList)sender;
ViewState["ddl_index"] = ComboBox.SelectedValue;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题可能在于您没有尽早将控件添加到页面。需要在页面生命周期的早期添加控件以将其事件绑定在一起。
您在
Load
事件中执行此操作,但为时已晚。尝试在Init
事件期间添加它或重写CreateChildControls
方法。并且还要确保在每个页面请求(包括回发)上执行此操作。
希望这对你有用。
The problem may be if you are not adding the control to the page early enough. Controls need to be added early in the page lifecycle to get their events tied in.
You're doing it in the
Load
event, which is too late. Try adding it during theInit
event or overriding theCreateChildControls
method.And also make sure you do this on EVERY page request including postbacks.
Hope this works for you.
似乎由于
IsPostBack
条件,此代码仅在重新发布页面或触发事件时才会运行。It seems like because of the
IsPostBack
condition this code will only run if there page is reposted or have an event triggered.if(Ispostback)
检查。动态添加控件时,几乎从不需要注意回发状态。if(Ispostback)
check when adding controls to control tree. Almost never you need to mind the postback state when adding controls dynamically.