简体   繁体   中英

Logout linkbutton is not firing click event

I am trying to implement my login and logout through my webpage. There are two panels which toggle. On my page load, the logout panel is made invisible so that the user may only see the login link. After logging in the login panel is made invisible and the logout panel is made visible where the logout link is there for logging out. but the logout linkbutton is not even firing the click event. This is what i see in the browser window at the bottom when i try to click the logout link button 这是我尝试单击注销链接按钮时在底部浏览器窗口中看到的内容 This is my aspx page-

<div>
<asp:Panel ID="LoginPanel" runat="server">
    <ul class="style1">
                <li>Username</li>
                <li><asp:TextBox ID="LoginEmail" runat="server" Width="90%" placeholder="Email" class="form-control"></asp:TextBox></li>

                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Email Required" ControlToValidate="LoginEmail" ForeColor="#CC0000" Font-Italic="True"></asp:RequiredFieldValidator>

                <li>Password</li>
                <li><asp:TextBox ID="LoginPassword" runat="server" Width="90%" placeholder="Password" class="form-control" TextMode="Password"></asp:TextBox></li>

                <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Password Required" ControlToValidate="LoginPassword" ForeColor="#CC0000" Font-Italic="True"></asp:RequiredFieldValidator>

                <li><asp:Button ID="LoginButton" runat="server" Text="Login" class="btn btn-default" onclick="LoginButton_Click"/>

                &nbsp;New User?<asp:HyperLink ID="new_user" runat="server" NavigateUrl="Registration.aspx">Register Here</asp:HyperLink></li>
    </ul>
</asp:Panel>
<asp:Panel ID="LogoutPanel" runat="server">
                <asp:LinkButton ID="LogoutLink" runat="server" Text="Logout" 
                    onclick="LogoutLink_Click" />
</asp:Panel>
</div>

This is my cs code-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

public partial class UserMasterPage : System.Web.UI.MasterPage
{
    ConnectionClass cl;
    SqlDataReader dr;
    string sql;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LogoutPanel.Visible = false;
            cl = new ConnectionClass();
        }
    }
    protected void LoginButton_Click(object sender, EventArgs e)
    {
        sql = "select Id, Email_, Password_, Block from MemberLogin where Email_='" + LoginEmail.Text + "'";
        cl = new ConnectionClass();
        cl.establishConnection();
        cl.createReaderCommand(sql);
        dr = cl.executeReaderCommand();
        if (dr.HasRows)
        {
            if (LoginPassword.Text == dr["Password_"].ToString())
            {
                if (dr["Block"].ToString() == "False")
                {
                    Session["user"] = dr[0].ToString();
                    LoginPanel.Visible = false;
                    LogoutPanel.Visible = true;
                    //Response.Write("login successful");
                }
            }
        }
        cl.closeConnection();
    }
    protected void LogoutLink_Click(object sender, EventArgs e)
    {
        Session.Abandon();
        Response.Redirect("AdminLogin.aspx");
    }
}

This is on my master page. I need to solve this problem very soon. Please help me with this.Thanks in advance.

The __DoPostBackWithOptions call associated with the LinkButton is related to the presence of the validators in your page. Setting CausesValidation to false would replace it by the "regular" __doPostBack :

<asp:LinkButton ID="LogoutLink" runat="server" CausesValidation="false" ... />

The validators shown in your markup being in a Panel with Visible = false , they should not be active. But since I don't see anything else that could prevent the postback, turning off the validation for the LinkButton would at least eliminate that possible cause.

By the way, if you have many controls and validators in your form, you may consider grouping them with validation groups. That would allow each button (or other controls causing a postback) to trigger only the relevant validators:

<asp:TextBox ID="txt1a" runat="server" ValidationGroup="Group1" />
<asp:RequiredFieldValidator ID="rfv1a" runat="server" ControlToValidate="txt1a" ValidationGroup="Group1" Text="*" ForeColor="Red" />
<asp:TextBox ID="txt1b" runat="server" ValidationGroup="Group1" />
<asp:RequiredFieldValidator ID="rfv1b" runat="server" ControlToValidate="txt1b" ValidationGroup="Group1" Text="*" ForeColor="Red" />
<asp:Button ID="btn1" runat="server" Text="Button1" ValidationGroup="Group1" />
<br />
<asp:TextBox ID="txt2a" runat="server" ValidationGroup="Group2" />
<asp:RequiredFieldValidator ID="rfv2a" runat="server" ControlToValidate="txt2a" ValidationGroup="Group2" Text="*" ForeColor="Red" />
<asp:TextBox ID="txt2b" runat="server" ValidationGroup="Group2" />
<asp:RequiredFieldValidator ID="rfv2b" runat="server" ControlToValidate="txt2b" ValidationGroup="Group2" Text="*" ForeColor="Red" />
<asp:Button ID="btn2" runat="server" Text="Button2" ValidationGroup="Group2" />

In the example above, a click on btn1 would cause a postback even if txt2a is empty, because they don't belong to the Group1 validation group.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM