注销linkbutton不会触发click事件

时间:2016-04-20 01:40:04

标签: c# asp.net

我正在尝试通过我的网页实现登录和注销。切换有两个面板。在我的页面加载中,注销面板变为不可见,以便用户只能看到登录链接。登录后,登录面板变为不可见,并且登出面板在登出链接所在的位置可见。但是注销链接按钮甚至没有触发click事件。这是我在底部的浏览器窗口中看到的,当我尝试单击注销链接按钮This is what i see in the browser window at the bottom when i try to click the logout link button时 这是我的aspx页面 -

<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>

这是我的cs代码 -

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");
    }
}

这是在我的母版页上。我需要很快解决这个问题。请帮帮我。谢谢你。

1 个答案:

答案 0 :(得分:3)

与LinkBut​​ton相关联的__DoPostBackWithOptions调用与您网页中验证程序的存在有关。将CausesValidation设置为false会将其替换为“常规”__doPostBack

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

标记中显示的验证程序位于具有Visible = false的Panel中,它们不应处于活动状态。但由于我没有看到任何其他可以阻止回发的内容,因此关闭LinkBut​​ton的验证至少可以消除这种可能的原因。

顺便说一下,如果表单中有许多控件和验证器,您可以考虑将它们与验证组分组。这将允许每个按钮(或导致回发的其他控件)仅触发相关的验证器:

<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" />

在上面的示例中,即使btn1为空,点击txt2a也会导致回发,因为它们不属于Group1验证组。