tags:

views:

112

answers:

4

So I am experiencing an issue with an .aspx page and some server side code, where I am getting unexpected results.

The goal of this page is simple, there are 5 radio buttons and a button with a server side onclick function. The idea is the user picks 1 of the 5 radio buttons, and then clicks the button. Upon clicking the button I verify (not using form validation, because I wanted a different feel) that a button is checked, and then store the selected option in a database.

Due to the fact that the number of radio buttons may change in the future I decided to try and abstract the number of radio buttons to make it easier on my self to change in the future.

So at the top of my server side code I created a list of possible options.

I then have a registerVote function that takes in a RadioButton object, and a number to grab a setting from the config file. I throw those 2 values into a wrapper class, and then add them to the list of possible options.

Finally when the submit button is pressed, I iterate through all possible options to see which one is checked, and grab its associated value.

public partial class VotePanel : System.Web.UI.Page
{
    List<VoteOption> voteOptions = new List<VoteOption>();

    public string registerVote(RadioButton newRadioButton, int voteOption)
    {
        voteOptions.Add(new VoteOption(newRadioButton, voteOption));
        return ConfigurationManager.AppSettings["vote_option_" + voteOption];
    }

    protected void Submit_Click(object sender, EventArgs e)
    {
        //Check vote
        string vote_value = "";
        bool someButtonChecked = false;

        foreach (VoteOption vo in voteOptions)
        {
            if (!someButtonChecked && vo.button.Checked)
            {
                vote_value = vo.movie;
                someButtonChecked = true;
            }
        }
        //....
    }
}

class VoteOption
{
    public RadioButton button;
    public int vote_value;

    public VoteOption(RadioButton r, int v)
    {
        button = r;
        vote_value= v;
    }
}

The code I use in page to add a radio button looks like this

<asp:RadioButton ID="RadioButton1" runat="server" GroupName="Vote" style="position: relative; top: 3px;" /><%=registerMovie(RadioButton1,1)%>

Now for the problem I am experiencing. Whenever the submit button is clicked, the list has a count of zero, and looks like it has been reinitialized. I validated that values are being added, by returning the list count in the registerVote method, and objects are indeed being added, but for some reason are not available to the Submit function.

Now variables on a page like this shouldn't reinitialize right? I also tested a string, and it did not reset and was available to the Submit button. What I did was define a class variable string time = DateTime.Now.Ticks.toString(); and displayed that after the submit button was clicked, and the time was always the same reguardless of how many times I clicked it.

So why would my List reinitialize, but not a string? Any ideas?

A: 

I don't see in your code any place where the list that you are building is placed in memory. I believe you are rebuilding it on each page reload. P.s. might be my reading but you created a function called registerVote and you are calling a method called registerMovie so that might be your problem.

You could place the list in the session and get it back from session.

Personnally I would change the code to 1) Check if the list is in memory and get it. If not in memory call a method to generate it once and then place it in memory. 2) Use a RadioButtonList on your page that you can then bind to your list as a data source.

Philippe Asselin
+1  A: 

The problem seems to be that you are constructing the List<VoteOption> voteOptions at page render then expecting it to still be there on postback. The Page object does not exist past the point that the page is delivered to the browser, so your list of vote options gets disposed of as well when the browser has received the page.

You'll either need to reconstruct the voteOption list before or during Submit_Click on postback, or give yourself enough information in the value of the radio button that you don't need it.

Lazarus
A: 

asp.net is stateless, so every postback (such as clicking Submit) recreates the server-side class. If you want your list to persist between calls, you should save it in ViewState or a Hidden field. Not sure about the string though; what you're describing doesn't fit the asp.net lifecycle.

Peter MacMurchy
+2  A: 

Keep in mind that your page class will be constructed and destructed for every request - no state will be maintained between each page load, it is up to you to properly recreate state as needed. In this case it appears that your list voteOptions is not being recreated before Submit_Click is called.

You'll have to register all your voting options regardless of whether the page is in a postback or not inside the Page_Load or OnInit handlers of the page. This will reconstruct voteOptions, which will then be accessed when Submit_Click is called.

Take a look at the ASP.NET Page Life Cycle.

jscharf