如何从asp.net c#中动态生成的控件中获取值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18733477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:03:35  来源:igfitidea点击:

How to get values from dynamically generated controls in asp.net c#?

c#asp.net

提问by vpv

I know this is a well asked question and I found some marked as answers but those doesn't solve my problem. Please have a look at my codes..

我知道这是一个很好的问题,我发现了一些标记为答案的问题,但这些并不能解决我的问题。请看看我的代码..

Method to Display dynamic controls

显示动态控件的方法

private void ShowControlsByFormId()
            {
                List<FormControlsBO> list = new List<FormControlsBO>();

                list = new FormControlsDA().FormControls_GetByFormId(Convert.ToInt32(ddlForm.SelectedValue.ToString()));

                if (list.Count > 0)
                {
                    for (int i = 0; i < list.Count; i++)
                    {
                        DynamicControl dynamicControl = CommonUtility.GenerateControl(list[i]);
                        pnlInput.Controls.Add(new LiteralControl("<tr><td>"));
                        pnlInput.Controls.Add(dynamicControl.GeneratedControlLiteral);
                        pnlInput.Controls.Add(new LiteralControl("</td><td></td><td>"));
                        pnlInput.Controls.Add(dynamicControl.GeneratedControl);
                        pnlInput.Controls.Add(new LiteralControl("</td><tr><br/><br/>"));
                    }
                    pnlAction.Visible = true;
                }
                else
                {
                    pnlAction.Visible = false;
                }
            }

Method to Generate dynamic controls

生成动态控件的方法

public static DynamicControl GenerateControl(FormControlsBO bo)
        {
            DynamicControl dynamicControl = new DynamicControl();
            Control control = new Control();

            LiteralControl literal = new LiteralControl();

            switch (bo.FieldType)
            {
                case "TextBox":
                    control = new TextBox();
                    control.ID = bo.FieldName;
                    literal.Text = bo.FieldLabel;
                    break;
                case "RadioButton":
                    control = new RadioButton();
                    control.ID = bo.FieldName;
                    literal.Text = bo.FieldLabel;
                    break;
                case "CheckBox":
                    control = new CheckBox();
                    control.ID = bo.FieldName;
                    literal.Text = bo.FieldLabel;
                    break;
                case "DropDownList":
                    control = new DropDownList();
                    control.ID = bo.FieldName;
                    literal.Text = bo.FieldLabel;
                    break;
            }

            control.ClientIDMode = ClientIDMode.Static;
            dynamicControl.GeneratedControl = control;
            dynamicControl.GeneratedControlLiteral = literal;

            return dynamicControl;
        }

Method to Save data

保存数据的方法

    private void FormRecords_Save()
            {
                List<FormControlsBO> list = new List<FormControlsBO>();
                FormControlsBO bo = new FormControlsBO();

                foreach (Control ctl in pnlInput.Controls)
                {
                    CommonUtility.DataFiller(bo, ctl);
                    list.Add(bo);
                }

                Boolean result = false;
                result = new FormControlsDA().FormRecords_Save(list);

                if(result == true)
                {
                    lblMessage.Text = "Form data saved successfully";
                }
                else
                {
                    lblMessage.Text = "Form data not saved";
                }
            }

The problem is, when I debug the code, pnlInput.Controlsshows Zero count. Please help !!

问题是,当我调试代码时,pnlInput.Controls显示零计数。请帮忙 !!

采纳答案by Johnny_D

As I already answered hereall dynamic controls should be reinitiated in Page's Initevent, as viewstate manager values are set every request. You can check page's lifecycle. So right now, when you do not create you control in init event, viewstates misses them while it is setting postback data, and when you do try to get values, they are equal to zeros. Keep in mind, that you have to create the same control types with the same names.

正如我已经在这里回答的那样所有动态控件都应该在 Page 的Init事件中重新启动,因为每个请求都设置了视图状态管理器的值。您可以检查页面的生命周期。所以现在,当你没有在 init 事件中创建你的控件时,视图状态在设置回发数据时会错过它们,当你尝试获取值时,它们等于零。请记住,您必须创建具有相同名称的相同控件类型。

回答by Subin Jacob

If you have written the code to generate Dynamic Controls and if it is in the page load event use FindControl("IdofControl");to retrive its value;

如果您已经编写了生成动态控件的代码,并且如果它在页面加载事件中使用FindControl("IdofControl");来检索其值;

For Example,

例如,

TextBox txtInstance = (TextBox)FindControl("IdofControl");
string txtvalueinTextBox =txtInstance.Text;

Make sure that the controls are dynamically generated in all page reloads.If the controls generated on the postback is different the viewState may not restore back properly.

确保在所有页面重新加载时动态生成控件。如果回发时生成的控件不同,则 viewState 可能无法正确恢复。