Check viewState before save data

Last post 11-02-2008 2:28 AM by talg. 3 replies.

Sort Posts:

  • Check viewState before save data

    10-31-2008, 12:15 PM
    • Member
      79 point Member
    • ASPCloud
    • Member since 05-18-2007, 5:32 AM
    • Posts 112

    Hi All,

    I have been googling around but cannot find the exact solution, hope somebody here can point me to the right direction, thanks in advance.

     

    Problem Scenario:

    Do not try to save webform data into database if it has not been changed, need to achieve this by using viewstate.

     

    Example:

    A textbox hold the name of a customer, and user enter their name then click on submit and data is save into the database.

    While the code works well on just small number of fields, I have multiple webforms each contains more than 50 fields (mostly textboxes). So I need to check to see if data has actually been change for all the fields in the webform, I know ViewState is storing the state of each control on the webform, so when user click a button causes a postback, data is reload back to the fields. My key question is how to use this last viewstate data to compare currect control data to see if data has been changed? All this is for performance issue, so the website to not hammer the SQL Server database when it is not neccessary.

    I was looking for something like:

    if(ViewState["txtName"].ToString() == txtName.Text) {

        //no change, do not save

    }

    else {

        //data changed, then save

    }

    But ViewState["txtName"] is actually null, looks as I need to manually assign a value into it at the first place. But Im sure ViewState load the data back into txtName when postback happens. Any ideas?

    Thanks.

     

  • Re: Check viewState before save data

    10-31-2008, 3:01 PM
    • All-Star
      27,996 point All-Star
    • bmains
    • Member since 10-22-2004, 12:20 PM
    • Posts 5,646
    • TrustedFriends-MVPs

    Hey, do:

    if (ViewState["txtName"] != null && ViewState["txtName"].ToString() == txtName.Text) {

        //no change, do not save

     

    Brian

    "Trust in the Lord and do what is good; dwell in the land and live securely. Take delight in the Lord, and He will give you your heart's desires" (Psalm 37: 3-4).
  • Re: Check viewState before save data

    10-31-2008, 11:36 PM
    • Contributor
      4,514 point Contributor
    • deesh1531982
    • Member since 12-24-2007, 1:15 PM
    • Posts 769

    ASPCloud:
    ViewState["txtName"].ToString

     where you are assigning these values? and where you are checking whether it has been modified?
    "Never underestimate the power of stupid people in large groups"
  • Re: Check viewState before save data

    11-02-2008, 2:28 AM
    • Member
      144 point Member
    • talg
    • Member since 08-22-2007, 7:49 AM
    • israel,tel aviv
    • Posts 73

    Hi,

    I would like to mention that there is an Event TextBoxChanged which is a built in event in the textBox control which is arised exactly on the best place

    and implement a better view state performence that you will,

    You should/could write ageneric method to attach the event:

    protected void GetFildes(object sender, EventArgs e)

    {

    }

    Sender is the Control who will attach the method to the event handler ,

    (let me know if something is not understood)

    Regards -tal

    Tal Gaviser
    DB & GUI Developer
    Israel,tel aviv
Page 1 of 1 (4 items)