Master page postback.. !IsPostback doesnt work

A

anthonykallay

Hi there,

I dont this this a bug but i cant get my head around it.. I have a
master page that has a login control on it, to log in i press the
button and the page posts back, however on the page that is derived
from the master page (default.aspx) i have code that i dont want to
run after the login button is pressed so i used the standard

if(!Page.IsPostBack) {}

to try and hide the code on default.aspx, however since the postbsck
comes from the control in the master page this code still runs???

How do i stop this code running in default.aspx whn the login button
in the aster page is clicked??

Cheers
Anthony
 
M

Mark Rae [MVP]

Hi there,

I dont this this a bug but i cant get my head around it.. I have a
master page that has a login control on it, to log in i press the
button and the page posts back, however on the page that is derived
from the master page (default.aspx) i have code that i dont want to
run after the login button is pressed so i used the standard

if(!Page.IsPostBack) {}

to try and hide the code on default.aspx, however since the postbsck
comes from the control in the master page this code still runs???

How do i stop this code running in default.aspx whn the login button
in the aster page is clicked??

A couple of things...

Firstly, in the MasterPage / ContentPage scenario, content pages aren't
"derived" from master pages - quite the reverse! In fact, a MasterPage is
really nothing more than a UserControl.

Secondly, master pages are intended to provide common look and feel (and
sometimes functionality) across several pages, possibly an entire site. If
you have functionality in your master page which is intended for only one
content page, then that functionality would be better placed in the content
page itself, not the master page...
 
A

anthonykallay

Hi Mark,

Thanks for your reply but i dont think you quite understood what my
problem was..

The user control in the master page (login.ascx) does need to be on
every page, the only problem i have is that when i click this login
button and the page posts back all the code in default.aspx page_load
posts again even if i use the !IsPostback statement?? Why is this flag
not stopping the page_load code from executing in the default.aspx
page??

Anthony
 
P

Patrice

You could also write the value to make sure your assumption is correct. For
now I would write down the value to make sure it's incorrect. Actually for
now I would expect that the value is correct but that you do something that
doesn't need the code to run (for example when you postback, control values
are populated from posted values even if you don't bind data etc...)
 
A

anthonykallay

Hi all,

Thanks for your replies but i still think im not being clear.. on
default.aspx i have the following code

if (!Page.IsPostBack)
{
string page;

if (Request.QueryString["PageID"] != null)
page = Request.QueryString["PageID"].ToString();
}

Obviously this code runs the first time the page loads, then when
someone logs in they press the submit button in the login control on
the masterpage causing the page to "postback". However when this
"postback occurs the above code runs when i thought the if (!
Page.IsPostBack) would stop the code running as i have just posted the
page from the login ox.. Why does this code run??

Cheers
Anthony
 
P

Patrice

Well have you tried to display Page.IsPostBack as asked previously ? How do
you know for sure that this code runs. Sorry for being picky but sometimes
we start from the assumption that a portion of code runs when actually it is
some other portion that does the job or because the code doesn't need to run
to get the same effect....

I would like to make 100% sure that IsPostBack doesn't return the correct
value. The best way is to just print this value to see wether or not it is
false. You could also dump Request.ServerVariables("HTTP_METHOD") to see if
it"'s GET or POST.

For example if you have some other code that does a redirect once the
postback for login occurs (which is perhaps the case with the standard
RedirectFromLogin method) then you would have a postback that does a
redirect and finally you are not seeing a postback in your code (i.e. you'll
see postback being false and the method being GET)...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,525
Members
44,997
Latest member
mileyka

Latest Threads

Top