我停止执行并返回可能保留数据的同一页面 [英] I stop execution and return to the same page possible keeping the data
问题描述
我正在为公司开发一些东西,用户必须使用公司的电子邮件地址进行注册。 someName@company.lc
我使用默认的asp.net 2012账户注册。
我希望当用户将信息放入文本框id = username时,如果它不包含@ Company.lc向他们发送消息告诉他们使用正确的电子邮件地址
并给他们改变它的机会。
目前显示消息但继续继续执行。
我尝试了什么:
I am developing something for a company and the user must register using the company's email address. "someName@company.lc"
I am using the default asp.net 2012 Account Registration.
I want when the user put information in the textbox id=username, that if it does not contain @Company.lc to send a message to them telling to use the correct email address
and give them the oppunitity to change it.
At present it shows the message but continue continue executing.
What I have tried:
<WizardSteps>
<asp:CreateUserWizardStep runat="server" ID="RegisterUserWizardStep">
<ContentTemplate>
<p class="message-info">
Passwords are required to be a minimum of <%: Membership.MinRequiredPasswordLength %> characters in length.
</p>
<p class="validation-summary-errors">
<asp:Literal runat="server" ID="ErrorMessage" />
</p>
<fieldset>
<legend>Registration Form</legend>
<ol>
<li>
<asp:Label runat="server" AssociatedControlID="UserName">User name</asp:Label>
<asp:TextBox runat="server" ID="UserName" />
<asp:RequiredFieldValidator runat="server" ControlToValidate="UserName"
CssClass="field-validation-error" ErrorMessage="The user name field is required." />
</li>
<li>
<asp:Label runat="server" AssociatedControlID="Email">Email address</asp:Label>
<asp:TextBox runat="server" ID="Email" TextMode="Email" />
<asp:RequiredFieldValidator runat="server" ControlToValidate="Email"
CssClass="field-validation-error" ErrorMessage="The email address field is required." />
</li>
<li>
<asp:Label runat="server" AssociatedControlID="Password">Password</asp:Label>
<asp:TextBox runat="server" ID="Password" TextMode="Password" />
<asp:RequiredFieldValidator runat="server" ControlToValidate="Password"
CssClass="field-validation-error" ErrorMessage="The password field is required." />
</li>
<li>
<asp:Label runat="server" AssociatedControlID="ConfirmPassword">Confirm password</asp:Label>
<asp:TextBox runat="server" ID="ConfirmPassword" TextMode="Password" />
<asp:RequiredFieldValidator runat="server" ControlToValidate="ConfirmPassword"
CssClass="field-validation-error" Display="Dynamic" ErrorMessage="The confirm password field is required." />
<asp:CompareValidator runat="server" ControlToCompare="Password" ControlToValidate="ConfirmPassword"
CssClass="field-validation-error" Display="Dynamic" ErrorMessage="The password and confirmation password do not match." />
</li>
</ol>
<asp:Button runat="server" CommandName="MoveNext" Text="Register" />
</fieldset>
</ContentTemplate>
<CustomNavigationTemplate />
protected void RegisterUser_CreatedUser(object sender, EventArgs e)
{
FormsAuthentication.SetAuthCookie(RegisterUser.UserName, createPersistentCookie: false);
MembershipUser newUser = Membership.GetUser(RegisterUser.UserName);
//Make sure that the user have company email name@company.lc
if (newUser.Email.Contains("@company.lc") != true)
{
MsgBox("Please used your Company email ending @company.lc ");
}
else if (newUser.Email.Contains("@company.lc") == true)
{
SendEmail();
string continueUrl = RegisterUser.ContinueDestinationPageUrl;
if (!OpenAuth.IsLocalUrl(continueUrl))
{
continueUrl = "~/";
}
Response.Redirect(continueUrl);
}
}
public void MsgBox(String msg)
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), "Message Box", "<script language='javascript'> alert('" + msg + "')</script>");
}
推荐答案
CreatedUser
事件触发之后用户已创建。拒绝无效的用户名为时已晚。
你可以使用CreatingUser
事件验证新用户名,并设置e.Cancel = true
如果它无效。
但是,使用RegularExpressionValidator
<会更加清晰/ a> [ ^ ]来验证输入:
TheCreatedUser
event fires after the user has been created. That is too late to reject an invalid username.
You could use theCreatingUser
event to validate the new username, and sete.Cancel = true
if it's not valid.
However, it would be far cleaner to use theRegularExpressionValidator
[^] to validate the input:
<li>
<asp:Label runat="server" AssociatedControlID="UserName">User name</asp:Label>
<asp:TextBox runat="server" ID="UserName" />
<asp:RequiredFieldValidator runat="server"
ControlToValidate="UserName"
CssClass="field-validation-error"
ErrorMessage="The user name field is required."
/>
<asp:RegularExpressionValidator runat="server"
ControlToValidate="UserName"
CssClass="field-validation-error"
ErrorMessage="Please use your company email ending @company.lc"
ValidationExpression="^[^@]+@company\.lc
/>
< / li>
" /> </li>
protected void RegisterUser_CreatedUser(object sender, EventArgs e)
{
if (Page.IsValid)
{
FormsAuthentication.SetAuthCookie(RegisterUser.UserName, createPersistentCookie: false);
MembershipUser newUser = Membership.GetUser(RegisterUser.UserName);
SendEmail();
string continueUrl = RegisterUser.ContinueDestinationPageUrl;
if (!OpenAuth.IsLocalUrl(continueUrl))
{
continueUrl = "~/";
}
Response.Redirect(continueUrl);
}
}
Regexper: ^ [^ @] + @ company\.lc
[ ^ ]
正则表达式语言 - 快速参考 [ ^ ]
这篇关于我停止执行并返回可能保留数据的同一页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!