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

时间:2013-09-11 05:43:12

标签: c# asp.net

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

显示动态控件的方法

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;
                }
            }

生成动态控件的方法

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;
        }

保存数据的方法

    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";
                }
            }

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

2 个答案:

答案 0 :(得分:8)

正如我已经回答here所有动态控件应该在Page的Init事件中重新启动,因为每个请求都设置了viewstate manager值。您可以查看页面lifecycle。  所以现在,当你没有在init事件中创建控件时,viewstates在设置回发数据时会错过它们,当你尝试获取值时,它们等于零。  请记住,您必须使用相同的名称创建相同的控件类型。

答案 1 :(得分:5)

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

例如,

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

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