[RESOLVED] Persisting Values in a Web Form

I have a web form where total costs for four regions and its proportion across various regions is entered manually and then I use some calculations to generate a monthly grid showing allocations by month. The proportion and calculations could be different for each of the regions. 

The rules and formula are stored in a back-end table and read and applied to total. I read rules store it in a public array.

Given the amount of data, I do not want to use viewState or Session. 

But later, when I try to read the array, it has all zeros. The data is not saved. What am I doing wrong?

Thanks in advance.

public string[] rules = new string[21];

protected void Page_Load(object sender, EventArgs e)
        {
                if (!IsPostBack)
                {
                    applyRules(); 
                }
             }
        public void applyRules()
        {
// read db and find rule. this works.
             }

        protected void tbReg_TextChanged(Object sender, EventArgs e)
        {
            onRegChanged();
        }

        public void onRegChanged()
        {
// this code is fired from several onChange events. if (rules[9] == "Yes") { // do something This is where rules has nothing in it. } }

tinausa

when I try to read the array, it has all zeros.

You will not be able to retain those values on page postback unless until it is saved somewhere. If you want to retain those across page postbacks, you would have to save it either in Session/Viewstate or hidden fields.

tinausa

Given the amount of data, I do not want to use viewState or Session.

You have initialized the string array with just 21 elements. As long as you are not storing datatables/datasets in session/viewstate, you would not notice any effect on the performance or page rendering time.

Create a public property that handles value from Session and use it across page postbacks.

If you want to use onchange events you have to use the viewstate, that is how .net knows the value has changed.  As already said, unless we're talking about a LOT of data I doubt you'll notice much difference.  If you are planning on keeping your array between postbacks you're going to have to store that somewhere to.  Things only exist while the page is rendering, after that everything you create is destroyed.  That is why we use viewstate/session etc.

Thanks, that's what I feared. I guess I will use viewstate.

Thanks I willuse viewstate.

1 thoughts on "[RESOLVED] Persisting Values in a Web Form"

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>