I have an asp.net web forms application. In this application, students upload assignments and submit a survey along with it. For the survey, I read values from DB and dynamically generate ASP.NET Controls like radiobuttonlist, checkboxlist, textbox on the form during gridview's RowDataBound Event. I cannot do it in Page_Init method because I create only if a user click on a specific assignment which requires a survey to be submitted.
When I submit the survey form, the survey values are not saved into database. During debugging I figured it out that the values are not available in the codebehind page. The radiobuttonlist.selectedvalue returns “” and I get following error:
String conversion from “” to integer not valid.
After a postback, the form values are still there and I don’t know why they are not accessible in the codebehind. And I am confused because viewstate is enabled on all controls.
Dim lstContextArtifacts As New List(Of ContextAttributesArtifacts)
Dim lstContextAttributesOpen As New List(Of ContextAttributesArtifactsOpenEnded)
For Each gvrow As GridViewRow In gridview_ContextAttributes.Rows
If gvrow.RowType = DataControlRowType.DataRow Then
Dim contextAttribute As New ContextAttributesArtifacts
Dim contextAttributeOpen As New ContextAttributesArtifactsOpenEnded
Select Case gvrow.Cells(1).Controls(1).ID.ToString()
Case "rdblist"
Dim _radiobtnlist As RadioButtonList = DirectCast(gvrow.Cells(1).FindControl("rdblist"), RadioButtonList)
contextAttribute.ContextAttributeOptionID = _radiobtnlist.SelectedValue
contextAttribute.ContextAttributeID = CType(gridview_ContextAttributes.DataKeys(gvrow.DataItemIndex).Value, Integer)
contextAttribute.ArtifactID = varArtifactID
lstContextArtifacts.Add(contextAttribute)
End Select
End If
Next
I receive error on the following line:
contextAttribute.ContextAttributeOptionID = _radiobtnlist.SelectedValue
I am able to cast radiobuttonlist from gridview but values are inaccessible even though they still persist after postback.
Thanks in advance for the help.
UPDATE1: I referred to these articles which kind of relate to my problems http://www.4guysfromrolla.com/articles/092904-1.aspx
http://msdn.microsoft.com/en-us/library/Aa479007
http://www.codeproject.com/KB/aspnet/dynamiccontrolsByLeon.aspx as suggested by @coding gorilla in the answer below.
However, I generate those controls when a user click on the assignment, then it checks whether it requires a survey or not. If it does then I generate controls in grid view rowdatabound event which allows me to check which controls I need to load. I do not load them in page load event which above articles refer. Or as articles mention how do I load them on each page request because it depends on user behavior. Sorry but please correct me if I misunderstood anything.
I think the values a user selects are not getting saved in the viewstate.
UPDATE2: I am giving the code that generates on gridviewdatabound event. And I am even databinding my gridview on postback to regenerate the dynamic controls. And they seems to preserve control selection but are inaccessible in codebehind.
If e.Row.DataItemIndex > -1 Then
Dim label1 As Label = DirectCast(e.Row.Cells(0).FindControl("lbl_ca"), Label)
' 'MsgBox(label1.Text)
Dim FormElement As Label = DirectCast(e.Row.Cells(0).FindControl("lbl_FormElement"), Label)
Select Case FormElement.Text
Case "radio"
''Add Custom Logic Here
Dim rdblist As New RadioButtonList
rdblist.ID = "rdblist"
rdblist.EnableViewState = True
rdblist = myRadioButtonList(gridview_ca.DataKeys(e.Row.DataItemIndex).Value)
Dim v1 As New RequiredFieldValidator
v1.ControlToValidate = "rdblist"
v1.ValidationGroup = "valSurvey"
v1.Text = "*"
v1.ForeColor = Drawing.Color.Red
v1.SetFocusOnError = True
v1.Display = ValidatorDisplay.Dynamic
Dim t1 As String = Now.ToLongTimeString.ToString.Replace(":", "")
t1 = t1.Replace("/", "")
t1 = t1.Remove(t1.Length - 3, 3)
t1 = t1.Replace(" ", "")
v1.ID = "val" + t1
e.Row.Cells(1).Controls.Add(v1)
e.Row.Cells(1).Controls.Add(rdblist)
End Select
End If
UPDATE 3: I ended up using an alternative way to grab values from the controls. The controls values persist when I try to check their values on onserver validate event. So Inserted a subroutine to grab values and store them temporarily and retrieve them later.
Although I feel this article series was very useful in understanding dynamically added controls.