Wednesday, March 13, 2013

Postback Issue and retrieving values - asp.net dynamic controls


In my last post “CREATINGCONTROLS DYNAMICALLY IN ASP.NET” I have explained about creating controls dynamically in asp.net.
Postback Issue Solving :
By creating controls dynamically for each and every post back the controls will get cleared.  This is due to the lack of initialization of these controls.  If we initialize an asp.net server control in aspx page then there is no need of initializing this control every time, objects will be initiated while parsing the aspx page itself.  By in case of dynamically created controls there will not be any initialization in postbacks so the solution for the controls to stay at postback is – Creating the controls on page load and repeating the creation process in each and every postback.  Now after a change my cs file’s code will look like as follows
    protected void Page_Load(object sender, EventArgs e)
    {
        if (hdfTemp.Value == "1") { CreateForm(); }
    }
    protected void btnGenerateForm_Click(object sender, EventArgs e)
    {
        if (hdfTemp.Value == "0") { CreateForm(); hdfTemp.Value = "1"; }
    }
    public void CreateForm()
    {
        try
        {
            //Create Table
            Table tbl = new Table();
            tbl.ID = "tbl";
            //Creating Table row
            TableRow tr = new TableRow();
            tr.ID = "tr1";
            tbl.Rows.Add(tr);//adding table row to the table
            //Creating table cell
            TableCell tc1 = new TableCell();
            TableCell tc2 = new TableCell();
            //Adding table cells to table rows
            tr.Cells.Add(tc1);
            tr.Cells.Add(tc2);
            //Creating Label
            Label lbl = new Label();
            lbl.Text = "First Name";
            lbl.ID = "lblFirstName";
            //Creating TextBox
            TextBox txt = new TextBox();
            txt.ID = "txtFirstName";
     //Assigning the label and textbox controls as child controls for table cells
            tc1.Controls.Add(lbl);
            tc2.Controls.Add(txt);
            phDCtrls.Controls.Add(tbl);
            tblForm.Visible = true;
        }
        catch (Exception ex) { Response.Write(ex.Message); }

    }
Here in the above code I have use a hidden field for avoiding the generation of dynamic controls on first time page loads, because in this scenario only after clicking on the Generate Controls button the controls should get generated.  If you want controls to be generated on page load then no need of using hidden field.

Retrieving values from dynamically generated controls

Controls can be found on the page with their ids.  While creating controls we have specified a unique id for each control.  With this ids we can find the control on the page using “Findcontrol()” method.  Now let’s get the form values in our example on clicking the save button.

  • We have created an id for textbox as “txtFirstName”.
  • Create a textbox object with name txtFirstName.
  • Equal this textbox object with the result of findcontrol method.  Now you got your textbox on this object.
  • Now the textbox’s text property will gives the value of the textbox.

The sample code on save button click event handler will be as follows.
try
{
    TextBox txt = (TextBox)this.Page.FindControl("txtFirstName");
    ScriptManager.RegisterClientScriptBlock(Page, GetType(), "alert", "alert('" + txt.Text.Trim() + "');", true);
}
catch (Exception ex) { Response.Write(ex.Message); }


If any issues or queries, kindly let me know


Happy coding…