Stop page loads on Web form

BigRunnerGuy

New member
Joined
Oct 1, 2014
Messages
4
Programming Experience
1-3
Hey guys,

I'm building my first web page using asp.net and C# in visual studio. I notice whenever any button is clicked the entire page is reloaded which resets all of the variables. Can someone please point me in the right direction towards what to research in order to stop this event? Thank you.
 

JohnH

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
903
Location
Norway
Programming Experience
10+
If you don't want postback you should probably use a Html Input button and not a server control button. If you for some reason need it to be a server control button then you can "return false" from its OnClick script (OnClientClick).
 

BigRunnerGuy

New member
Joined
Oct 1, 2014
Messages
4
Programming Experience
1-3
If you don't want postback you should probably use a Html Input button and not a server control button. If you for some reason need it to be a server control button then you can "return false" from its OnClick script (OnClientClick).


It does need to run at server and returning false seems to stop all c# code that would be executed, essentially making it a button that does nothing.
 

wim sturkenboom

Well-known member
Joined
Aug 6, 2014
Messages
85
Location
Roodepoort, South Africa
Programming Experience
10+
You are aware of the IsPostBack flag?

if (!IsPostBack)
{
              // initialization
    // clear textboxes, populate dropdownlists etc
}
else
{
    // don't touch controls
}

You can use this in the Page_Load(). Note that this does not stop the postbacks.
 

BigRunnerGuy

New member
Joined
Oct 1, 2014
Messages
4
Programming Experience
1-3
You are aware of the IsPostBack flag?

if (!IsPostBack)
{
              // initialization
    // clear textboxes, populate dropdownlists etc
}
else
{
    // don't touch controls
}

You can use this in the Page_Load(). Note that this does not stop the postbacks.


Thank you for your response, yes I am aware of it. My problem is that it is clearing the variables everytime the page reloads.
 

JohnH

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
903
Location
Norway
Programming Experience
10+
It does need to run at server and returning false seems to stop all c# code that would be executed, essentially making it a button that does nothing.
C# code runs at server during postback, which is when page is reset and recreated by state management, that is generally how ASP.Net works.
I guess the solution is to use a static variable
There are many state management options, have a look: ASP.NET State Management Overview
In addition Ajax and UpdatePanel allows for partial page postbacks, and client callbacks for calling some server code without postback: Implementing Client Callbacks Programmatically Without Postbacks in ASP.NET Web Pages
 
Top Bottom