简体   繁体   English

ASP / C#TextBox验证

[英]ASP/C# TextBox Validation

I need to create a webform where users can add, update, delete, retrieve customer data from a table in an SQL database. 我需要创建一个Web表单,用户可以在其中添加,更新,删除,从SQL数据库中的表中检索客户数据。

Should have textboxes for each field in the table so that the users can enter details of the fields to update the table in the DB. 表中的每个字段都应具有文本框,以便用户可以输入字段的详细信息以更新数据库中的表。

What im having trouble with is in the code behind the form i need to make a clear method to clear all the textboxes and the message label. 即时通讯遇到的麻烦是在表单后面的代码中,我需要使用清晰的方法来清除所有文本框和消息标签。 I also need to set validation requirements for each textbox. 我还需要为每个文本框设置验证要求。 But i am unsure how to do this properly. 但我不确定如何正确执行此操作。

The textboxes are; 文本框是;

CustID, Firstname, Surname, Gender, Age, Address1, Address2, City, Phone, Mobile, Email, Confirm Email. 客户ID,名字,姓氏,性别,年龄,地址1,地址2,城市,电话,手机,电子邮件,确认电子邮件。

Now my main question is, how do i validate the textboxes? 现在我的主要问题是,如何验证文本框? For example; 例如; CustID is required. 必须提供CustID。 & Must be unique. &必须是唯一的。 Must be an integer and must be between 1 & 1000. 必须是整数,并且必须在1到1000之间。

You should use RequiredValidator for example 您应该使用RequiredValidator为例

http://www.w3schools.com/aspnet/control_reqfieldvalidator.asp http://www.w3schools.com/aspnet/control_reqfieldvalidator.asp

This will perform validation before submitting data to server ;) 这将在将数据提交到服务器之前执行验证;)

There are also other type of validator, like RangeValidator (for the need to check if the integer is between 1 and 1000). 验证器还有其他类型,例如RangeValidator(需要检查整数是否在1到1000之间)。

Example: 例:

<asp:RangeValidator ControlToValidate="youtField" MinimumValue="1" MaximumValue="1000" Type="Integer" Text="The field must be between 1 and 1000" runat="server" />

You can also add a ValidationGroup="save" for example to all your validators and to the button that user should click to save and update data. 例如,您还可以将ValidationGroup =“ save”添加到所有验证器以及用户应单击以保存和更新数据的按钮。

Asp.net Have some (5 main types) server validation control's , You can use the validations for your requirement Asp.net具有一些(5种主要类型)服务器验证控件,您可以根据需要使用验证

See this image for understanding validation controls (The image referred from ) 请参阅此图像以了解验证控件(图像来自

在此处输入图片说明

More understanding by this MSDN sit3 对此MSDN sit3的更多理解

and here is link for all validation control sample's : click me 这是所有验证控件样本的链接: 单击我

To clear all textbox you can try something like this 要清除所有文本框,您可以尝试这样的操作

foreach (var item in Page.Controls)
    {
        if (item is TextBox)
        {
            ((TextBox)item).Text = "";
        }
        if (item is DropDownList)
        {
            ((DropDownList)item).SelectedIndex= 0;
        }
        //and the other types
    }

For the part of validation you have to set the validation fields that you desire and bind it to the field directly on the .aspx page 对于验证,您必须设置所需的验证字段并将其直接绑定到.aspx页上的字段。

<asp:textbox ID="Name" runat="server" TabIndex="1"/>
<asp:RequiredFieldValidator ID="rfvName" ControlToValidate="Name" runat="server" ErrorMessage="Name is required">
<asp:ValidationSummary ID="ValidationSummary1" runat="server" DisplayMode ="BulletList" ShowSummary ="true" HeaderText="Errors:" />

When you try to save and one of the condition of your validators returns false, the validation summary will show all the errors written in the errormessage attribute. 当您尝试保存并且验证器的条件之一返回false时,验证摘要将显示在errormessage属性中写入的所有错误。

Here could be an example for ASP/ MVC - because you have forgotten to specify which technology from ASP. 这可能是ASP / MVC的示例-因为您忘记了从ASP中指定哪种技术。 Forms or MVC ?!? 表格还是MVC?!? This bellow aplies to mvc and the other attributes are allready defined by the users before me. 此波纹适用于mvc,其他属性已由我之前的用户定义。

Note that the RemoteAttribute can verify a function (validation function) . 注意,RemoteAttribute可以验证功能(验证功能)。

 namespace ModelValidation.Models {
 public class Appointment {
  [Required]
  [StringLength(10, MinimumLength = 3)]
  public string ClientName { get; set; }
  [DataType(DataType.Date)]
  [Remote("ValidateDate", "Home")]
  public DateTime Date { get; set; }
  public bool TermsAccepted { get; set; }
 }
}

To apply validation on a model property that describes a TextBox, then a good proactice is to use TextBoxFor<>(). 要将验证应用于描述TextBox的模型属性,那么一个好的做法是使用TextBoxFor <>()。 Like that: 像那样:

@Html.TextBoxFor((model) => model.ClientName )

You can clear all your controls values by either redirecting to user to another page telling him that form is submitted, New Registration button to redirect user again to Registration page, However if you don't want that you can pick up each control and reset them either in Cs.file or using javascript, 您可以通过以下方式清除所有控件的值:将用户重定向到另一个页面,告诉他表单已提交,单击“ New Registration按钮将用户再次重定向到“注册”页面,但是,如果您不希望您可以选择每个控件并将其重置,在Cs.file或使用javascript中,

foreach (Control ctrl in form1.Controls)
        {
            if (ctrl is TextBox)
            {
                TextBox tb = (TextBox)ctrl;
                tb.Text = string.Empty;
            }
            else if (ctrl is DropDownList)
            {
                DropDownList dl = (DropDownList)ctrl;
                dl.SelectedIndex = 0;
            }
            else if (ctrl is CheckBox)
            {
                CheckBox cb = (CheckBox)ctrl;
                cb.Checked = false;
            }
        }

For your validation purpose i strongly suggest you to read about validation in Asp.net , here is a good tutorial you can learn from here 为了您的验证目的,我强烈建议您阅读Asp.net中的validation ,这是一个很好的教程,您可以从这里学习

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM