Question

I am perplexed, and maybe I am just familiar with the properties of pageLoad, IsPostBack, or IsCallback. I created a Boolean variable called "first" and set it to True. First time through PageLoad, there is a line of code if first = False, if so, write = true. Then I have a Run_write routine attached to a button, when it runs, if the user response Yes, to the initial question, I make another group of radio buttons visible and set first to false. (i ran this in debug, and I know it hits this line of code) ... so the write to sql is ignored because write == false and the window reappears with the new set of Buttons... Great!

Furthermore, I go through the PageLoad routine again, and it hits the line if (!first), set write to TRUE. my issue is first has been re-set to true? What am I missing? Note, I was able to work around this by utilizing whether the new set of buttons is checked, but I may not want to go this route, and I do want to understand what is going on.

code is below.

namespace MEAU.Web.Components.SupportCenter
{
    public partial class feedback : System.Web.UI.Page
    {
        String login;
        String myurl;
        String response;
        String s_call;
        String p_ship;
        String wrnty;
        Boolean write;
        Boolean first = true;

        protected void Page_Load(object sender, EventArgs e)
        {
            login = Sitecore.Security.Accounts.User.Current.Profile.Email;
            myurl = Request.QueryString["value"];
            s_call = "No";
            p_ship = "No";
            wrnty = "No";
            // Hide the question Buttons
            scall.Visible = false;
            parts.Visible = false;
            wrnt.Visible = false;
            lit.Visible = false;
            write = false;
            if (!first)
                write = true;
        }

        protected void Run_Write(object sender, EventArgs e)
        {
            // Get Reponse
            if (yes.Checked)
            {
                response = "Yes";
                // Display the quesiton buttons, and Hide the NO button
                scall.Visible = true;
                parts.Visible = true;
                wrnt.Visible = true;
                lit.Visible = true;
                no.Visible = false;
                first = false;

                // Did this Prevent a Service call?
                if (scall.Checked)
                {
                    s_call = "Yes";
                    write = true;
                }

                // Did this Prevent a parts shipment?
                if (parts.Checked)
                {
                    p_ship = "Yes";
                    write = true;
                }

                // Is this under warranty?
                if (wrnt.Checked)
                {
                    wrnty = "Yes";
                    write = true;
                }                    
            //   write = true;
            }
            if (no.Checked)
            {
                response = "No";
                write = true;
            }

            if (write == true)
            {
                SqlConnection conn = new SqlConnection(Sitecore.Configuration.Settings.GetConnectionString("feedback"));
                SqlCommand cmd = new SqlCommand("Insert_fb", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@login", login);
                cmd.Parameters.AddWithValue("@url", myurl);
                cmd.Parameters.AddWithValue("@response", response);
                cmd.Parameters.AddWithValue("@dateTime", DateTime.Now);
                cmd.Parameters.AddWithValue("@serviceCall", s_call);
                cmd.Parameters.AddWithValue("@partsShipment", p_ship);
                cmd.Parameters.AddWithValue("@warranty", wrnty);

                try
                {
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    Response.Write("<script type='text/javascript'>parent.$.fancybox.close();</script>");
                    Response.Write("<script type='text/javascript'>return false;</script>");    
                }
                catch (Exception ex)
                {
                    throw new Exception("Error on file update" + ex.Message);
                }
                finally
                {
                    conn.Close();
                }
            }
        }
    }
}
Was it helpful?

Solution

Every HTTP request to your site creates a new instance of your page class.
Instance state is not preserved.

Instead, you need to store the state in session or ViewState, depending on what you want to apply to.

OTHER TIPS

Page_Load will be called every time your server is requested for the page. This includes post-backs.

You can check IsPostBack to see if the current Page_Load execution is for the first display of the page, or a subsequent post-back.

protected void Page_Load(object sender, EventArgs e)
{
  if (this.IsPostBack)
  {
    // Do first-time things
  }
  else
  {
    // Do non-first-time things
  }
}

Note that the particular instance of your page object will not persist from access to access. So there may be some information that you need to initialize every time the page is called.

Every time you visit the page, you're creating a new instance of the class.

To distinguish a page load from a user clicking a button vs. a user arriving on the page for the first time, you want to check the IsPostBack property.

So rewrite your if along the lines of

// Code that always executes

if (IsPostBack)
{
    // Code that only executes on initial page load
}
else
{
    // Code that only executes when a postback event occurs
    // e.g. A user clicks on a button.
}

There are some ways by which you can maintain the state or value of the controls, so I have been maintaining the Viewstate of the controls in the not is postback, as check of

(!IspostBack) // means when page loads first time

and whatever written in the else means when postback occurs in which you can maintain the viewstate of the objects.

Also we can use the session, if Viewstate is not used.

Rykiel,

the basic concept of the web is (state-less), and that why you have to handling, but anyway you can read about page life cycle I recommend you read it http://www.codeproject.com/Articles/20659/The-ASP-NET-Page-Lifecycle-A-Basic-Approach

you can use

    if(!isPostBack)
    { 
       first=true;
         this portion of code only run when the page is requested first time
    }else
   {
      first = false;
    }

if you change the value of control in the page or click a button isPostBack will be true. but if you refresh the page or hit F5 you page will be requested again and isPostBack will be false;.

also you can use cookies or session variable (I also recommend do not load too much the Session Variable). try to read the prior link and you will be more clear where put your code to get the best performance.

J.S.

The answers on here are good, but technical. I will try to explain a little of what is happening.

When a browser requests your page, and on the server, a new instance of your class is created.

ASP.NET then runs the rest of the page, starting with your page_load. This will call all your other functions and then render the HTML as a response to your request and send it back to the browser. I like to explain this as a disconnected environment. Once the response is sent, everything is disposed of in a sense. Your variables, previous work, etc... are all gone. The Server as far as it is concerned, never expects to get anything from the browser again... its done its job. It took your request for a page, created a result and posted it back to the browser. Done.

So, think of your code as a new request each time it is called.

You can use the IsPostback as stated by ThatBlairGuy, because that will return true if you are responding to a postback from the browser, meaning that it has already served up this page to the browser on the previous postback.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top