5

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

显示动态控件的方法

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显示零计数。请帮忙 !!

4

2 回答 2

9

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

于 2013-09-11T05:56:45.833 回答
6

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

例如,

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

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

于 2013-09-11T06:11:57.397 回答