简体   繁体   English

通过DropDownList将动态ASP.NET控件添加到页面

[英]Add dynamic ASP.NET controls via a DropDownList to the page

- Primary Info: -主要信息:
In my recent project, I need to have a page with a DropDownList with some items like 'firstName' , 'lastName' , 'Age' and etc. I want to add optional controls to the page when every item selected by user. 在我最近的项目中,我需要一个包含DropDownList的页面,其中包含一些项目,例如“ firstName”,“ lastName”,“ Age”等。当用户选择每个项目时,我想向该页面添加可选控件。 For example when user select the 'Age' another dropdownlist created dynamically with these values : 'Less than 10' 例如,当用户选择“年龄”时,将使用这些值动态创建另一个下拉列表:“小于10”
'Between 10 and 30' “ 10到30之间”
'more than 30' “超过30”
Here is a button that add this user selection to listBox and let user to choice another options. 这是一个将此用户选择添加到listBox并允许用户选择其他选项的按钮。 (I made a query at last according to user choices and send it to db) (我最后根据用户选择进行了查询,并将其发送到数据库)
- What I do: - 我所做的:
I create a dropDownList and set it's AutoPostBack property to true and adds some items in it and user must select one of those item. 我创建了一个dropDownList并将其AutoPostBack属性设置为true,并在其中添加了一些项目,用户必须选择其中一项。 then I add user SelectedValue of dropDownList in a Cache variable before page post back happens: 然后在回发页面之前,在Cache变量中添加dropDownList的用户SelectedValue:

protected void DropDownListColumnNameSelectedIndexChanged(object sender, EventArgs e)
    {
        Cache["SelectedKey"] = dropDownListColumnName.SelectedValue;
    }

When user select an item from dropDownList *DropDownList_SelectedIndexChanged* fire, and I must create controls dynamically in a place holder: 当用户从dropDownList * DropDownList_SelectedIndexChanged *中选择一项时,我必须在占位符中动态创建控件:

 var textBoxName = new TextBox
    {
        ID = "textBoxName",
        CssClass = "str-search-textbox-highlight",
        ViewStateMode = ViewStateMode.Disabled
    };
 placeHolderFirstItem.Controls.Add(textBoxName);

- What is the problem? - 问题是什么? When I try add new control in current Button_Click event, control added successfully to page but I can't find it by placeHolderFirstItem.Controls.Find("textBoxName") actually placeHolderFirstItem.Controls.Count is always zero. 当我尝试在当前Button_Click事件中添加新的控制,控制成功添加到页面,但我不能placeHolderFirstItem.Controls.Find(“textBoxName”)发现它实际上placeHolderFirstItem.Controls.Count始终为零。 So I can't get textBoxName.Text values. 所以我无法获取textBoxName.Text值。
I try to google that for any solution and I found some solution that I must add controls in Page.OnInit so I add controls in overridden OnInit(e): 我尝试用谷歌搜索任何解决方案,但发现一些解决方案,必须在Page.OnInit中添加控件,因此我要在覆盖的OnInit(e)中添加控件:

protected override void OnInit(EventArgs e)
        {
            if (!Page.IsPostBack) return;
            var textBoxName = new TextBox
               {
                   ID = "textBoxName",
                   CssClass = "str-search-textbox-highlight",
                   ViewStateMode = ViewStateMode.Disabled
               };
            placeHolderFirstItem.Controls.Add(textBoxName);
        }

after doing this I can find "textBoxName" in placeHolderFirstItem, but it fire before DropDownList_SelectedIndexChanged ! 完成此操作后,我可以在placeHolderFirstItem中找到“ textBoxName”,但它会在DropDownList_SelectedIndexChanged之前触发!
so how can I add new controls to place holder exactly when user change the dropDownList value and how can I read new controls value? 因此,当用户更改dropDownList值时,如何将新控件准确地添加到占位符中?如何读取新控件的值?
Thanks in advance, 提前致谢,
Mohsen. 莫森

- Updated: - 更新:
Here is the better solution 这是更好的解决方案
( http://forums.asp.net/p/1959726/5596531.aspx?p=True&t=635244790943067485&pagenum=1 ) http://forums.asp.net/p/1959726/5596531.aspx?p=True&t=635244790943067485&pagenum=1

When you are dynamically adding controls, you have to reload the controls into the control tree everytime thereafter for it to appear. 在动态添加控件时,此后每次都必须将控件重新加载到控件树中才能显示。 With the help of viewstate, you could change your code sample to have: 借助viewstate,您可以将代码示例更改为:

ViewState("ShowTextbox") = true

And then in your init routine: 然后在您的初始化例程中:

protected override void OnInit(EventArgs e)
    {
        if (!Page.IsPostBack) return;

        if (ViewState("ShowTextBox") == true) {
           var textBoxName = new TextBox
           {
               ID = "textBoxName",
               CssClass = "str-search-textbox-highlight",
               ViewStateMode = ViewStateMode.Disabled
           };
           placeHolderFirstItem.Controls.Add(textBoxName);
        }
    }

Please note it's much easier to have a control on the control tree, and then show/hide by setting Visible to true/false, because of these ASP.NET control tree issues. 请注意,由于这些ASP.NET控件树问题,在控件树上具有控件然后通过将Visible设置为true / false来显示/隐藏要容易得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM