Storing radio button values after postback (C#)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Gotejjeken
    New Member
    • Sep 2007
    • 27

    Storing radio button values after postback (C#)

    I have a form with four radio buttons and I want to store the value of the selected radio button (it's a poll and need to capture and record the user's response). I'm dynamically generating everything, so I'm a bit confused as to how to know which radio button was selected and what it's value is.

    Is dynamically generating the poll form the easiest way, then on postback (provided the user passed a db check that says they already answered the poll) draw the actual poll?
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Originally posted by Gotejjeken
    I have a form with four radio buttons and I want to store the value of the selected radio button (it's a poll and need to capture and record the user's response). I'm dynamically generating everything, so I'm a bit confused as to how to know which radio button was selected and what it's value is.

    Is dynamically generating the poll form the easiest way, then on postback (provided the user passed a db check that says they already answered the poll) draw the actual poll?
    Are you using a RadioButtonList?
    You can use theSelected Index, SelectedItem, and SelectedValue properties to determine which radio button has been selected.

    Comment

    • joedeene
      Contributor
      • Jul 2008
      • 579

      #3
      if you are just using 4 different radio buttons, have an IF ELSE statement, to see which radio button is checked, using the .checked() function of a radiobutton? and store it after you know which one is checked by either the .name() or .text() methods ?

      joedeene

      Comment

      • Gotejjeken
        New Member
        • Sep 2007
        • 27

        #4
        I'll have to look into the RadioButtonList . The problem with looping through my radio buttons is that they are dynamically generated, so when the page posts back they are gone. I also have a dynamically generated submit button, so when I post back that's gone also.

        I can successfully get the button back by re-creating it and setting an event handler on it. I tried storing the actual radio buttons in the viewstate and pulling them back out on postback, but it's giving me an error that they aren't marked as serializable.

        The reason I'm creating everything dynamically is so that the form is only drawn if the user hasn't answered the poll, if they have the actual poll is going to be drawn.

        Is there some way I can serialize the radio buttons and put them in the viewstate, then on postback pull them out and loop through them to see which one was checked?

        EDIT: I haven't really messed around with it, but would it be easier to use a MultiView for what I'm trying to do?

        Comment

        • joedeene
          Contributor
          • Jul 2008
          • 579

          #5
          Originally posted by Gotejjeken
          I'll have to look into the RadioButtonList . The problem with looping through my radio buttons is that they are dynamically generated, so when the page posts back they are gone. I also have a dynamically generated submit button, so when I post back that's gone also.

          I can successfully get the button back by re-creating it and setting an event handler on it. I tried storing the actual radio buttons in the viewstate and pulling them back out on postback, but it's giving me an error that they aren't marked as serializable.

          The reason I'm creating everything dynamically is so that the form is only drawn if the user hasn't answered the poll, if they have the actual poll is going to be drawn.

          Is there some way I can serialize the radio buttons and put them in the viewstate, then on postback pull them out and loop through them to see which one was checked?

          EDIT: I haven't really messed around with it, but would it be easier to use a MultiView for what I'm trying to do?
          well, how about creating panels, and when you want to show the polls, then use the .visible() property of the panels... im not sure if asp.net has it though, but i think so. but that way it will still be there, just not visible to them...

          joedeene

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            Originally posted by Gotejjeken
            I'll have to look into the RadioButtonList . The problem with looping through my radio buttons is that they are dynamically generated, so when the page posts back they are gone. I also have a dynamically generated submit button, so when I post back that's gone also.

            I can successfully get the button back by re-creating it and setting an event handler on it. I tried storing the actual radio buttons in the viewstate and pulling them back out on postback, but it's giving me an error that they aren't marked as serializable.

            The reason I'm creating everything dynamically is so that the form is only drawn if the user hasn't answered the poll, if they have the actual poll is going to be drawn.

            Is there some way I can serialize the radio buttons and put them in the viewstate, then on postback pull them out and loop through them to see which one was checked?

            EDIT: I haven't really messed around with it, but would it be easier to use a MultiView for what I'm trying to do?

            It sounds like you aren't properly creating your dynamic controls.
            See if this article on how to use dynamic controls in ASP.NET helps you at all...

            You shouldn't lose the value of your controls if you follow that article.

            Comment

            Working...