Post back and browser back button issue in asp.net - asp.net

I am working on web application. I have a grid that shows data to the user. When user click on the any row of the grid, it redirects user to another page, as we have a asp link control on the a column.
Issues
My code is like
if (!Page.IsPostBack)
{
//CODE
}
When user click on the BROWSER BACK button, it does not execute the CODE. Simply show the data from CACHE.
How can I execute the CODE , on browser back button click ?

It's a global problem with ASP.Net. Most of developers thinks like Windows developer. It ends with a postback for mostly any action, including navigation actions.
To solve such problems:
Avoid using button to change page. Generate an hyperlink with the target url.
Wrap parts of the page within an update panel. The benefits is that the browser won't see a page change after each postback. Then you will be able to "refresh" the page without warning dialog
When populating a grid, instead of "selecting" a row from codebehin, generate a link to the same page with "?ID=42" in the url, and bind this value to the grid as the selectedvalue
The root cause of this "evil" behavior, is that postback are issued using HTTP Post request. Then, the browser requires to repost the page to rebuild it.
Using the technics mentionned above, you are issuing GET request. This kind of request doesn't require to resubmit anything.

Try Disabling browser cache for the page that you don't want to cache.
protected void Page_Load(object sender, EventArgs e)
{
Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
Response.Cache.SetValidUntilExpires(false);
Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();
if (!Page.IsPostBack)
{
//CODE
}
}

Related

ASP.NET Master Page Server Control Events

I had been having a great deal of issues trying to put a server control button and event handler onto a master page. Every time I clicked the button from one of the content pages, the postback would take me not back to the content page, but to the site.master URL, so naturally that would crash my application. I scoured the internet for a solution, but I couldn't get any to work for me. I finally figured out a very easy fix and so I thought I'd share it in case anyone else runs into similar troubles.
On the Master Page's Page_Init() method, I assigned the PostBackUrl attribute of the server control to the current page Uri using HttpContext.Current.Request.Url.AbsoluteUri;
(UserLogin is my button control name)
protected void Page_Init(object sender, EventArgs e)
{
UserLogin.PostBackUrl = HttpContext.Current.Request.Url.AbsoluteUri;
}

ASP.NET change captcha without page refresh

I've created a simple captcha system in my assp.net web application, I use an ASPX page as ImageUrl of my captcha image, this ASPX file creates captcha image and returns it to be used as my image source. It works fine but I'm going to insert a Change Captcha button which I'd like to change my captcha WITHOUT REFRESH, I've used to methods with no success,
first I tried Ajax Update panel and inserted my change captcha button and my captcha image into it, my captcha didn't change (of course when I use a button outside of update panel to change captcha, it works fine but I have page refresh),
this is my button click code:
protected void Button1_Click(object sender, EventArgs e)
{
imgCaptcha.ImageUrl = "CreateCaptcha.aspx?New=1";
}
then I used a Jquery Ajax call (web method) to call my CaptchaImage.ASPX file and use it as my image source, but again I had no luck! what is going wrong here? in both cases my CaptchaImage.ASPX file is never called! how can I change my captcha image without page refresh?
thanks
$('#btnSearch').click(function () {
var src = //Url to image
$('#image').attr("src", src);
});
Above code changes source of image (#image) used b'coz image is the id for the imagebox.or whatever u using.

Dropdownlist selectedindex doesn't work on page refresh

I'm having a problem with a very normal ASP.NET dropdownlist.
I have a dropdownlist in the page with AutoPostback=false.
And a code to generate dropdownlist’s content:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.dropDownList.Items.Add(new ListItem("text1", "value1"));
this.dropDownList.Items.Add(new ListItem("text2", "value2"));
this.dropDownList.SelectedIndex = 0;
}
}
Run the page, and choose the second option: text2.
After that, press F5, this will do a page load (note that I haven't done any postback yet, just change the selected item of Dropdownlist and press F5)
I expected that the dropdownlist will now selected the default item with index = 0, but the selected item is still the item “text2”- the item that I have chosen. In this situation, the this.dropDownList.SelectedIndex = 0; doesn’t work.
I totally don’t understand. Could anyone help me?
Update: This behavior only happens in Firefox, it doesn't happen in Chrome/IE.
It seems like you're misunderstanding what post back means; it does not mean just a page refresh. When you hit F5 in the browser, the original request will be sent to the server, and Page.IsPostBack will be false.
If you want to do an actual postback, add an asp:Button server control to the page and click it (or just set AutoPostBack=true on the DropDownList).
It seems like Firefox is doing some kind of client-side caching, so it doesn't make another round-trip to the server when you hit F5. You can work around this by resetting the select element when the page loads.
<script type='text/javascript'>
document.getElementById('<%= dropDownList.ClientID %>').selectedIndex = 0;
</script>
(Note: put this at the bottom of the page so that it executes after the document has loaded.)
When you press F5, it will send the post headers along with the request.
Your last request was to select the second option. So that gets selected.
If you type the url again, or hit enter in the address bar; you will not send the post data.
In this case your SelectedIndex will be 0.

Firing Postback in ASP.NET Without javascript

Is this possible?
EDIT
I want to user the asp:ListView
List item
I want to use it for editing
I dont want the postback to use Javascript when I put it into the edit mode
I've been trying to use a to do this instead of a link button but to no avail.
.
The only way to trigger a PostBack without JavaScript is by using a submit button. This is an HTML limitation, not an ASP.NET one.
I'm a little unsure of what you're trying to do without more detail. If I knew more specifically what you're trying to accomplish I could give you more details.
Like you mentioned in your post, you could use the <a> tag to send the user to the page you want.
You can add information to the link like so:
Click here for a new item
Then in the page load of newpage.aspx you can check what the action the user selected was, the query parameters are stored in the Request (Language is C#).
protected void Page_Load(object sender, EventArgs e)
{
string action = Request.Params["action"];
if(!String.IsNullOrEmpty(action))
{
switch(action)
{
case "newitem":
//handle the new item action
break;
case "deleteitem":
//handle the delete item action
break;
//handle other actions.
}
}
}
EDIT: You should be aware that the <a> tag will send the user to the page specified, the page though will act as if it is the first time the user has visited the page. With that said, the page variable IsPostBack will be false.

Refresh the page after a postback action in asp.net

I have command button added in my asp.net grids. After performing an action using that button, we refresh the grid to reflect the new data. (basically this action duplicates the grid row).
Now when user refresh the page using F5, an alert message is displayed (to resend the information to server) if we select "retry", the action is repeated automatically.
I know this is a common problem in asp.net, how can we best handle this?
Search for GET after POST - http://en.wikipedia.org/wiki/Post/Redirect/Get - basically, redirect to the current page after you're finished processing your event.
Something like:
Response.Redirect(Request.RawUrl)
If you think you don't need postback paradigm, you might want to look at ASP.NET MVC.
The problem is that asp.net buttons perform form posts when you push a button. If you replace the button with a link your problem should go away. You can also use a button that performs a javascript function that sets the document.location to the address of your page.
If I well understood, you simply have to check if you are in a post-back situation before populating your grid.
Assuming you do that on Page_Load, simply surround the operation with post-back test like this:
private void Page_Load(object sender, EventArgs e)
{
if(!this.IsPostBack)
{
// populate grid
}
}
You need to call response.Redirect(Request.Url.ToString());
or you can wrap the grid with updatepanel and after every command bind the datasource to grid
Inside your <asp:Repeater> tag put this:
EnableViewState="false"
This will cause your control to refresh every time the page loads, no matter if it's a postback or not.
for example:
if you click on 'button' system will catch the event 'button_click'.
if you refresh the page, system will re execute again the same event.
to don t have this problem, in your event insert :
on your event
private void button_click(object sender, System.EventArgs e)
{
button.Enabled =false;
button.Enabled =true;
}
is what you meant?

Resources