Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

ASP.net Lifecylce & Persisting the Viewstate

Options
  • 08-10-2008 9:44am
    #1
    Registered Users Posts: 2,791 ✭✭✭


    Hi,

    I have a button click event handler in a user control which sets a Viewstate property when invoked.

    In the OnLoad event, I check this viewstate property and perform some actions based that condition. My problem is that the Viewstate is null when this condition is checked. From what I've read, the viewstate should be loaded before the OnLoad event.

    I've only been able to access the correct values in the OnPreRender event but this is called twice for some reason. The viewstate is correctly populated the first time round, but empty the second time it's called.

    Would appreciate any advice. Thanks in advance


Comments

  • Registered Users Posts: 11,980 ✭✭✭✭Giblet


    Do you have a redirect? Viewstate will be lost unless it is a postback.


  • Registered Users Posts: 2,791 ✭✭✭John_Mc


    Do you have a redirect? Viewstate will be lost unless it is a postback.

    No, no redirect.

    The Event handler is hooked up to the .net membership provider Change password control. I think this is causing the second postback and clearing the viewstate.

    Just going to have to use Session Vars I guess.


  • Registered Users Posts: 11,980 ✭✭✭✭Giblet


    Set a watch on the IsPostBack property. You shouldn't lose the view state on a second postback.


  • Registered Users Posts: 2,791 ✭✭✭John_Mc


    Set a watch on the IsPostBack property. You shouldn't lose the view state on a second postback.

    The first time the OnPreRender loads after the button has been clicked, IsPostBack is true and the viewstate contains the correct information.

    The second time it runs after the button is clicked, IsPostBack is false and the viewstate is empty :confused:


  • Registered Users Posts: 11,980 ✭✭✭✭Giblet


    Are you using Firefox and have you got firebug? Use the Net console to see if there are any Gets after you post the form. You might be getting a Post followed by a Get.

    Also, try this in IE if you haven't, as there is a number of problems that cause double postbacks in Firefox


  • Advertisement
  • Registered Users Posts: 11,980 ✭✭✭✭Giblet


    Do you have a <Asp:Image> on your page? If you don't set the ImageUrl to something, it can cause this problem in Firefox.


  • Registered Users Posts: 2,791 ✭✭✭John_Mc


    I saw that problem with the empty image src tag but there are none on the page or other controls used on that page.

    This problem occurs in IE and FF.


  • Registered Users Posts: 11,980 ✭✭✭✭Giblet


    Can you post any code samples?


  • Registered Users Posts: 2,791 ✭✭✭John_Mc


    Can you post any code samples?

    I'm not really sure what I can post short of pasting the entire controls sourcecode in.

    The event handler for setting the viewstate values is correctly setting the value. (This is triggered by the ASP.net Membership ChangePassword control ChangedPassword event)

    The OnPreRender logic is correct, and works the first time but is immediately overwritten by a Non-Postback page reload of some sort.

    I can get around this using Session vars and a conditional check against IsPostback but it's a messy solution.

    Thanks for you help by the way


  • Registered Users Posts: 11,980 ✭✭✭✭Giblet


    I'd really see what is causing that additional Get request; step through each control as see when IsPostBack is set to false. Update panels can cause this behaviour as well. Do you have autowireup set to true along with an onclick property on the button?


  • Advertisement
Advertisement