单击按钮时触发Page_load事件. [英] Page_load event fired when button is clicked..

查看:450
本文介绍了单击按钮时触发Page_load事件.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个登录页面,其中包括两个文本框和一个按钮.一个用于用户名,另一个用于密码.当我单击按钮输入用户名和密码后,将自动触发page_load事件.这使我的if(!IsPostBack)检查无效.因为如果将page_load事件触发两次,然后在第二次将page_load视为回发,并且我的if(!IsPostBack)块内的登录逻辑将永远不会执行..任何人都可以帮助我解决此问题..

关于


这是我的登录页面的代码段..

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
Rain

Here is a code snippet of my login page..

<pre lang="c#">
 protected void Page_Load(object sender, EventArgs e)
        {
Supporter.setStaticVariables();
}

 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))
                    Response.Redirect("Web.aspx");
                else
                {
                    
                    lblmismatch.Visible = true;
                    lblmismatch.Text = "**Password and username doesn''t match**";
                }
            }
        }

推荐答案

Page_Load子例程在页面每次加载时运行.您可以在登录按钮单击事件内编写登录逻辑.

第一次加载页面时,在if(!IsPostback){....}块中编写的代码仅执行一次.

无论如何,您可以查看如何跳过调用Page_Load事件 [ ^ ] .


--Amit
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[^].


--Amit


Page_Load事件将始终触发,因为单击按钮后会发回邮件.看来您已经在按钮click事件处理程序中进行了回发检查,请尝试将其移至page_load事件处理程序.按钮单击处理程序应正常执行.
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.


我认为您的按钮控件存在一些问题(希望您不要在OnclientClick事件中调用其他任何东西,或者可能是其他原因).

建议您删除现有按钮,然后添加一个新按钮,然后重试...
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…


这篇关于单击按钮时触发Page_load事件.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆