注册按钮刷新页面而不添加用户

时间:2014-10-10 11:29:58

标签: asp.net entity-framework button

我有一个ASPX网页,其中包含四个字段,电子邮件,确认电子邮件,密码,确认密码和显示名称。我已将代码放在页面后面的代码中,这样当按下注册按钮时,它应该提交信息并将新用户添加到数据库中。但是,当我单击按钮时,它只刷新页面。我已经尝试弄乱后面的代码,以便在单击按钮时它只是重定向到没有if语句的主页,但它仍然刷新页面。 以下是相关代码:

namespace PiccyPic
{
    public partial class Login : System.Web.UI.Page
    {
        protected void butRegisterDefine(object sender, EventArgs e)
        {
            butRegister.Click += butRegister_Click;
        }
        void butRegister_Click(object sender, EventArgs e)
       {
         using (DataLayer.Repository db = new DataLayer.Repository())
         {
           BO.User user = db.UserGet(txtEmail.Text);

            if (user != null)
            {
                //If the same email exists
                pnlError.Visible = true;
                lblError.Text = "Error: The email you have entered is already assigned to an account.";
            }
            else if (user.Username != null)
            {
                //If the same username exists
                pnlError.Visible = true;
                lblError.Text = "Error: The username you have entered is already assigned to an account.";

            }
            else if (txtEmail.Text != txtEmailConfirm.Text)
            {
                //If the emails don't match
                pnlError.Visible = true;
                lblError.Text = "Error: The emails you have entered do not match";
            }
            else if (txtPassword.Text != txtPasswordConfirm.Text)
            {
                //If the passwords don't match
                pnlError.Visible = true;
                lblError.Text = "Error: The passwords you have entered do not match";
            }
            else
            {
                // Add user
                var newUser = new BO.User(txtEmail.Text, txtPassword.Text, txtUsername.Text);

                db.UserAdd(newUser);
                db.Save();

                Response.Redirect("Default.aspx");
            }


        }
    }
}

1 个答案:

答案 0 :(得分:0)

您的标记中设置的OnClick方法是什么?我没有C#专家,但在我看来你的OnClick没有正确注册。什么时候"但是注册定义"运行

如果你没有定义onlick,你需要在某处运行事件注册(butRegisterDefine)。 Page_Load似乎是一个好地方。下面的片段有用。

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="ButtonTest.Login" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button runat="server" ID="butRegister" Text="Register" />
        <asp:Label runat="server" ID="lblSomething"></asp:Label>
    </div>
    </form>
</body>
</html>

代码隐藏:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ButtonTest
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            butRegisterDefine(sender, e);
        }

        protected void butRegisterDefine(object sender, EventArgs e)
        {
            butRegister.Click += butRegister_Click;
        }

        protected void butRegister_Click(object sender, EventArgs e)
        {
            lblSomething.Text = "Clicked!";
        }
    }
}