单击按钮时,页面中的所有文本字段都显示必需的错误(ASP.NET,C#(Web)? [英] When click on button then all the text field in the page show required error (ASP.NET, C# (web)?

查看:47
本文介绍了单击按钮时,页面中的所有文本字段都显示必需的错误(ASP.NET,C#(Web)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好/您好!

我有3页aspx,一个是母版页,其他是家和注册页面,当我打开注册页面并点击创建帐户然后所有必填字段显示必要的选项,我再次检查验证控制ID是否对每个文本框都不同。任何人都帮助我。我的代码谢谢

这里是我的register.aspx代码

 <   label       =  name > 名称<   / label  >  
< asp:TextBox style = position:relative;右:40像素; width:200px runat = server ID = name 拼写检查 = false 占位符 = shreee / >

< asp:RequiredFieldValidator ID = RequiredFieldValidator3 runat = server ControlToValidate = name ErrorMessage = 名称必填 ForeColor = 红色 > < / asp:RequiredFieldValidator >
< / div >



 <   div  >  
< < span class =code-leadattribute> label for = 电子邮件 > 电子邮件< / label >

< asp:TextBox style = position:relative;右:40像素; width:200px; runat = server ID < span class =code-keyword> = txtEmail 占位符 = abcd@mail.com / >
< asp:RequiredFieldValidator ID = RequiredFieldValidator4 runat = server ControlToValidate = txtEmail ErrorMessage = 需要电子邮件 ForeColor = 红色 > < / asp:RequiredFieldValidator >
< asp:RegularExpressionValidator ID = regexEmailValid runat = server ValidationExpression = \w +([ - +] \w +)* @ \w +([ - ] \w +)* \.\w +([ - ] \w +)* < span class =code-attribute> ControlToValidate = txtEmail ErrorMessage = 无效的电子邮件格式 ForeColor = 红色 > < / asp:RegularExpressionValidator >



这是我的错误截图,当我点击创建帐户然后所有文本字段登录以及crate帐户字段show reuired validator,当我点击登录时,所有字段显示所需的验证者。 LINK



我尝试过:



这是我的主页代码

 <   label  >  
用户名< / label >
< asp:TextBox runat = 服务器 ID = user 占位符 = 输入您的用户名 > < / asp:TextBox >
< asp:RequiredFieldValidator runat = server ControlToValidate = user 错误orMessage = 需要用户名 ForeColor = 红色 < span class =code-keyword>> < / asp:RequiredFieldValidator < span class =code-keyword>>

< label >
密码
< asp:TextBox runat = server ID = pas 占位符 = ********************** > < / asp:TextBox >
< asp:RequiredFieldValidator runat = server ControlToValidate = pas ErrorMessage = 需要密码 ForeColor = 红色 > < / asp:RequiredFieldValidator >
< / label >
< / fieldset < span class =code-keyword>>

< fieldset class = 帐户操作 >
< < span class =code-leadattribute> asp:Button CssClass = btn runat = server 文字 = 登录 / >

解决方案

在该方案中使用 ValidationGroup 当您有多个按钮时实施验证。



创建一个验证组作为AccountCreateValidation并将其分配给仅创建帐户:

 <   label     for   =  name  > 名称<   / label  > ;  
< asp:textbox style = position:relative ;右:40px;宽度:200px runat = server id = 名称 拼写检查 = false 占位符 = shreee xmlns:asp = #unknown / >
< asp:requiredfieldvalidator id = RequiredFieldValidator3 runat = server validationgroup = AccountCreateValidation controltovalidate = 名称 errormessage = 名称必填 forecolor = 红色 xmlns:asp = #unknown > < / asp:requiredfieldvalidator >

< div >
< label = < span class =code-keyword> email > 电子邮件< / label >
< asp:textbox style = position:relative;右:40像素; width:200px; runat = server id < span class =code-keyword> = txtEmail 占位符 = abcd@mail.com xmlns:asp = #unknown / > ;
< asp:requiredfieldvalidator id = RequiredFieldValidator4 validationgroup = AccountCreateValidation runat = server controltovalidate = txtEmail errormessage = 需要电子邮件 forecolor = 红色 xmlns:asp = #unknown > < / asp:requiredfieldvalidator >
< asp:regularexpressionvalidator id = regexEmailValid validationgroup = AccountCreateValidation runat = server validationexpression = \w +([ - +。] \w +)* @ \ w +([ - 。] \ w +)* \.\w +([ - 。] \w +)* controltovalidate = txtEmail errormessage = 无效的电子邮件格式 forecolor = 红色 xmlns:asp = #unknown >
< / asp:regularexpressionvalidator >
< asp:按钮 cssclass = btn runat = server validationgroup = AccountCreateValidation text = 创建帐户 xmlns:asp = #unknown / >
< / div >



创建验证组LoginValidation并将其分配给仅帐户登录:

 <  标签 > 用户名<   / label  >  
< asp:textbox runat = server id = user 占位符 = 输入您的用户名 xmlns:asp = #unknown > < / asp:textbox >
< asp:requiredfieldvalidator runat = server validationgroup = LoginValidation < span class =code-attribute> controltovalidate = user errormessage = 需要用户名 forecolor = 红色 >
< / asp:requiredfieldvalidator >
< label >
密码
< asp:textbox runat = server id = pas 占位符 = ********************** xmlns:asp = #unknown > < / asp:文本框 >
< asp:requiredfieldvalidator runat = server validationgroup = LoginValidation controltovalidate = pas errormessage = 需要密码 forecolor = 红色 xmlns:asp = #unknown >
< / asp:requiredfieldvalidator >
< / label > ;

< asp:按钮 cssclass = btn runat = server validationgroup = LoginValidation text = 登录 / >


Hi/Hello!
I have 3pages aspx,one is master page and others are home and register page,when i open register page and click on "create account" then all the required field show required options,I check again and again validate control id is different for every text box.can any one help me.here is my code thanks
here is my register.aspx code

<label for="name">Name</label>
              <asp:TextBox style=" position:relative; right:40px; width:200px" runat="server" ID="name" spellcheck="false" placeholder="shreee" />
            
             <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="name" ErrorMessage="Name Required" ForeColor="Red"></asp:RequiredFieldValidator>
</div>


<div>
              <label for="email">Email</label>
              
<asp:TextBox   style=" position:relative; right:40px; width:200px;" runat="server" ID="txtEmail" placeholder="abcd@mail.com"/> 
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="txtEmail" ErrorMessage="Email required" ForeColor="Red"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="regexEmailValid" runat="server" ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" ControlToValidate="txtEmail" ErrorMessage="Invalid Email Format" ForeColor="Red"></asp:RegularExpressionValidator>


and here is my error screenshot,when i click create account then all the text field login and as well as crate account field show reuired validator and when i click login then all field show required validtor.LINK

What I have tried:

Here is my master page code

<label>
      Username  </label>
     <asp:TextBox runat="server" ID="user" placeholder="Enter your username"></asp:TextBox>
      <asp:RequiredFieldValidator runat="server" ControlToValidate="user" ErrorMessage="Username required" ForeColor="Red"></asp:RequiredFieldValidator>
  
    <label>
      Password
    <asp:TextBox runat="server" ID="pas" placeholder="**********************"></asp:TextBox>
     <asp:RequiredFieldValidator runat="server" ControlToValidate="pas" ErrorMessage="Password required" ForeColor="Red"></asp:RequiredFieldValidator>
    </label>
  </fieldset>
  <fieldset class="account-action">
   <asp:Button CssClass="btn" runat="server" Text="Login" />

解决方案

In that scenario use ValidationGroup to implement validation when you have multiple buttons.

Create a validation group as "AccountCreateValidation" and assign it to only account creation:

<label for="name">Name</label>
<asp:textbox style=" position:relative; right:40px; width:200px" runat="server" id="name" spellcheck="false" placeholder="shreee" xmlns:asp="#unknown" />
<asp:requiredfieldvalidator id="RequiredFieldValidator3" runat="server" validationgroup="AccountCreateValidation" controltovalidate="name" errormessage="Name Required" forecolor="Red" xmlns:asp="#unknown"></asp:requiredfieldvalidator>

<div>
<label for="email">Email</label>
<asp:textbox style=" position:relative; right:40px; width:200px;" runat="server" id="txtEmail" placeholder="abcd@mail.com" xmlns:asp="#unknown" /> 
<asp:requiredfieldvalidator id="RequiredFieldValidator4" validationgroup="AccountCreateValidation" runat="server" controltovalidate="txtEmail" errormessage="Email required" forecolor="Red" xmlns:asp="#unknown"></asp:requiredfieldvalidator>
<asp:regularexpressionvalidator id="regexEmailValid" validationgroup="AccountCreateValidation" runat="server" validationexpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" controltovalidate="txtEmail" errormessage="Invalid Email Format" forecolor="Red" xmlns:asp="#unknown">
</asp:regularexpressionvalidator>
<asp:button cssclass="btn" runat="server" validationgroup="AccountCreateValidation" text="Create Account" xmlns:asp="#unknown" />
</div>


Create a validation group as "LoginValidation" and assign it to only account Login:

<label>Username</label>
<asp:textbox runat="server" id="user" placeholder="Enter your username" xmlns:asp="#unknown"></asp:textbox>
<asp:requiredfieldvalidator runat="server" validationgroup="LoginValidation" controltovalidate="user" errormessage="Username required" forecolor="Red">
</asp:requiredfieldvalidator>
<label>
Password
<asp:textbox runat="server" id="pas" placeholder="**********************" xmlns:asp="#unknown"></asp:textbox>
<asp:requiredfieldvalidator runat="server" validationgroup="LoginValidation" controltovalidate="pas" errormessage="Password required" forecolor="Red" xmlns:asp="#unknown">
</asp:requiredfieldvalidator>
</label>

<asp:button cssclass="btn" runat="server" validationgroup="LoginValidation" text="Login" />


这篇关于单击按钮时,页面中的所有文本字段都显示必需的错误(ASP.NET,C#(Web)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆