从登录页面重定向页面的问题

时间:2011-06-06 07:47:11

标签: c# asp.net

您好 下面是我用于登录页面的代码。但它没有进入按钮点击。当我尝试放置新按钮时,其按钮点击事件在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 page

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

2 个答案:

答案 0 :(得分:0)

下面的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()">

答案 1 :(得分:0)

把这个:

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

而不是

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

如前所述,请使用parametrized query来避免sql注入和其他许多问题。