从登录页面重定向页面时出现问题

本文关键字:问题 重定向 登录 | 更新日期: 2023-09-27 17:58:24

嗨以下是我用于登录页面的代码。但它不会进入按钮单击。当我尝试放置新按钮时,它的按钮单击事件会在aspx页面中打开。我不知道问题到底出在哪里。

protected void btnSubmit_Click(object sender, EventArgs e)
{
    string password = "";
    string query = "select AdminPwd from tbladminlogin where AdminId='" + txtUserName.Text + "'";
    MySqlConnection objMyCon = new MySqlConnection(strProvider);
    objMyCon.Open();
    MySqlCommand cmd = new MySqlCommand(query, objMyCon);
    MySqlDataReader r = cmd.ExecuteReader();
    while (r.Read())
    {
        password = r[0].ToString();
    }
    if (password == "")
    {
        Session["Login"] = false;
        lblMessage.Visible = true;
        lblMessage.Text = "Invalid Username";
        txtUserName.Text = "";
        txtPwd.Text = "";
    }
        // 
    else
    {
        if (txtPwd.Text == password)
        {
            Session["Login"] = true;
            Session["UserName"] = txtUserName.Text;
            Response.Redirect("concertDetail.aspx");
        }
        else if (txtPwd.Text != password)
        {
            Session["Login"] = false;
            lblMessage.Visible = true;
            lblMessage.Text = "Invalid Password";
            txtPwd.Text = "";
        }
    }
    objMyCon.Close();
}

//aspx页面

<div id="login">
    <%--<div class="response-msg success ui-corner-all">
                        <span>Success message</span>
                        Lorem ipsum dolor sit amet, consectetuer adipiscing elit
                    </div>--%>
    <asp:Label ID="lblMessage" runat="server" Text="Label" Font-Size="Medium" ForeColor="Red"></asp:Label>
    <form id="Form1" runat="server">
    <ul>
        <li>
            <label for="email" class="desc">
                Username:
            </label>
            <div>
                <%--<input type="text" tabindex="1" maxlength="255" value="" class="field text full" name="email" id="email" />--%>
                <asp:TextBox ID="txtUserName" runat="server" MaxLength="255" Width="470px"></asp:TextBox>
            </div>
        </li>
        <li>
            <label for="password" class="desc">
                Password:
            </label>
            <div>
                <%--<input type="text" tabindex="1" maxlength="255" value="" class="field text full" name="password" id="password" />--%>
                <asp:TextBox ID="txtPwd" runat="server" MaxLength="255" Width="470px"></asp:TextBox>
            </div>
        </li>
        <li class="buttons">
            <div style="position: relative; top: -4px; left: 408px; width: 67px; height: 32px;">
                <div style="position: relative;">
                </div>
                <%--<button class="ui-state-default ui-corner-all float-right ui-button" onclick="btnSubmit_Click" type="submit">Login</button>--%>
                <asp:Button ID="btnSubmit" runat="server" Text="Login" OnClick="btnSubmit_Click" />
            </div>
        </li>
    </ul>
</form>

从登录页面重定向页面时出现问题

下面的onclick事件用javascript搜索函数"btnSubmit_Click",而不是代码隐藏,因为按钮是html控件:

<button class="ui-state-default ui-corner-all float-right ui-button" onclick="btnSubmit_Click" type="submit">Login</button>

至于asp:Button,它应该可以工作。它似乎没有任何问题。请检查ASP.NET开发服务器是否尚未运行。接下来,清除缓存并检查浏览器中是否启用了调试。

就双击问题而言,你应该禁用它。据我所知,最好的解决方案是在客户端处理它:

<script>
    var reqSubmitted = false;
    function submitRequest() {
        if (!reqSubmitted) {
            reqSubmitted = true;
            return reqSubmitted;
        }
        reqSubmitted = false;
        return reqSubmitted;
    }
</script>

<form id="Form1" runat="server" onsubmit="return submitRequest()">

放这个:

 <form id="Form1" runat="server" defaultbutton="btnSubmit">

而不是

 <form id="Form1" runat="server">

如前所述,请使用参数化查询来避免sql注入和其他许多问题。