• Visitors can check out the Forum FAQ by clicking this link. You have to register before you can post: click the REGISTER link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. View our Forum Privacy Policy.
  • Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!

Simple ASP .NET question

Collapse
X
  •  
  • Filter
  • Time
  • Show
Clear All
new posts

    Simple ASP .NET question

    This is not my area, so please be gentle. This is a simple shopping basket system I knocked up in C# and VS2005 and runs on a Windows 2003 server.

    The initial url is of the form: www.site.com/basket.aspx?addqty=1

    And I have something like:
    Code:
    protected void Page_Load(object sender, EventArgs e)
    {
      string addQty = Request["addqty"];
       if (addQty != null)
      {
         m_basket.IncreaseItem(item);
      }
      // display basket and calculate totals...
    }
    That all works. There's also a check box on the page (for VAT) and that has an OnClick handler:

    Code:
    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
       Session["VATOverride"]=1;
       Response.Redirect("Basket.aspx");
    }
    The problem is if I go to the page via the addqty=1 link, and then click on the checkbox, it reloads the basket but does the addqty again. I assumed (perhaps naively) that because the redirect was to just "Basket.aspx", the Request variable would be empty on the next PageLoad. Is that correct? Or does the Request persist? Or is there something else going on - perhaps the browser is caching the request somehow?

    Thanks for any help.
    Will work inside IR35. Or for food.

    #2
    Originally posted by VectraMan View Post
    This is not my area, so please be gentle. This is a simple shopping basket system I knocked up in C# and VS2005 and runs on a Windows 2003 server.

    The initial url is of the form: www.site.com/basket.aspx?addqty=1

    And I have something like:
    Code:
    protected void Page_Load(object sender, EventArgs e)
    {
      string addQty = Request["addqty"];
       if (addQty != null)
      {
         m_basket.IncreaseItem(item);
      }
      // display basket and calculate totals...
    }
    That all works. There's also a check box on the page (for VAT) and that has an OnClick handler:

    Code:
    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
       Session["VATOverride"]=1;
       Response.Redirect("Basket.aspx");
    }
    The problem is if I go to the page via the addqty=1 link, and then click on the checkbox, it reloads the basket but does the addqty again. I assumed (perhaps naively) that because the redirect was to just "Basket.aspx", the Request variable would be empty on the next PageLoad. Is that correct? Or does the Request persist? Or is there something else going on - perhaps the browser is caching the request somehow?

    Thanks for any help.
    It's been yonks since I did asp.net but I would have thought the way to do it was to make the checkbox do a postback on change (which would reload the page without needing Response.Redirect) and then add an if(!Page.Postback) statement in the page load event.

    Comment


      #3
      Code:
      protected void Page_Load(object sender, EventArgs e)
      {
      if (!this.IsPostBack)
      {
        int addQty;
         if (!string.IsNullOrEmpty(Request["addqty"]) && int.TryParse(Request["addqty"],addQty))
        {
          addQty = Math.Abs(addQty);
           m_basket.IncreaseItem(item);
        }
      }
        // display basket and calculate totals...
      }
      Code needs to be a bit more defensive.
      Remember, most users are t***ts.

      Comment


        #4
        Originally posted by Bunk View Post
        It's been yonks since I did asp.net but I would have thought the way to do it was to make the checkbox do a postback on change (which would reload the page without needing Response.Redirect) and then add an if(!Page.Postback) statement in the page load event.
        I thought the postback was the mechanism that allowed the check box to call the server side CheckChanged function.

        Is that the issue? Does that mechanism call the Page_Load again with the same Request data?
        Will work inside IR35. Or for food.

        Comment


          #5
          Yes, kinda, if you look at your pages form in the rendered HTML once you've loaded your page with the querystring, it'll look like:

          <form name="form1" method="post" action="basket.aspx?addqty=1" id="form1">

          So that is what your form is posting to. An quick fudge may be to change the action attribute:

          m_basket.IncreaseItem(item);
          form1.Action = "basket.aspx";

          Comment


            #6
            Originally posted by VectraMan View Post
            Does that mechanism call the Page_Load again with the same Request data?
            Yes, so you don't need the response.redirect. Use the IsPostback property in the Page_Load - this will be true when your handler is invoked & false when you first load the page.

            Edit: Which is basically what Bunk said
            Last edited by wurzel; 18 August 2011, 15:08.

            Comment

            Working...
            X