Click here to Skip to main content
15,617,101 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a login page which include two text box and a button. one is for user name and another is for password. After entering the user name and password when i click the button the page_load event is fired automatically. This makes my if(!IsPostBack) checking useless. Because if the page_load event is fired twice then in the second time the page_load is considered as postback and my login logic inside the if(!IsPostBack) block is never executed..can any one help me with this problem..

with regards

Here is a code snippet of my login page..

<pre lang="c#">
 protected void Page_Load(object sender, EventArgs e)

 protected void btnsubmit_Click(object sender, EventArgs e)

            string uname = txtusername.Text;
            string password = txtpassword.Text;
            Session["user"] = txtusername.Text;
            if (!IsPostBack)
                if (checkuser(uname, password, Supporter.strConString))
                    lblmismatch.Visible = true;
                    lblmismatch.Text = "**Password and username doesn't match**";
Updated 1-Oct-12 1:54am
I.explore.code 1-Oct-12 7:49am    
can u post the code please?
Achha Insan 1-Oct-12 8:22am    
nothing is impossible. you can do it.

The Page_Load subroutine runs EVERY time the page is loaded. You can write your login logic inside your Login Button Click Event.

The code written inside if(!IsPostback){....} block is executed only once when page is loaded for the first time.

Anyway, you can take a look at How to skip calling Page_Load event[^].

Share this answer
goroba 1-Oct-12 7:56am    
Thanks amy for your reply. I have written my logic in my Login button click event only..But the problem is whenever i click the button the page_load event is fired automatically..i just cant understand why..
_Amy 1-Oct-12 8:12am    
To load the page contents this event will be fired every time when page is posted back to the server. It's up to you that you can avoid reloading the content by writing your content in if(!IsPostBack){....} block of page load.

I.explore.code 1-Oct-12 7:59am    
i m not sure why would you want to skip page_load event when you can just move the !ispostback to page load event and it will effectively do the same thing. Feel free to correct though.
Page_Load event will always fire because of the post back that happens on button click. It seems you have got your postback check in the button click event handler, try moving it to page_load event handler. Button click handler should then execute normally.
Share this answer
goroba 1-Oct-12 8:03am    
if i move the if(!IsPostBack) logic in the page load event handler then how will i check whether the page is a postback or not when i click the login button.. what i want to achieve is, after clicking the login button the page should be checked as postback or not and then then accordingly different logic will execute..
I.explore.code 1-Oct-12 8:09am    
if your button click is only handling one function, then I don't see the problem. Moving the check to the page_load event handler would ensure that the view state of the controls is not reinitailised and your click handler would still execute the way you want it to.
goroba 1-Oct-12 8:14am    
ok.. i will try it. i'm new to and stuff so i'm just trying to learn the correct approach to create web application..any way thanks for helping me..
I.explore.code 1-Oct-12 8:18am    
you are welcome mate! Also i would suggest reading up on ASP.NET page lifecycle, that would really help you to understand how pages come to life and die. :)
I think some problem with your button control (I hope you are not calling anything else like in OnclientClick event or may be something else).

I suggest you to delete your existing button add a fresh button and try again…
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900