Asp.NET Webforms register.aspx验证
我正在开发一个使用Visual Studio 2017的自定义网站。我已经包含了Identity,因此有一些提供的页面用于注册或登录用户。我正在修改注册页面以允许使用fileupload控件上传图片(我创建了一个配置文件表,这将保存我想要捕获的额外数据)。但是,当我这样做时,即使电子邮件文本框中有数据,我提交时也会触发电子邮件验证。我认为这可能与上传功能发生时页面重新加载的方式有关?我还想添加一个复选框并显示其他字段(如果选中),使用CheckedChanged事件来设置我想要隐藏的控件的Visible属性。我已经看到,如果我想这样做,我需要将AutoPostBack设置为true。 代码在register.aspxAsp.NET Webforms register.aspx验证
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent"> <h2><%: Title %>.</h2>
<p class="text-danger">
<asp:Literal runat="server" ID="ErrorMessage" />
</p>
<div class="form-horizontal">
<h4>Create a new account</h4>
<hr />
<asp:ValidationSummary runat="server" CssClass="text-danger" />
<div class="form-group">
<asp:Label runat="server" AssociatedControlID="UserName" CssClass="col-md-2 control-label">User Name</asp:Label>
<div class="col-md-10">
<asp:TextBox runat="server" ID="UserName" CssClass="form-control" />
<asp:RequiredFieldValidator runat="server" ControlToValidate="UserName"
CssClass="text-danger" ErrorMessage="The user name is required." />
</div>
</div>
<div class="form-group">
<asp:Label runat="server" AssociatedControlID="Email" CssClass="col-md-2 control-label">Email</asp:Label>
<div class="col-md-10">
<asp:TextBox runat="server" ID="Email" CssClass="form-control" TextMode="Email" />
<asp:RequiredFieldValidator runat="server" ControlToValidate="Email"
CssClass="text-danger" ErrorMessage="The email field is required." />
</div>
</div>
<div class="form-group">
<asp:Label runat="server" AssociatedControlID="Password" CssClass="col-md-2 control-label">Password</asp:Label>
<div class="col-md-10">
<asp:TextBox runat="server" ID="Password" TextMode="Password" CssClass="form-control" />
<asp:RequiredFieldValidator runat="server" ControlToValidate="Password"
CssClass="text-danger" ErrorMessage="The password field is required." />
</div>
</div>
<div class="form-group">
<asp:Label runat="server" AssociatedControlID="ConfirmPassword" CssClass="col-md-2 control-label">Confirm password</asp:Label>
<div class="col-md-10">
<asp:TextBox runat="server" ID="ConfirmPassword" TextMode="Password" CssClass="form-control" />
<asp:RequiredFieldValidator runat="server" ControlToValidate="ConfirmPassword"
CssClass="text-danger" Display="Dynamic" ErrorMessage="The confirm password field is required." />
<asp:CompareValidator runat="server" ControlToCompare="Password" ControlToValidate="ConfirmPassword"
CssClass="text-danger" Display="Dynamic" ErrorMessage="The password and confirmation password do not match." />
</div>
</div>
<div class="form-group">
<asp:Label runat="server" AssociatedControlID="UserPicture" CssClass="col-md-2 control-label">Upload your user picture</asp:Label>
<div class="col-md-10">
<asp:FileUpload runat="server" ID="UserPicture" CssClass="form-control" />
</div>
</div>
<div class="form-group">
<asp:Label runat="server" AssociatedControlID="userStatus" CssClass="col-md-2 control-label">Do you want to upload Artwork? If so check this box and once approved you will be able to do this.</asp:Label>
<div class="col-md-10">
<asp:CheckBox ID="userStatus" runat="server" CssClass="form-control" AutoPostBack="true" Checked=false OnCheckedChanged="userStatus_CheckedChanged" />
</div>
</div>
<div class="form-group">
<asp:Label runat="server" AssociatedControlID="ArtistIntro" CssClass="col-md-2 control-label" ID="lblInfo" Visible="True">Please enter information about yourself as an artist</asp:Label>
<div class="col-md-10">
<asp:TextBox runat="server" ID="ArtistIntro" CssClass="form-control" Visible="True" />
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<asp:Button runat="server" OnClick="CreateUser_Click" Text="Register" CssClass="btn btn-default" />
<asp:Label ID="lblError" runat="server"></asp:Label>
</div>
</div>
</div>
</asp:Content>
在register.aspx.cs
using System; using System.Linq;
using System.Web;
using System.Web.UI;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
using Owin;
using UHFinal.Models;
using System.Data.SqlClient;
namespace UHFinal.Account
{
public partial class Register : Page
{
protected void CreateUser_Click(object sender, EventArgs e)
{
var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
var signInManager = Context.GetOwinContext().Get<ApplicationSignInManager>();
var user = new ApplicationUser() { UserName = UserName.Text };
IdentityResult result = manager.Create(user, Password.Text);
if (result.Succeeded)
{
//Insert a UserAccount record to save the additional details.
//Also sets the userStatus. AP or UP, Artist Pending or User Pending
string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["defaultConnection"].ConnectionString;
SqlConnection conn = new SqlConnection(connStr);
SqlCommand insert = new SqlCommand("insert into userAccount(UserID, userStatus, UserPicture, ArtistIntro) " +
"values(@userId, @userStatus, @UserPicture, @UserIntro)", conn);
insert.Parameters.AddWithValue("@userId", user.Id);
if (userStatus.Checked == true) {
insert.Parameters.AddWithValue("@userStatus", "AP");
}
else
{
insert.Parameters.AddWithValue("@userStatus", "UP");
}
insert.Parameters.AddWithValue("@UserPicture", "UserPics/" + UserPicture.FileName);
insert.Parameters.AddWithValue("@UserIntro", ArtistIntro.Text);
try
{
conn.Open();
object returnObj = insert.ExecuteNonQuery();
}
catch (Exception ex)
{
lblError.Text = "Error: " + ex.Message;
}
conn.Close();
signInManager.SignIn(user, isPersistent: false, rememberBrowser: false);
IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
}
else
{
ErrorMessage.Text = result.Errors.FirstOrDefault();
}
}
protected void userStatus_CheckedChanged(object sender, EventArgs e)
{
if (userStatus.Checked == true)
{
ArtistIntro.Visible = true;
lblInfo.Visible = true;
}
else
{
ArtistIntro.Visible = false;
lblInfo.Visible = false;
}
}
}
}
回答:
排序的代码,创建新ApplicationUser当我刚走出去的电子邮件。
var user = new ApplicationUser() { UserName = UserName.Text, Email = Email.Text };
以上是 Asp.NET Webforms register.aspx验证 的全部内容, 来源链接: utcz.com/qa/258185.html