ASP.Net登录控件的登录按钮事件未在ASP.Net中触发 [英] ASP.Net Login control's loggin button event not firing in ASP.Net

查看:82
本文介绍了ASP.Net登录控件的登录按钮事件未在ASP.Net中触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一个登录控件。在那个登录按钮就在那里。我的问题是,在点击登录按钮时,事件未触发。请看下面的代码。请帮帮我..



Login.aspx页面代码 -

 <   asp:登录    ID   =  Login1    runat   = 服务器    OnAuthenticate   =  Login1_Authenticate    OnLoggedIn   =  Login1_LoggedIn  

LabelStyle-ForeColor = white CheckBoxStyle-ForeColor = 白色

高度 = 133px 宽度 = 290px >
< span class =code-keyword>< CheckBoxStyle ForeColor = 白色 > < / CheckBoxStyle > ;
< LoginButtonStyle > < / LoginButtonStyle >
< LayoutTemplate >
< table border = 0 < span class =code-attribute> >
< tr >
< td 样式 = 颜色:白色; background-color:transparent; align = right >
< asp:标签 ID = UserNameLabel 文字 = 用户名: < span class =code-attribute> runat = server ForeColor = 灰色 AssociatedControlID = UserName 字体粗体 = true > < / asp:标签 >
< / td >
< td >
& nbsp;& nbsp;& nbsp ; < asp:TextBox ID = UserName runat = 服务器 宽度 = 120px > < / asp:TextBox >
< asp:RequiredFieldValidator ID = UserNameRequired runat = server ControlToValidate = UserName

ErrorMessage = 用户名是必需的。 工具提示 < span class =code-keyword> = 用户名是必需的。 < span class =code-attribute> ValidationGroup = Login1 > * < / asp:RequiredFieldValidator >
< / td < span class =code-keyword>>
< / tr >
< tr >
< td style = 颜色:白色; background-color:transparent; align = right >
< asp:标签 ID = PasswordLabel runat = server 文本 = 密码: ForeColor = 灰色 AssociatedControlID = 密码 Font-Bold = true > < / asp:标签 >
< / td >
< td >
& nbsp;& nbsp;& nbsp; < span class =code-keyword>< asp:TextBox ID = 密码 runat = server TextMode = 密码 宽度 = 120px < span class =code-attribute> > < / asp:TextBox >
< asp:RequiredFieldValidator ID = PasswordRequired runat = 服务器 ControlToValidate = 密码

ErrorMessage = 密码是必需的。 ToolTip = 密码是必需的。 ValidationGroup = Login1 > * < / asp :RequiredFieldValidator >
< / td >
< / tr >
< tr runat = server id = tdDate >
< td style = 颜色:白色; background-color:transparent; align = right >
< asp:标签 ID = lblDate runat = server Font-Bold = true > 日期:< / asp:标签 >
< span class =code-keyword>< / td >
< td >
< asp:TextBox < span class =code-attribute> ID = txtSystemDate runat = server 高度 = 20px > < / asp:TextBox >
< / td >
< / tr >
< tr >
< td align = center colspan < span class =code-keyword> = 2 >
& nbsp;& nbsp;& nbsp; < < span class =code-leadattribute> asp:按钮 ID = LoginButton runat = server CommandName = 登录 < span class =code-attribute> 文本 = 登录 ValidationGroup = Login1 / > & nbsp;& nbsp;
< / td >
< / tr >
< tr > ;
< td colspan = 2 valign = top align = center >
< asp:HyperLink 字体下划线 = false ID = hlFPwd ForeColor = LightBlue CssClass = text form-control

目标 < span class =code-keyword> = _ self NavigateUrl = 〜/ ForgotPassword.aspx runat = server 文字 = 忘记密码?

< span class =code-attribute> 可见 = true BackColor = 透明 < span class =code-attribute> Font-Bold = true > < / asp:HyperLink > & nbsp;
< / td >
< / tr >
< tr >
< td align = left colspan = 2 style = 颜色:红色; >
< asp:Literal ID = FailureText runat = server EnableViewState = False > < ; / asp:Literal >
< / td >
< / tr >
< / table >
< / LayoutTemplate >
< LabelStyle ForeColor = 白色 > < / LabelStyle >
< / asp:登录 >



Login.aspx.cs页码 -

<前lang =cs> 受保护 void Page_Load( object sender,EventArgs e)
{
TextBox txtSystemDate =(TextBox) Login1.FindControl( txtSystemDate);
Label lblDate =(Label)Login1.FindControl( lblDate);
if (txtSystemDate.Text ==
{
txtSystemDate.Text = DateTime.Today.ToString( MM / dd / yyyy); // System.DateTime.Now.ToString();
}
if (ConfigurationSettings.AppSettings [ SysteminProduction]。ToString()==
{
lblDate.Visible = false ;
txtSystemDate.Visible = false ;
}

// TrailVersion();
KeyValues =(Dictionary< String,String>)应用程序[ KeyValues];


if (ConfigurationSettings.AppSettings [ SharepointLogin]。ToString()==
{

string username = Context.User.Identity.Name.Substring(Context.User.Identity.Name。 IndexOf(' \\')+ 1 < /跨度>);
if (用户名!=
{
AutoLogin(用户名+ @ gh.com);
}
}

Login1.Focus();
// Login1_Authenticate();
}

protected 覆盖 void OnInit(EventArgs e )
{
// lblYear.Text = DateTime.Now.Date.ToString(yy );
if (!IsPostBack&& Session [ user] == null
{

if (Request.Cookies [ Cook] != null
{
HttpCookie cookie = Request.Cookies [ < span class =code-string> Cook
];

string userId = cookie.Values[\"userId\"].ToString();
string pwd = cookie.Values[\"pwd\"].ToString();


int PrimaryRoleID = 0;

int HRID = 0;

bool _IsActive = false;

int LoginId = APMSUser.ValidateLogin(userId, pwd, out PrimaryRoleID, out HRID, out _IsActive);

if (LoginId > 0)
{
// e.Authenticated = true;

Session[\"LoginId\"] = LoginId;

Session[\"PrimaryRole\"] = PrimaryRoleID;

Session[\"HRID\"] = HRID;
IsEmpActive = _IsActive;
}

if (_IsActive == false)
{
doLogin(userId, pwd);
}
else
{
Login1.FailureText = \"You have been deactivated, please contact administrator.\";
}
}
}
base.OnInit(e);
}

public void Login1_Authenticate(object sender, System.Web.UI.WebControls.AuthenticateEventArgs e)
{


int LoginId;
int PrimaryRoleID = 0;
int HRID = 0;
bool isMatched = false;
bool beforelogin = true;

bool _IsActive = false;
DataSet dsLogcheck;
PrjMgmtComps.CDataManager.CDataManager objloginCheck = new PrjMgmtComps.CDataManager.CDataManager();
dsLogcheck = objloginCheck.LoginLock(Login1.UserName, isMatched, beforelogin);
if (dsLogcheck.Tables[0].Rows.Count != 0)
{

if (Convert.ToInt32(dsLogcheck.Tables[0].Rows[0][\"LoginAttempts\"].ToString()) >= 3)
{
LoginId = 0;
}
else
{
LoginId = APMSUser.ValidateLogin(Login1.UserName, Login1.Password, out PrimaryRoleID, out HRID, out _IsActive);
IsEmpActive = _IsActive;
}
}
else
{
LoginId = APMSUser.ValidateLogin(Login1.UserName, Login1.Password, out PrimaryRoleID, out HRID, out _IsActive);
IsEmpActive = _IsActive;
}

if (LoginId > 0)
{

if (IsEmpActive == false)
{
e.Authenticated = true;
isMatched = true;
beforelogin = false;

dsLogcheck = objloginCheck.LoginLock(Login1.UserName, isMatched, beforelogin);

Session[\"LoginId\"] = LoginId;

Session[\"PrimaryRole\"] = PrimaryRoleID;

Session[\"HRID\"] = HRID;

//Session[\"SystemDate\"] = // txtSystemDate.Text;
}
else
{
Login1.FailureText = \"You have been deactivated, please contact administrator.\";

}
}
else
{

int GuestID = APMSUser.GuestLogin(Login1.UserName, Login1.Password);
Session[\"GuestID\"] = GuestID;
isMatched = false;
beforelogin = false;
dsLogcheck = objloginCheck.LoginLock(Login1.UserName, isMatched, beforelogin);
if (dsLogcheck.Tables[0].Rows.Count != 0)
{

if (dsLogcheck.Tables[0].Rows[0][\"IsLockedOut\"].ToString() == \"True\")
{
Login1.FailureText = \"Your password has been locked please change your Password!!\";
Session[\"PrimaryRole\"] = 1323;//later get from web.config

}
}

else
{
if (GuestID.Equals(0))
{
Login1.FailureText = \"Login Incorrect.Try Again!!\";
Session[\"PrimaryRole\"] = 1323;//later get from web.config

}

else
{
Response.Redirect(@\"apc\Portfolio_Modules.aspx\");
}
}
}

}
public void Login1_LoggedIn(object sender, EventArgs e)
{

//CheckBox cek = (CheckBox)Login1.FindControl(\"RememberMe\");
//if (cek != null && cek.Checked)
//{

// HttpCookie cookie = new HttpCookie(\"Cook\");
// cookie.Values.Add(\"userId\", Login1.UserName);
// cookie.Values.Add(\"pwd\", Login1.Password);
// cookie.Expires = DateTime.Now.AddYears(1);
// Response.Cookies.Add(cookie);

//}

if (IsEmpActive == false)
{
doLogin(Login1.UserName, Login1.Password);
}
else
{
Login1.FailureText = \"You have been deactivated, please contact administrator.\";

}
}

public void doLogin(string strUserID, string strPassword)
{
try
{
CController objUser = new CController();
PrjMgmtComps.ObjectFactory.CSecurity obj = new PrjMgmtComps.ObjectFactory.CSecurity();
obj = objUser.Login(strUserID, strPassword);

if (obj.LoginId.ToString() != null)
{
if (obj.LoginId.ToString() != \"0\")
{
Session[\"LoginId\"] = obj.LoginId.ToString();
Session[\"LoginName\"] = obj.LoginName.ToString();
Session[\"ModulePermission\"] = obj.dtModulePermission;
Session[\"ChildEntity\"] = obj.dtChildEntity;
Session[\"GroupName\"] = obj.GroupName.ToString();
Session[\"Mail\"] = obj.Mail.ToString();
Session[\"UserID\"] = obj.LoginId.ToString();
string RestrictedRole = \"\";
DataView dv = new DataView(obj.dtModulePermission, \"RestrictedRole='Restricted Role'\", \"RestrictedRole\", DataViewRowState.CurrentRows);
if (dv.Count > 0)
{
RestrictedRole = \"Yes\";
}
else
{
RestrictedRole = \"No\";
}

Session[\"RestrictedRole\"] = RestrictedRole.ToString();
//Added by Madhu on 14-DEc-2011 to get all in active projects
//CheckBox chkInActive = (CheckBox)Login1.FindControl(\"chkInActive\");
//if (chkInActive.Checked)
//{
// Session[\"ShowAll\"] = \"True\";
//}
//else
//{
// Session[\"ShowAll\"] = \"False\";
//}


TextBox txtSystemDate = (TextBox)Login1.FindControl(\"txtSystemDate\");
if (txtSystemDate.Text == \"\")
{
txtSystemDate.Text = DateTime.Today.ToString(\"MM/dd/yyyy\");//System.DateTime.Now.ToString();
}
Session[\"SystemDate\"] = txtSystemDate.Text;
//End By Jyoshna

//if (ChkProfile.Checked == true)
// Session[\"CheckProfile\"] = \"PROFILE_ON\";
//else
// Session[\"CheckProfile\"] = \"PROFILE_OFF\";
Session[\"LoginPage\"] = \"NO\";
if (obj.IsChanged == true)
{
if (ConfigurationManager.AppSettings[\"SysteminProduction\"].ToString() == \"No\")
{
Response.Redirect(@\"Portfolio\ICVMain.aspx\");
}
else
{
Response.Redirect(@\"Portfolio\PortfoliloMain.aspx\");
}
}
else
{
Session[\"LoginPage\"] = \"Yes\";
Server.Transfer(\"Portfolio\\APMS\\ChangePassword.aspx\");

}
Response.Redirect(@\"apc\PortfoliloMain.aspx?m=yes\");

}

}//if
}
catch (Exception ex)
{
throw ex;
}
}







[Edit member=\"Tadit\"]

Added pre tags.

[/Edit]

解决方案

Hi,

I have a login control. In that Login button is there. my problem is, while clicking on Login button, event is not firing. please see the below code. Please help me..

Login.aspx page code--

<asp:Login ID="Login1" runat="server" OnAuthenticate="Login1_Authenticate" OnLoggedIn="Login1_LoggedIn"

     LabelStyle-ForeColor="white" CheckBoxStyle-ForeColor="White"

    Height="133px" Width="290px" >
    <CheckBoxStyle ForeColor="White"></CheckBoxStyle>
    <LoginButtonStyle ></LoginButtonStyle>
    <LayoutTemplate>
        <table border="0"  >
            <tr>
                <td  style="color: white; background-color: transparent;" align="right">
                    <asp:Label ID="UserNameLabel" Text="User Name :" runat="server" ForeColor="Gray" AssociatedControlID="UserName" Font-Bold="true"></asp:Label>
                </td>
                <td>
                    &nbsp;&nbsp;&nbsp;<asp:TextBox ID="UserName" runat="server"   Width="120px"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName"

                        ErrorMessage="User Name is required." ToolTip="User Name is required." ValidationGroup="Login1">*</asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td  style="color: white; background-color: transparent;" align="right">
                    <asp:Label ID="PasswordLabel" runat="server" Text="Password:" ForeColor="Gray" AssociatedControlID="Password" Font-Bold="true"></asp:Label>
                </td>
                <td>
                    &nbsp;&nbsp;&nbsp;<asp:TextBox ID="Password" runat="server" TextMode="Password" Width="120px"  ></asp:TextBox>
                    <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password"

                        ErrorMessage="Password is required." ToolTip="Password is required." ValidationGroup="Login1">*</asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr runat="server" id="tdDate" >
                <td  style="color: white; background-color: transparent;" align="right">
                    <asp:Label ID="lblDate" runat="server" Font-Bold="true">Date:</asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txtSystemDate" runat="server"  Height="20px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td align="center" colspan="2">
                    &nbsp;&nbsp;&nbsp;<asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="Log In" ValidationGroup="Login1" />&nbsp;&nbsp;
                </td>
            </tr>
            <tr>
                <td colspan="2" valign="top" align="center">
                    <asp:HyperLink Font-Underline="false" ID="hlFPwd" ForeColor="LightBlue" CssClass="text form-control"

                        Target="_self" NavigateUrl="~/ForgotPassword.aspx" runat="server" Text="Forgot Password ?"

                        Visible="true" BackColor="Transparent" Font-Bold="true"></asp:HyperLink>&nbsp;
                </td>
            </tr>
            <tr>
                <td align="left" colspan="2" style="color: Red;">
                    <asp:Literal ID="FailureText" runat="server" EnableViewState="False"></asp:Literal>
                </td>
            </tr>
        </table>
    </LayoutTemplate>
    <LabelStyle ForeColor="White"></LabelStyle>
</asp:Login>


Login.aspx.cs page code--

protected void Page_Load(object sender, EventArgs e)
{
    TextBox txtSystemDate = (TextBox)Login1.FindControl("txtSystemDate");
    Label lblDate = (Label)Login1.FindControl("lblDate");
    if (txtSystemDate.Text == "")
    {
    txtSystemDate.Text = DateTime.Today.ToString("MM/dd/yyyy");//System.DateTime.Now.ToString();
    }
    if (ConfigurationSettings.AppSettings["SysteminProduction"].ToString() == "Yes")
    {
    lblDate.Visible = false;
    txtSystemDate.Visible = false;
    }
    
    //TrailVersion();
    KeyValues = (Dictionary<String, String>)Application["KeyValues"];
    
    
    if (ConfigurationSettings.AppSettings["SharepointLogin"].ToString() == "Yes")
    {
    
    string username = Context.User.Identity.Name.Substring(Context.User.Identity.Name.IndexOf('\\') + 1);
    if (username != "")
    {
    AutoLogin(username + "@gh.com");
    }
    }
    
    Login1.Focus();
    //Login1_Authenticate();
}

protected override void OnInit(EventArgs e)
{
    //lblYear.Text = DateTime.Now.Date.ToString("yy");
    if (!IsPostBack && Session["user"] == null)
    {

        if (Request.Cookies["Cook"] != null)
        {
            HttpCookie cookie = Request.Cookies["Cook"];

            string userId = cookie.Values["userId"].ToString();
            string pwd = cookie.Values["pwd"].ToString();


            int PrimaryRoleID = 0;

            int HRID = 0;

            bool _IsActive = false;

            int LoginId = APMSUser.ValidateLogin(userId, pwd, out  PrimaryRoleID, out HRID, out _IsActive);

            if (LoginId > 0)
            {
                // e.Authenticated = true;

                Session["LoginId"] = LoginId;

                Session["PrimaryRole"] = PrimaryRoleID;

                Session["HRID"] = HRID;
                IsEmpActive = _IsActive;
            }

            if (_IsActive == false)
            {
                doLogin(userId, pwd);
            }
            else
            {
                Login1.FailureText = "You have been deactivated, please contact administrator.";
            }
        }
    }
    base.OnInit(e);
}

public void Login1_Authenticate(object sender, System.Web.UI.WebControls.AuthenticateEventArgs e)
{


    int LoginId;
    int PrimaryRoleID = 0;
    int HRID = 0;
    bool isMatched = false;
    bool beforelogin = true;

    bool _IsActive = false;
    DataSet dsLogcheck;
    PrjMgmtComps.CDataManager.CDataManager objloginCheck = new PrjMgmtComps.CDataManager.CDataManager();
    dsLogcheck = objloginCheck.LoginLock(Login1.UserName, isMatched, beforelogin);
    if (dsLogcheck.Tables[0].Rows.Count != 0)
    {

        if (Convert.ToInt32(dsLogcheck.Tables[0].Rows[0]["LoginAttempts"].ToString()) >= 3)
        {
            LoginId = 0;
        }
        else
        {
            LoginId = APMSUser.ValidateLogin(Login1.UserName, Login1.Password, out  PrimaryRoleID, out HRID, out _IsActive);
            IsEmpActive = _IsActive;
        }
    }
    else
    {
        LoginId = APMSUser.ValidateLogin(Login1.UserName, Login1.Password, out  PrimaryRoleID, out HRID, out _IsActive);
        IsEmpActive = _IsActive;
    }

    if (LoginId > 0)
    {

        if (IsEmpActive == false)
        {
            e.Authenticated = true;
            isMatched = true;
            beforelogin = false;

            dsLogcheck = objloginCheck.LoginLock(Login1.UserName, isMatched, beforelogin);

            Session["LoginId"] = LoginId;

            Session["PrimaryRole"] = PrimaryRoleID;

            Session["HRID"] = HRID;

            //Session["SystemDate"] =  // txtSystemDate.Text;
        }
        else
        {
            Login1.FailureText = "You have been deactivated, please contact administrator.";

        }
    }
    else
    {

        int GuestID = APMSUser.GuestLogin(Login1.UserName, Login1.Password);
        Session["GuestID"] = GuestID;
        isMatched = false;
        beforelogin = false;
        dsLogcheck = objloginCheck.LoginLock(Login1.UserName, isMatched, beforelogin);
        if (dsLogcheck.Tables[0].Rows.Count != 0)
        {

            if (dsLogcheck.Tables[0].Rows[0]["IsLockedOut"].ToString() == "True")
            {
                Login1.FailureText = "Your password has been locked please change your Password!!";
                Session["PrimaryRole"] = 1323;//later get from web.config

            }
        }

        else
        {
            if (GuestID.Equals(0))
            {
                Login1.FailureText = "Login Incorrect.Try Again!!";
                Session["PrimaryRole"] = 1323;//later get from web.config

            }

            else
            {
                Response.Redirect(@"apc\Portfolio_Modules.aspx");
            }
        }
    }

}
public void Login1_LoggedIn(object sender, EventArgs e)
{

    //CheckBox cek = (CheckBox)Login1.FindControl("RememberMe");
    //if (cek != null && cek.Checked)
    //{

    //    HttpCookie cookie = new HttpCookie("Cook");
    //    cookie.Values.Add("userId", Login1.UserName);
    //    cookie.Values.Add("pwd", Login1.Password);
    //    cookie.Expires = DateTime.Now.AddYears(1);
    //    Response.Cookies.Add(cookie);

    //}

    if (IsEmpActive == false)
    {
        doLogin(Login1.UserName, Login1.Password);
    }
    else
    {
        Login1.FailureText = "You have been deactivated, please contact administrator.";

    }
}

public void doLogin(string strUserID, string strPassword)
{
    try
    {
        CController objUser = new CController();
        PrjMgmtComps.ObjectFactory.CSecurity obj = new PrjMgmtComps.ObjectFactory.CSecurity();
        obj = objUser.Login(strUserID, strPassword);

        if (obj.LoginId.ToString() != null)
        {
            if (obj.LoginId.ToString() != "0")
            {
                Session["LoginId"] = obj.LoginId.ToString();
                Session["LoginName"] = obj.LoginName.ToString();
                Session["ModulePermission"] = obj.dtModulePermission;
                Session["ChildEntity"] = obj.dtChildEntity;
                Session["GroupName"] = obj.GroupName.ToString();
                Session["Mail"] = obj.Mail.ToString();
                Session["UserID"] = obj.LoginId.ToString();
                string RestrictedRole = "";
                DataView dv = new DataView(obj.dtModulePermission, "RestrictedRole='Restricted Role'", "RestrictedRole", DataViewRowState.CurrentRows);
                if (dv.Count > 0)
                {
                    RestrictedRole = "Yes";
                }
                else
                {
                    RestrictedRole = "No";
                }

                Session["RestrictedRole"] = RestrictedRole.ToString();
                //Added by Madhu on 14-DEc-2011 to get all in active projects
                //CheckBox chkInActive = (CheckBox)Login1.FindControl("chkInActive");
                //if (chkInActive.Checked)
                //{
                //    Session["ShowAll"] = "True";
                //}
                //else
                //{
                //    Session["ShowAll"] = "False";
                //}


                TextBox txtSystemDate = (TextBox)Login1.FindControl("txtSystemDate");
                if (txtSystemDate.Text == "")
                {
                    txtSystemDate.Text = DateTime.Today.ToString("MM/dd/yyyy");//System.DateTime.Now.ToString();
                }
                Session["SystemDate"] = txtSystemDate.Text;
                //End By Jyoshna

                //if (ChkProfile.Checked == true)
                //  Session["CheckProfile"] = "PROFILE_ON";
                //else
                //  Session["CheckProfile"] = "PROFILE_OFF";
                Session["LoginPage"] = "NO";
                if (obj.IsChanged == true)
                {
                    if (ConfigurationManager.AppSettings["SysteminProduction"].ToString() == "No")
                    {
                        Response.Redirect(@"Portfolio\ICVMain.aspx");
                    }
                    else
                    {
                        Response.Redirect(@"Portfolio\PortfoliloMain.aspx");
                    }
                }
                else
                {
                    Session["LoginPage"] = "Yes";
                    Server.Transfer("Portfolio\\APMS\\ChangePassword.aspx");

                }
                Response.Redirect(@"apc\PortfoliloMain.aspx?m=yes");

            }

        }//if
    }
    catch (Exception ex)
    {
        throw ex;
    }
}




[Edit member="Tadit"]
Added pre tags.
[/Edit]

解决方案

这篇关于ASP.Net登录控件的登录按钮事件未在ASP.Net中触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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