动态添加 DropDownList

发布于 2025-01-01 01:38:34 字数 1260 浏览 0 评论 0原文

我正在尝试动态添加下拉菜单。 除了我看不到下拉菜单之外没有错误 这是代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

前事休说 2025-01-08 01:38:35

问题可能在于您没有尽早将控件添加到页面。需要在页面生命周期的早期添加控件以将其事件绑定在一起。

您在 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 the Init event or overriding the CreateChildControls method.

And also make sure you do this on EVERY page request including postbacks.

Hope this works for you.

黯淡〆 2025-01-08 01:38:35

似乎由于 IsPostBack 条件,此代码仅在重新发布页面或触发事件时才会运行。

It seems like because of the IsPostBack condition this code will only run if there page is reposted or have an event triggered.

娇俏 2025-01-08 01:38:35
  1. 将该代码移动到 OnInit (Page_Init) 中,
  2. 删除向控件树添加控件时的 if(Ispostback) 检查。动态添加控件时,几乎从不需要注意回发状态。
  1. move that code to OnInit (Page_Init)
  2. remove if(Ispostback) check when adding controls to control tree. Almost never you need to mind the postback state when adding controls dynamically.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文