hyperlink and responce.redirect - asp.net

I am new to asp.net and i need to know
whats the differences between a button with response.redirect behind it
and a hyper link ? they get you both to the page but one is server side while the other is not
is it the only difference
and when it is best to use either one of them

A hyperlink render a link to the given NavigateUrl in the browser. When the user clicks on it, the browser directly goes to the destination.
A button with redirect renders a button which does a postback using javascript. When the user clicks on it, a postback is done to the same page and the browser is instructed to go to another URL using Response.Redirect.
The main difference is that the second solution loads the same page again, while the first solution goes directly to the destination. With a button you can run some code before redirecting, or redirect to a different URL based on the information in the postback. Because it loads the original page before loading the destination page, it is a bit slower.
With a hyperlink, the visitor will see where the hyperlink leads to. With a button you can not see this. A hyperlink is thus better for search engines, because they will follow a hyperlink and will not follow a button.
If you know the URL in advance and you do not want to run extra code when the user clicks on something, use a hyperlink. Else, use a button.

A hyperlink is a link which will redirect you somewhere in the same tab or in a new tab when you click on it.
In case of response.sendredirect() the user's browser is redirected to a link sent by the server depending on your business logic.(the link may belong to some other domain).
hope that helps

Hyperlink control
This is server control use for navigation to another page specified in the NavigateURL property. Hyperlink control doesn’t expose any server side event.
Response.Redirect method
This method is used to navigate to another page from code. You can use this method to navigate from a Linkbutton or ImageButton control.

Related

Confirm Resubmission page on clicking back button

In my page I have a droplist which with OnSelectedIndexChanged event, which initiates a POST request to the server on selection change.
Whenever I navigate away from the page to someother page and try to click the back button in the browser it gives me a "Confirm Resubmission Page" since it has a post data associated for the page.
I cant use redirect to clear the post data since i need the data from the droplist control and other controls to be retained even after the OnSelectedIndexChanged. Is there any solution to come around this issue??
Take a look at this question. May be it can help.
If you can't redirect to self after your post request then you can do it using ajax.

Automatic page refresh even we navigate to another page

i have a problem in page navigating. please help me to solve out.
in one asp.net page forexample Page1.aspx i have dropdown when we select an item in dropdown that would display in a label. Then we navigate to another page from page1.aspx to page2.aspx by clicking linkbutton in page1.aspx. Again if i come to page1.aspx that previously selected value of dropdown should appear in label.
please help me.
HTTP is a stateless protocol. This means that it'll forget anything that you don't tell it to specifically ask it to.
When going back to Page1.aspx, your program has no idea what was selected before.
In terms of persisting user choices, you should look into storing them in Session ( or alternatively, cookies ) and checking for a pre-existing choice when returning to Page1.aspx.
If you have a saved value for the user at that point, you'll be able to set the correct value during the Page_Load event.
If it is like a wizard, I suggest you use a wizard control in the same page.
here is an example,
https://web.archive.org/web/20211020103244/https://www.4guysfromrolla.com/articles/061406-1.aspx

WebForms with ASP.NET AJAX: HyperLink vs LinkButton & Response.Redirect from GridView

I have 2 asp.net web forms. The first has a ScriptManager, History, UpdatePanel and GridView; the later is ScriptManager, UpdatePanel and TextBoxes. The premise here is a list that links to a detail form where an item can be edited.
From within the GridView (inside the UpdatePanel), if I use a HyperLink control with the url set to the edit page (with the necessary parameters), change something, save it and then click the back button I see the original list with no updates. Pressing F5 to refresh shows the changes.
If instead I use a LinkButton inside the GridView, and handle that LinkButton in code-behind to perform a Response.Redirect to the same edit page (with the same parameters), make the same changes, save and then click the back button, the list on the original page refreshes automatically to show my changes.
Note that the code in the detail page where the editing/saving takes place does not change - only the way it is first displayed is changed.
My question is this: what is it about the Response.Redirect that causes the page to be refreshed when the back button is clicked, and it it possible to replicate this for the direct HyperLink approach? I would prefer to use the HyperLink method as I see no reason for the postback, but I want the GridView to refresh when the user browses back to it.
Thanks.
A LinkButton causes a postback, the response to which is a HTTP 302 redirect command triggered on the server side by your Response.Redirect. Your web browser therefore does not cache the old version of the page.
The Hyperlink control simply renders a regular <a> tag which takes you to the detail page on the client side. The browser has no reason to believe the page may have changed, so it presents the cached version when you hit the back button.
If you want to tell the browser specifically not to cache the page if the back button is used,
use the cache-control HTTP header. W3C Link,
In any case, you should provide a link on the detail page (or automatic redirect on accepting changes) which takes the user back to the GridView/summary page, so they don't have to resort to using the back button.
Edit:
Sorry, the previously provided header example was not for Asp.net, but basically you'll want to do something like this:
Response.AppendHeader("Cache-Control", "no-cache")

ASP.net Image Button Issue

I have a hyperlink on a page that sends certain variables e.g. Default.aspx?var1=x&var2=y
I then create an ImageButton dynamically thereafter. My ImageButton has its own event.
It seems that the ImageButton inherits the values sent from the hyperlink. If I right click on the image button and view is properties via the browser it points to the following as well Default.aspx?var1=x&var2=y , I then tried to turn view state off which didnt work.
I also tried specifying the PostBackUrl so that the url variables are not sent. But then its event doesnt trigger.
Is there any way to not h
The postback URL for a form in ASP.NET will ALWAYS be the same URL that the form was originally loaded using. So if you load a page from a hyperlink with a querystring, then use a postback control on that page (like an ImageButton), it will always post back to the same URL it was originally loaded with, including the querystring.
There are different ways to change the postback URL that depend on the version of ASP.NET you're using, but if 2.0 or below it will have to be javascript. Search on this issue specifically (changing postback url or form action in asp.net) to find the answers to your problem.
Other less elegant solutions include redirecting a page to itself with no querystring after the initial load with a querystring.
You can also just ignore querystrings on postback, which is almost always the desired behaviour, so for the most part this is a cosmetic problem. E.g. in your code just do...
if (!Page.IsPostback) {
// check for querystring data and do stuff if there, otherwise ignore it
}
That's default behavior of ASP.NET. Since ImageButton will just cause the execution of event handler , the hyperlink attached to it will be its same page.
Why is this a concern ?

Firing server side validations on moving from one page to another in Asp.NEt

I need to fire certain server side validation for a page when the user is trying move from that page to another..
Even though he is not clicking on any buttons these server side validations have to be fired when he is moving out of this page..
Could you please help me how to handle this.. Required field validations will not happen as the user is entering the page and leaving the page without doing any action in the page..
So you mean he's just changing the URL in the address bar? Or is he clicking something? If it's via clicking anything; you can just make whatever that thing is part of the appropriate validation group, and it will validate.
If he's just changing the URL, then you're going to need to detect leaving of the page (in JavaScript), and do some work. But depending on your scenario, I wouldn't recommend that.

Resources