understanding IsPostBack property

janakors

New member
Joined
Apr 6, 2020
Messages
4
Programming Experience
Beginner
Hi there,
I need to use IsPostBack Property on my web site but I have understanding issues with it. now after exploring the web, I found that the ist page is loaded as get request but when I click again on the link of the same page so this will be a postback. so what I did is to put IsPostBack property on that page load event as follows

C#:
if (!IsPostBack)
{
    Response.Wriet("page loaded for the first time");
}
else
{
    Response.Wriet("page is  not loaded for the first time");
}

Now the above code when run for the first time so it is ok and work fine but when i click a link of the same page even again and again so i am getting the message"page loaded for the first time". what i thin that it should display the other message as the page is being posted back

please help in this regard what concept I am missing

Regards
 
Last edited by a moderator:

Sheepings

Senior Programmer
Staff member
Joined
Sep 5, 2018
Messages
1,141
Location
UK
Programming Experience
10+
Please post your code in code tags.

How are you setting your Page.IsPostBack Property (System.Web.UI) ?

C#:
    if (IsPostBack)
    {
        Response.Wriet("page is not loaded for the first time");
    }
    esle
    {
        Response.Wriet("page loaded for the first time");
    }
That logic is better. You don't need the inverted if statement, and it's easier to read like that. If you don't change your IsPostBack value, it will be stuck on whatever setting you last set it on. So how are you setting the property and where? Also, if you don't want to have your page refresh, consider using update panels.

Mod : Topic is also posted in the wrong section.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
1,227
Location
Virginia Beach, VA
Programming Experience
10+
but when I click again on the link of the same page so this will be a postback
This is not always true. It depends on what kind of link it is. If it is a vanilla anchor link that just refers to the same URL, then, no it will not be a post back. But if the link is actually a submit button for a HTTP form, and the submit button is set to use a POST action, and the target of the submit button is same URL as the current page, then it should be a post back.
 
Top Bottom