简体   繁体   中英

Access text box value created dynamically when check box is checked

I am a litle confused with ASP.NET lifecycle event. I have a checkbox when it is checked, it will dynamically create labels and text boxes. This is done in checkbox oncheckchanged event.I have Ajax enabled on the checkbox without full postback.

Now in the newly created text boxes I am entering the values and when I click on the save button, in the button click event it would not even find the controls created. So how does the page viewstate remember the dynamic controls created in the checkbox events and then access it's values in the button save event? Mark up:

                  <tr> <td> <asp:CheckBox ID="chkType" runat="server" Text="Medical Procedure" OnCheckedChanged="ChkMedicalProc_Clicked"></td></tr>

                <tr><td colspan="2">
                    <asp:PlaceHolder ID="dyna" EnableViewState="true" runat="server"></asp:PlaceHolder>
                </td></tr>

Code behind in the checkedchanged event:

TableRow tr = new TableRow();
            TableCell tc1 = new TableCell();
            TableCell tc2 = new TableCell();
            Label lbl = new Label();
            lbl.Text = string.Empty;
            lbl.Text = (_queryParam[i].Param_Name + " (" + _queryParam[i].Param_Type + ") (" + _queryParam[i].Param_Length + ")").ToString();
            lbl.Style.Add("font-size", "11px");
            lbl.Style.Add("font-family", "Arial");
            _txtBox = new TextBox();
            _txtBox.ID = ctrlId;
            _txtBox.CssClass = "textEntry";
            _txtBox.Text = string.Empty;
            _txtBox.Text = _queryParam[i].Param_Value;
            tc1.Style.Add("width", "21.8%");
            tc1.Controls.Add(lbl);
            tc2.Controls.Add(_txtBox);
            tr.Cells.Add(tc1);
            tr.Cells.Add(tc2);

            _tbl.Rows.Add(tr);

            this.Master.FindControl("pagecontent1").FindControl("dyna").Controls.Add(_tbl);

Save button click event:

  for (int i = 0; i < box.Count; i++)
                                {
                                    TextBox boxValue= this.Page.Master.FindControl("pagecontent1").FindControl("dyna").FindControl("txtBoxParams-" + i) as TextBox;
                                   //I get object reference error on boxValue
}

Dynamic controls are lost on postback, so on every page request you must dynamically add them again.

Though for your example, it might be easier to always have the label / textbox on the page, but contain it in an asp:panel which you toggle the Visible property on it, or show/hide it via javascript.

As for the values of these dynamic controls during postback, if you re-create them using the same ID , asp.net will automatically re-initialize them to the proper inputted values using the viewstate information.

For more information about dealing with dynamic controls, this website seems to be fairly accurate: http://www.4guysfromrolla.com/articles/081402-1.aspx

Using the following recursive function you will be able to retrieve any control inside the dynamic table

public static Control DeepFindControl(Control c, string id) {

  if (c.ID == id) { return c; } if (c.HasControls()) { Control temp; foreach (var subcontrol in c.Controls) { temp = DeepFindControl((Control)subcontrol, id); if (temp != null) { return temp; } } } return null; } 


to receive the values of the control after finding it you should know the name of the control and then you will receive the values in another new created control with the same type... "cast the control returned from the DeepFindControl"
eg...

Control C1 = DeepFindControl(DynamicTableName, ControlNAme);
TextBox _txtBox = (TextBox)C1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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