简体   繁体   English

如何在asp.net中使用验证将表单数据插入数据库?

[英]How to insert form data into a database with validations in asp.net?

I am trying to build a web application that requires users to register themselves. 我正在尝试构建一个需要用户注册自己的Web应用程序。 I added custom validators and other validators according to my need. 我根据需要添加了自定义验证器和其他验证器。

Part of the code in .aspx file .aspx文件中的部分代码

<form id="form" name="form" action="../Hi.aspx" method="post">
<table cellspacing="4" class="style1">
    <tr>
<td class="style4">
<asp:TextBox ID="TxtFirstName" runat="server" Width="157px"></asp:TextBox>
</td>
td class="style5" id="FName">
<asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="TxtFirstName" 
 ErrorMessage="Your First Name should be at least 2 characters long" 
 onservervalidate="CustomValidator1_ServerValidate" ForeColor="Red" 
 ValidateEmptyText="True"></asp:CustomValidator>...

and correspnding code in .aspx.cs file is 和.aspx.cs文件中的相应代码是

protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {
            args.IsValid = (args.Value.Length>1);
        }

This works fine when I run only this part of the application. 当我只运行应用程序的这一部分时,这很好用。

Now, I want to retrieve the values of all the text fields and store them in a database. 现在,我想检索所有文本字段的值并将它们存储在数据库中。

In aspx.cs file, i wrote code as 在aspx.cs文件中,我编写了代码

 protected void ButtonRegister_Click(object sender, EventArgs e)
 {
      string fname = TxtFirstName.Text;
      Controllers.RegistrationController r = new Controllers.RegistrationController();
      int a = r.registerData(fname);
      if (a==1) {
           Response.Redirect("../Hi.aspx");
      }
 }     

which is called when the submit button is clicked. 在单击提交按钮时调用。

The registerData() method in the RegistrationController that established connection with the database and stores the form values. RegistrationController中的registerData()方法,它与数据库建立连接并存储表单值。 The connection is established correctly and the values are retrieved and stored. 正确建立连接,并检索和存储值。 But, the problem is, when i call the registerData() method from the method ButtonRegister_Click , all the validation that I have written doesn't work. 但是,问题是,当我从方法ButtonRegister_Click调用registerData()方法时,我写的所有验证都不起作用。 Anything that is entered in the form gets stored into the database without validation. 在表单中输入的任何内容都将存储到数据库中而无需验证。

How do I retrieve the values and store them and at the same time ensure that they are being validated? 如何检索值并存储它们,同时确保它们得到验证?

I am new to .net, so any help is appreciated. 我是.net的新手,所以任何帮助都表示赞赏。

Thank you very much in advance. 非常感谢你提前。

You could call Page.Validate within your click method and check the result of that or specifying CausesValidation on your button should cause the valdation to run 您可以在单击方法中调用Page.Validate并检查其结果或在按钮上指定CausesValidation应该导致valdation运行

In the long term though you might want to look at moving the rules to a lower lower (ie. business logic) such that when/if you move to supporting services you wont have to reimplement the rules in those services, of course if you're not ever planning to do that getting away with them on the ui may suffice 从长远来看,您可能希望将规则移至较低的较低层(即业务逻辑),以便当您转移到支持服务时,您不必重新实现这些服务中的规则,当然,如果您'我不打算这样做,在ui上与他们一起离开可能就足够了

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

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