RequiredFieldValidator在asp.net中不起作用。 [英] RequiredFieldValidator is not working in asp.net.

查看:66
本文介绍了RequiredFieldValidator在asp.net中不起作用。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友们..我在asp.net中遇到问题请帮帮我..



i我开发一个注册页面。当我提交数据时..data插入数据库甚至空值也..但我设置数据库中不允许空值。我也添加验证但这些都没有正常工作..请帮助我..



我的代码是:

hi friends ..i got aone problem in asp.net please help me..

i am develop a signup page .in that when i am submit the data ..data was inserted into database even null values also ..but i am set the null values are not allowed in the database .and also i am adding the validations also but these are not worked properly ..please help me..

my code is:

protected void btn_r_submit_Click(object sender, EventArgs e)
    {

               con.Open();
        string strid = "select count(*) from tbl_user_register";
        SqlCommand cmdid = new SqlCommand(strid, con);
        int count = Convert.ToInt16(cmdid.ExecuteScalar()) + 1;
        string struserid = count.ToString();
        

        string strquery = "insert into tbl_user_register values('"+struserid+"','" + txt_fname.Text + "','" + txt_dob.Text + "','" + txt_r_loginid.Text + "','" + txt_r_pwd.Text + "','" + txt_r_confirmpwd + "','" + txt_r_email.Text + "','" + txt_r_mobile.Text + "')";

        SqlCommand cmdregister = new SqlCommand(strquery, con);
        if (cmdregister.ExecuteNonQuery()>0)
        {

            Response.Write("<Script>alert('Successfully Registered in GVK Academy..')</Script>");         
      
            con.Close();
        }

        else
        {
            try
            {

                con.Open();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
                //return false;

                con.Close();
            }
        }







我的设计页面代码是:




my design page code is:

<div id="div_signup">
                      <div id="div_sigbg">
                          <table class="auto-style1">
                              <tr>
                                  <td colspan="2">&nbsp;</td>
                              </tr>
                              <tr>
                                  <td colspan="2" style="background-color: #006699; color: #FFFFFF; text-align: center; font-weight: bold; height:30px; font-size: 25px; font-family: 'Courier New', Courier, monospace;">Student Registration</td>
                              </tr>
                              <tr>
                                  <td class="td_regist">Full Name :</td>
                                  <td class="auto-style4">
                                      <asp:TextBox ID="txt_fname" CssClass="txt_register" runat="server"></asp:TextBox>
                                      <asp:RequiredFieldValidator ID="RequiredFieldValidator1" SetFocusOnError="true" ControlToValidate="txt_fname" ValidationGroup="register" runat="server" ErrorMessage="*" ForeColor="#ff0000" Font-Bold="true"></asp:RequiredFieldValidator>
                                  </td>
                              </tr>
                              <tr>
                                  <td class="td_regist">Date Of Birth :</td>
                                  <td>
                                      <asp:TextBox ID="txt_dob" CssClass="txt_register" TextMode="Date" runat="server"></asp:TextBox>
                                      <asp:RequiredFieldValidator ID="RequiredFieldValidator2" SetFocusOnError="true" ControlToValidate="txt_dob" ValidationGroup="register" runat="server" ErrorMessage="*" ForeColor="#ff0000" Font-Bold="true"></asp:RequiredFieldValidator>
                                  </td>
                              </tr>
                              <tr>
                                  <td class="td_regist">Login ID :</td>
                                  <td>
                                      <asp:TextBox ID="txt_r_loginid" runat="server" CssClass="txt_register" ></asp:TextBox>
                                      <asp:RequiredFieldValidator ID="RequiredFieldValidator3"  SetFocusOnError="true" ControlToValidate="txt_r_loginid" ValidationGroup="register" runat="server" ErrorMessage="*" ForeColor="#ff0000" Font-Bold="true"></asp:RequiredFieldValidator>
                                  </td>
                              </tr>
                              <tr>
                                  <td class="td_regist">Password :</td>
                                  <td>
                                      <asp:TextBox ID="txt_r_pwd" runat="server"  TextMode="Password" CssClass="txt_register"></asp:TextBox>

                                      <asp:RequiredFieldValidator ID="RequiredFieldValidator4"  SetFocusOnError="true" ControlToValidate="txt_r_pwd" ValidationGroup="register" runat="server" ErrorMessage="*" ForeColor="#ff0000" Font-Bold="true"></asp:RequiredFieldValidator>

                                  </td>
                              </tr>
                              <tr>
                                  <td class="td_regist">Confirm Passsword :</td>
                                  <td>
                                      <asp:TextBox ID="txt_r_confirmpwd" runat="server"  TextMode="Password" CssClass="txt_register"></asp:TextBox>

                                      <asp:RequiredFieldValidator ID="RequiredFieldValidator9"  SetFocusOnError="true" ControlToValidate="txt_r_confirmpwd" ValidationGroup="register" runat="server" ErrorMessage="*" ForeColor="#ff0000" Font-Bold="true"></asp:RequiredFieldValidator>
                                      &nbsp;<asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="txt_r_pwd" ControlToValidate="txt_r_confirmpwd" ForeColor="#ff0000" ErrorMessage="Password not matched"></asp:CompareValidator>
                                  </td>
                              </tr>
                              <tr>
                                  <td class="td_regist" >E-Mail Id :</td>
                                  <td>
                                      <asp:TextBox ID="txt_r_email" runat="server" CssClass="txt_register"></asp:TextBox>
                                      <asp:RequiredFieldValidator ID="RequiredFieldValidator5"  SetFocusOnError="true" ControlToValidate="txt_r_email" ValidationGroup="register" runat="server" ErrorMessage="*" ForeColor="#ff0000" Font-Bold="true"></asp:RequiredFieldValidator>
                                      &nbsp;&nbsp;&nbsp;
                                      <asp:RegularExpressionValidator ID="RegularExpressionValidator1"  SetFocusOnError="true" runat="server" ForeColor="#ff0000" ControlToValidate="txt_r_email" ErrorMessage="Invaild email ID" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
                                  </td>
                              </tr>
                              <tr>
                                  <td class="td_regist">Mobile No :</td>
                                  <td>
                                      <asp:TextBox ID="txt_r_mobile" runat="server" CssClass="txt_register"></asp:TextBox>
                                      <asp:RequiredFieldValidator ID="RequiredFieldValidator6"  SetFocusOnError="true" ControlToValidate="txt_r_mobile" ValidationGroup="register" runat="server" ErrorMessage="*" ForeColor="#ff0000" Font-Bold="true"></asp:RequiredFieldValidator>
                                      &nbsp;&nbsp;&nbsp;
                                      <asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server"  SetFocusOnError="true"  ControlToValidate="txt_r_mobile" ErrorMessage="Invalid Mobi no" ForeColor="Red" ValidationExpression="[789][0-9]{9}" ValidationGroup="register"></asp:RegularExpressionValidator>
                                  </td>
                              </tr>
                              <tr>
                                  <td>&nbsp;</td>
                                  <td>
                                      <asp:Label ID="lbl_registermessage" Visible="false" runat="server" ForeColor="#006600" Text="Successfully Registration Complete..."></asp:Label>
                                  </td>
                              </tr>
                              <tr>
                                  <td colspan="2" style="text-align:center">
                                      <asp:Button ID="btn_r_submit"  CssClass="btn_register" runat="server"   ValidationGroup="register" Text="Submit" OnClick="btn_r_submit_Click" ValidateRequestMode="Enabled" />
                                      &nbsp;
                                      <asp:Button ID="btn_r_clear"   CssClass="btn_register"  runat="server"  ValidationGroup="register" Text="Clear" OnClick="btn_r_clear_Click" />
                                  </td>
                              </tr>
                              <tr>
                                  <td colspan="2" style="text-align:center">
                                      &nbsp;</td>
                              </tr>
                          </table>
                      </div>
                  </div>

推荐答案

Two things, the first should fix your problem.



1) You need to add ValidationGroup to your web controls, such as: <asp:TextBox ID=\"txt_r_confirmpwd\" runat=\"server\" ValidationGroup=\"register\" TextMode=\"Password\" CssClass=\"txt_register\">



2) It is best practice to use the Page.IsValid method when submitting a form, because it will force the validation controls to run on the server side too.



Page.Validate();
$b$ b if (!Page.IsValid)

{

Literal1.Text = \"* Form is incomplete\";

}

else

{

//insert data

}
Two things, the first should fix your problem.

1) You need to add ValidationGroup to your web controls, such as: <asp:TextBox ID="txt_r_confirmpwd" runat="server" ValidationGroup="register" TextMode="Password" CssClass="txt_register">

2) It is best practice to use the Page.IsValid method when submitting a form, because it will force the validation controls to run on the server side too.

Page.Validate();
if (!Page.IsValid)
{
Literal1.Text = "* Form is incomplete";
}
else
{
//insert data
}


There are couple of possibility of your problem

1. You should check Page.IsValid before code execution on button click.

2. if you are using ’asp:Button’ and .CS file for code behind then you should fire Serverclick event
There are couple of possibility of your problem
1. You should check Page.IsValid before code execution on button click.
2. if you are using 'asp:Button' and .CS file for code behind then you should fire Serverclick event


这篇关于RequiredFieldValidator在asp.net中不起作用。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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