requiredFieldValidator 无法在 asp.net 中工作。

编程

[ad_1]

嗨,朋友们..我在 ASP.NET 中遇到了一个问题,请帮助我..

我正在开发一个注册页面。当我提交数据时..数据被插入到数据库中,甚至空值也被插入..但我设置数据库中不允许空值。而且我也添加了验证但这些都不能正常工作..请帮助我..

我的代码是:

C#
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();
            }
        }

我的设计页面代码是:

网络平台
<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>

解决方案1

有两件事,第一件事应该解决你的问题。

1)您需要将ValidationGroup添加到您的Web控件中,例如:

2) 提交表单时最好使用 Page.IsValid 方法,因为它会强制验证控件也在服务器端运行。

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

解决方案2

你的问题有几种可能
1.你应该检查一下 页面是否有效 在单击按钮执行代码之前。
2. 如果您使用“asp:Button”和 .CS 文件作为后台代码,那么您应该触发 Serverclick 事件

[ad_2]

コメント

标题和URL已复制