Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

asp.net

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.Controls shows Zero count. Please help !!

like image 429
vpv Avatar asked Sep 11 '13 05:09

vpv


2 Answers

As I already answered here all dynamic controls should be reinitiated in Page's Init event, 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.

like image 69
Johnny_D Avatar answered Sep 28 '22 16:09

Johnny_D


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;

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.

like image 33
Subin Jacob Avatar answered Sep 28 '22 16:09

Subin Jacob