Archive

Archive for January, 2010

Retain the value of a dynamic control on post back

January 25th, 2010 14 comments

I heard this question several time that how you can retain the value of dynamically created controls on postbacks. You might see this before that when you create dynamic controls either in GridView or page. When you do postback, you cannot find the controls in page or GridView.

If you want your dynamic controls to work you can create them on page_load or page_init methods. But in some cases you have to create controls on some control event other than page_laod or page_init. In these cases you can follow my blog to retain the values of dynamic controls on postbacks.

In my case I have a button named btnSetParameters. On click event I’m creating controls in a GridView column. I have an ASP .Net panel in that column. My code is creating dynamic controls and then adding them to panel. But on postback I couldn’t found these controls in GridViewRow by their IDs. I need their values on postback.

For this I have created an ArrayList to keep controls.

Private objControlsList As New ArrayList

Then on RowDataBound where I was creating dynamic controls I added some code to add these controls in ArrayList. Here is an example of adding a TextBox in ArrayList. ASP Panel in GridViewRow name is pnlControls.

Dim txtValue As New TextBox
txtValue.ID = “txtValue1″
CType(e.Row.FindControl(“pnlControls”), Panel).Controls.Add(txtValue)
objControlsList.Add(txtValue)

Do the same for all the dynamic controls and add them on ArrayList. You can even put custom controls in this ArrayList. After adding all the controls, put this ArrayList in Session.

Session.Add(“ControlsList”, objControlsList)

Now on page postback, use the controls from the ArrayList. Use the UniqueID to pick the values from the ViewState for orginal controls.

Dim txtSaved As TextBox = CType(objControlsTempList(counter), TextBox)
txtValue.Text = Request.Form(txtSaved.UniqueID).ToString()
counter += 1

For dynamic DropDownlist control you can use this piece of code.

Dim ddlSaved As DropDownList = CType(objControlsTempList(counter), DropDownList)
ddlSaved.SelectedValue = Request.Form(ddlSaved.UniqueID).ToString()
counter += 1

For radio buttons you can use this piece of code. You must define a group name while creating the radio buttons to use this code.

Dim rdSaved As RadioButton = CType(objControlsTempList(counter), RadioButton)
Dim radioName As String = (rdSaved.ClientID).Remove(rdSaved.ClientID.LastIndexOf(“_”)).Replace(“_”, “$”) + “$” + rdSaved.GroupName
If Request.Form(radioName) <> Nothing Then
        selectedValue = Request.Form(radioName).ToString()
End If

Now you can match this selectedValue with radio buttons in the Panels, to find the selected radio button.

For CheckBoxe controls you can use this code.

Dim chSaved As CheckBox = CType(objControlsTempList(counter), CheckBox)
If Request.Form(chSaved.UniqueID) <> Nothing Then
        chSaved.Checked = True
Else
        chSaved.Checked = False
End If

Finally, you can use this code for dynamic custom control. It’s a bit tricky to find the value. I know the structure of my custom user control. It is a custom date control. It is actually creating two DropDownList controls for day and month part of date. And it is also creating a TextBox control for Year part of the date. Here is the piece of code I use to get the selected value of dynamic custom control on postback.

Dim dtSaved As CustomDate = CType(objControlsTempList(counter), CustomDate)
Dim cntlName As String = “”
Dim selectedDate As String = “”
cntlName = (dtSaved.ClientID + “_ddlDay”).Replace(“_”, “$”)
selectedDate = Request.Form(cntlName).ToString()

cntlName = (dtSaved.ClientID + “_ddlMonth”).Replace(“_”, “$”)
selectedDate += “/” + Request.Form(cntlName).ToString()

cntlName = (dtSaved.ClientID + “_txtYear”).Replace(“_”, “$”)
selectedDate += “/” + Request.Form(cntlName).ToString()

dateCtrl.DateValue = Date.Parse(selectedDate)
counter += 1

Hope it will help someone, creating dynamic controls and retain the values on postbacks.

Cheers