Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6128235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:36:14+00:00 2026-05-23T16:36:14+00:00

I have an asp.net web forms application. In this application, students upload assignments and

  • 0

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
https://web.archive.org/web/20210330142645/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.

http://weblogs.asp.net/infinitiesloop/archive/2006/08/30/TRULY-Understanding-Dynamic-Controls-_2800_Part-3_2900_.aspx

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:36:15+00:00Added an answer on May 23, 2026 at 4:36 pm

    This can be a very complex issue, but the basics of the issue are this:

    What you dynamically generate asp.net controls, after a postback, you have to re-generate those controls before attempting to use those controls. The problem is that your code-behind only contains controls that are directly created by parsing the .aspx file.

    Keep in mind that every time a page request is generated, including a post-back, the class that is represented by your .aspx.cs file is re-created (i.e. newed up). So if on the initial rendering you create your checkboxlist control, and store it in an instance field. After the page is rendered, that particular class is destroyed and when the post-back comes in it’s recreated.

    So for the postback, you have to recreate those controls exactly the same way as when you originally created them, and this has to happen before the post back data is loaded. If not, then the data is available as a form field in the HTTP request, but ASP.NET doesn’t know what to do with it.

    There are a lot of postings on how to do this kind of thing if you just search “dynamically created asp.net controls”. A quick search brought me to this one: http://www.codeproject.com/KB/aspnet/dynamiccontrolsByLeon.aspx

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this existing environment: 1) ASP.NET 3.5 web application 2) forms authentication with
I have an ASP .NET web application which uses Forms Authentication. Let's call this
I have a ASP.NET Web Forms application and I'm using some dynamic controls in
I have an ASP.NET web application that is using forms authentication. Everything is configured
I have a ASP.NET Web Forms application that internally makes many SOAP and REST
I have a hybrid asp.net web forms / mvc application that I recently converted
I have installed an ASP.NET 4.0 Web forms application in IIS 7.5. If I
Here's what i have : ASP.NET 4.0 Web Application Forms Authentication (Cookie Based) Here's
I have developed a web application (ASP.NET Web Forms). One of my customer has
We have been using log4net for logging our asp.net web forms application. Our logging

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.