views:

421

answers:

1

I have a page request with a QueryString, say http://www.xyz.net/Orders.aspx?OrderID=1. The page is displayed in a browser. Now there is an asp:LinkButton on the page which should enable the user to open the page without the QueryString (as if he had entered http://www.xyz.net/Orders.aspx directly in the browser's address bar).

I had two ideas:

1) Use the PostBackUrl attribute of the LinkButton:

<asp:LinkButton ID="LinkButton1" runat="server" Text="Select"
    PostBackUrl="~/Orders.aspx" />

2) Use "RedirectUrl" in an event handler:

<asp:LinkButton ID="LinkButton1" runat="server" Text="Select"
    OnClick="LinkButton1_Click" />

...and...

protected void LinkButton1_Click(object sender, EventArgs e)
{
    Response.Redirect("~/Orders.aspx");
}

In both cases the browser's address bar shows http://www.xyz.net/Orders.aspx without the QueryString, as I like to have it. But in the first case the page does not change at all. But it should, because I'm evaluating the QueryString in code-behind and control the appearance of the page depending on whether a QueryString exists or not. The second option works as intended.

If I am not wrong the second option requires an additional roundtrip:

  • Browser sends request to server
  • Event handler on server side sends Redirect URL to browser
  • Browser sends again request to the server, but with the new URL
  • Server sends new requested page to browser

Is this correct at all?

Whereas the first option omits the first two steps in the list above, thus saving the additional roundtrip and resulting in:

  • Browser sends request to the server, but with the new URL (the PostbackURL specified in the LinkButton)
  • Server sends new requested page to browser

But, as said, the result isn't the same.

I'm sure my try to explain the differences between the two options is wrong somewhere. But I don't know where exactly.

Can someone explain what's really the difference? Do I really need this second roundtrip of option (2) to achieve what I want?

Thanks in advance!

Update and solution

The author of the question (me) has proven with this question that he does not understand the difference between HTTP POST and GET. For the solution see my comment in egrunin's answer.

+2  A: 

If you don't want to hit the server, why not use a plain HTML control:

function stripQuerystring()
{
    return window.location.protocol + "//" 
        + window.location.host + window.location.pathname;
}

<input type="button" 
    onclick="javascript:window.location = stripQuerystring();" 
    value="Click Me">
egrunin
Thanks, it works! But why? And why not the first option in my question? Doesn't the `PostBackUrl` attribute of the LinkButton do basically the same: changing the URL of the page request to the URL without the QueryString? The URL is hardcoded in my code whereas your script is more general of course. But in my special example: Why isn't the result the same?
Slauma
@egrunin: Forget the questions in my comment. I didn't understand the difference between POST and GET. What I actually should have done is using `asp:HyperLink` with `NavigateUrl` property instead of a LinkButton with PostBackUrl. I've seen now that the important difference in code-behind is that in case of a HyperLink `IsPostBack==false` whereas in case of the LinkButton `IsPostBack==true` which explains the totally different behaviour. But using an asp:HyperLink is basically the same like your solution. Thanks again for pointing me into the right direction!
Slauma