• 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

VectraMan

Lord of the Vectras
Joined
Dec 28, 2005
Messages
11,460
Likes received
1,218
Thanks received
233
Location
European Union (Yorkshire)
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.
 
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.
 
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.
 
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?
 
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";
 
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: 15:57

Advertisers

Back
Top