服务器端customvalidator只是不工作! [英] Server side customvalidator just not working!!

查看:94
本文介绍了服务器端customvalidator只是不工作!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在办理登记表格。首先我尝试使用RequiredfieldValidators,但后来我的CustomValidators不起作用。即使不需要触发RequiredFieldValidators也不行。所以我决定使用所有CustomValidators。服务器端。如果每次验证都成功,那么计数器将会增加。我打算根据计数器的值更改按钮单击事件。现在按钮点击事件没有做任何工作,因为如果它,那么页面被重定向而不触发验证!但现在问题是即使我没有将文本框保持为空或具有默认值,验证也会失败。请帮忙。任何帮助表示赞赏。请原谅这是一个非常愚蠢的问题。我是初学者...



我的aspx页面:



I am working on a registration form. First I tried using RequiredfieldValidators but then my CustomValidators were just not working. Not even if the RequiredFieldValidators didn't need to be fired. So I decided to use all CustomValidators. server side. If each validation is succesful then a counter will increase. I plan to change the button click event based on the value of counter. Now button click event is not doing any work because if it does, then the page is being redirected without firing validations!!! But now the problem is the validation is failing even if I don't keep my textbox empty or having default values. Please help. Any help is appreciated. Please excuse if this is a very stupid question. I am a beginner...

My aspx page :

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="reg.aspx.cs" Inherits="Aptitude.reg" Theme="Skin1"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        #form1
        {
            height: 1428px;
        }

        #Password1
        {
            padding-left: 5px;
      outline-color: #CCFFFF;
    z-index: 1;

    position: absolute;
    width: 246px;
    height: 28px;
            left: 382px;
            top: 698px;
            position: absolute;
            width: 328px;
            height: 28px;

            -webkit-border-radius: 5px;
border-radius: 5px;
-moz-border-radius: 5px;


-webkit-box-shadow: 5px 5px 3px #888;
box-shadow: 2px 2px 2px #888;
-moz-box-shadow: 5px 5px 3px #888;

padding-left: 5px;
        }
 #b1
 {
   -webkit-border-radius: 5px;
border-radius: 5px;
-moz-border-radius: 5px;


-webkit-box-shadow: 5px 5px 3px #888;
box-shadow: 2px 2px 2px #888;
-moz-box-shadow: 5px 5px 3px #888;
 }
        #Select1
        {
            z-index: 1;
            left: 427px;
            top: 1411px;
            position: absolute;
            height: 15px;
            width: 283px;
        }


    </style>

































































</head>
<body style="height: 1428px; margin-right: 0px;">
    <form id="form1" runat="server">

    <asp:TextBox ID="add_1" runat="server" CssClass="t" AutoCompleteType="HomeStreetAddress"

        CausesValidation="True"





        style="z-index: 1; left: 382px; top: 763px; position: absolute; height: 28px; width: 328px"></asp:TextBox>

    <asp:Label ID="father_lbl" runat="server" class="1"



        style="z-index: 1; left: 207px; top: 425px; position: absolute; width: 121px;"

        Text="Father's Name" ForeColor="#9933FF" Font-Bold="True"

        Font-Size="Large"></asp:Label>

    <asp:TextBox ID="lname_txt" runat="server" CssClass="t"







        style="z-index: 1; position: absolute; top: 360px; width: 154px; height: 28px; left: 556px; right: 186px;"

        AutoCompleteType="LastName" CausesValidation="True">Last Name</asp:TextBox>
    <asp:TextBox ID="fname_txt" runat="server" CssClass="t"







        style="z-index: 1; position: absolute; top: 361px; width: 143px; height: 28px; left: 382px; right: 376px;"

        AutoCompleteType="FirstName" CausesValidation="True" >First Name</asp:TextBox>
     <asp:CustomValidator ID="CustomValidator1" runat="server"

        ErrorMessage="Please provide your full name" Font-Bold="True" ForeColor="#CC0000" ClientValidationFunction="vc" OnServerValidate="vs" EnableClientScript="false" ValidateEmptyText="true"



        style="z-index: 1; left: 737px; top: 364px; position: absolute; width: 202px;"></asp:CustomValidator>

    <asp:Label ID="name_lbl" runat="server" class="1" BorderColor="White" Font-Bold="True"

        Font-Names="Times New Roman" Font-Size="Large" ForeColor="#9933FF"

        style="z-index: 1; left: 207px; top: 367px; position: absolute; height: 23px; width: 70px; margin-bottom: 2px"

        Text="Name"></asp:Label>





    <asp:TextBox ID="ff_name" runat="server" CssClass="t"





        style="z-index: 1; left: 382px; top: 421px; position: absolute; width: 143px; height:28px; "

        AutoCompleteType="FirstName" CausesValidation="True">First Name</asp:TextBox>
    <asp:TextBox ID="fl_name" runat="server" CssClass="t"



        style="z-index: 1; left: 556px; top: 419px; position: absolute; width: 154px; height:28px; "

        AutoCompleteType="LastName" CausesValidation="True">Last Name</asp:TextBox>





    <asp:Label ID="email_lbl" runat="server"



        style="z-index: 1; left: 207px; top: 637px; position: absolute; width: 83px; height: 22px;"

        Text="Email ID" Font-Bold="True" Font-Size="Large" ForeColor="#9933FF"></asp:Label>





    <asp:TextBox ID="email_txt" runat="server" CssClass="t"



        style="z-index: 1; left: 382px; top: 633px; position: absolute; height: 28px; width: 328px;"

        AutoCompleteType="Email" CausesValidation="True">Email ID must be valid</asp:TextBox>





    <asp:Label ID="Label4" runat="server" Font-Bold="True" ForeColor="#CC0000"

        style="z-index: 1; left: 735px; top: 637px; position: absolute; height: 19px; width: 218px"

        Visible="False"></asp:Label>





    &nbsp;<asp:TextBox ID="add_2" runat="server" AutoCompleteType="HomeStreetAddress"

        CausesValidation="True"







        style="z-index: 1; left: 382px; top: 1006px; position: absolute; height: 28px; width: 328px"></asp:TextBox>
    <asp:Label ID="state_lbl" runat="server" Font-Bold="True" Font-Size="Large"

        ForeColor="#9933FF"

        style="z-index: 1; left: 207px; top: 889px; position: absolute; height: 20px"

        Text="State"></asp:Label>
    <asp:TextBox ID="state_txt" runat="server" CssClass="t"







        style="z-index: 1; left: 382px; top: 886px; position: absolute; height: 28px; width: 128px;"></asp:TextBox>
        <asp:TextBox ID="zip1_txt" runat="server"









        style="z-index: 1; left: 382px; top: 1182px; position: absolute; height: 28px; width: 128px;"></asp:TextBox>
    <asp:Label ID="zip1_lbl" runat="server" Font-Bold="True" Font-Size="Large"

        ForeColor="#9933FF"

        style="z-index: 1; left: 207px; top: 1187px; position: absolute"

        Text="ZIP"></asp:Label>
    <asp:TextBox ID="state1_txt" runat="server" CssClass="t"









        style="z-index: 1; left: 383px; top: 1127px; position: absolute; height: 28px; width: 128px"></asp:TextBox>
    <asp:Label ID="state1_lbl" runat="server" Font-Bold="True" Font-Size="Large"

        ForeColor="#9933FF"

        style="z-index: 1; left: 207px; top: 1131px; position: absolute"

        Text="State"></asp:Label>
    <asp:Label ID="zip_lbl" runat="server" Font-Bold="True" Font-Size="Large"

        ForeColor="#9933FF"



        style="z-index: 1; left: 207px; top: 951px; position: absolute; right: 662px;"

        Text="ZIP"></asp:Label>
    <asp:TextBox ID="zip_txt" runat="server" CssClass="t"







        style="z-index: 1; left: 382px; top: 946px; position: absolute; height: 28px; width: 128px;"></asp:TextBox>
    <asp:Label ID="num_lbl" runat="server" Font-Bold="True" Font-Size="Large"

        ForeColor="#9933FF"

        style="z-index: 1; left: 207px; top: 1241px; position: absolute; height: 22px"

        Text="Contact number"></asp:Label>
    <asp:Label ID="gender_lbl" runat="server" Font-Bold="True" Font-Size="Large"

        ForeColor="#9933FF"

        style="z-index: 1; left: 207px; top: 480px; position: absolute; right: 639px"

        Text="Gender"></asp:Label>




   <asp:RadioButtonList ID="rbl" runat="server"



        style="z-index: 1; left: 382px; top: 476px; position: absolute; width: 108px; right: 452px;" >
            <asp:ListItem>Male</asp:ListItem>
            <asp:ListItem>Female</asp:ListItem>
        </asp:RadioButtonList>









&nbsp;





    <asp:Label ID="dob_lbl" runat="server" Font-Bold="True" Font-Size="Large"

        ForeColor="#9933FF"

        style="z-index: 1; top: 553px; position: absolute; left:207px;"

        Text="Date Of Birth"></asp:Label>

        <asp:textbox id="tbMyDate" runat="server" style="z-index: 1; top: 550px; position: absolute; left:382px; height: 28px; width: 143px;"

        ></asp:textbox>

         &nbsp;&nbsp;


    <asp:textbox id="pwd_txt" runat="server" textmode="Password" xmlns:asp="*unknown" style="z-index: 1; top: 700px; position: absolute; left:382px; height: 28px; width: 143px;"

        ></asp:textbox>

    <asp:Label ID="add1_lbl" runat="server"

        Font-Bold="True" Font-Size="Large" ForeColor="#9933FF"

        style="z-index: 1; left: 207px; top: 769px; position: absolute"

        Text="Present Address"></asp:Label>












    <asp:Label ID="add2_lbl" runat="server" Font-Bold="True" Font-Size="Large"

        ForeColor="#9933FF"

        style="z-index: 1; left: 207px; top: 1011px; position: absolute"

        Text="Permanent Address"></asp:Label>
    <asp:Label ID="city_lbl" runat="server" Font-Bold="True" Font-Size="Large"

        ForeColor="#9933FF"

        style="z-index: 1; left: 207px; top: 826px; position: absolute; height: 26px; width: 44px"

        Text="City"></asp:Label>
        <asp:Label ID="city1_lbl" runat="server" Font-Bold="True" Font-Size="Large"

        ForeColor="#9933FF"

        style="z-index: 1; left: 207px; top: 1071px; position: absolute; height: 26px; width: 44px; bottom: 169px;"

        Text="City"></asp:Label>
    <asp:TextBox ID="city_txt" runat="server" CssClass="t"





        style="z-index: 1; left: 382px; top: 824px; position: absolute; height: 28px; width: 128px;"></asp:TextBox>
        <asp:TextBox ID="city1_txt" runat="server" CssClass="t"







        style="z-index: 1; left: 382px; top: 1069px; position: absolute; height: 28px; width: 128px;"></asp:TextBox>












    <asp:Label ID="pd_lbl" runat="server" Font-Bold="True" Font-Size="Large"

        Font-Underline="True"

        style="z-index: 1; left: 43px; top: 313px; position: absolute"

        Text="Personal Details:"></asp:Label>













    <asp:TextBox ID="num_txt" runat="server" AutoCompleteType="Cellular" CssClass="t"

        CausesValidation="True"









        style="z-index: 1; left: 382px; top: 1236px; position: absolute; height: 28px; width: 328px"></asp:TextBox>












    <asp:Label ID="pwd_lbl" runat="server" Font-Bold="True" Font-Size="Large"

        ForeColor="#9933FF"



        style="z-index: 1; left: 207px; top: 701px; position: absolute; height: 23px;"

        Text="Password"></asp:Label>













    <asp:Button ID="b1" runat="server" BackColor="#9966FF" Font-Bold="True"

        Font-Italic="False" Font-Size="Larger" ForeColor="White" OnClick="b1_Click"

        style="z-index: 1; left: 629px; top: 1348px; position: absolute; width: 136px; height: 37px"

        Text="Next" />












    <asp:Label ID="Label2" runat="server" Font-Bold="True" ForeColor="#CC0000"

        style="z-index: 1; left: 737px; top: 364px; position: absolute; width: 224px"

        Visible="False"></asp:Label>
    <asp:Label ID="Label3" runat="server" Font-Bold="True" ForeColor="#CC0000"

        style="z-index: 1; left: 737px; top: 422px; position: absolute; width: 218px"

        Visible="False"></asp:Label>












 <%--   <asp:CustomValidator ID="RequiredFieldValidator1" runat="server"
        ErrorMessage="RequiredFieldValidator" Font-Bold="True" ForeColor="#CC0000" OnServerValidate="vs1"

        style="z-index: 1; left: 735px; top: 550px; position: absolute; width: 230px"
        >Please provide your date of birth</asp:CustomValidator>



  <%--  <asp:CustomValidator ID="RequiredFieldValidator2" runat="server"
        ErrorMessage="RequiredFieldValidator" Font-Bold="True" ForeColor="#CC0000" OnServerValidate="vs"

        style="z-index: 1; left: 735px; top: 1185px; position: absolute; width: 230px; height: 22px;"
        ControlToValidate="zip1_txt">Please provide the ZIP code</asp:CustomValidator>


    <asp:CustomValidator ID="RequiredFieldValidator3" runat="server"
        ErrorMessage="RequiredFieldValidator" Font-Bold="True" ForeColor="#CC0000" OnServerValidate="vs"

        style="z-index: 1; left: 735px; top: 767px; position: absolute; width: 254px"
        ControlToValidate="add_1">Please provide your present address</asp:CustomValidator>


  <%--  <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server"
        ErrorMessage="RequiredFieldValidator" Font-Bold="True" ForeColor="#CC0000"

        style="z-index: 1; left: 734px; top: 829px; position: absolute; width: 261px"
        ControlToValidate="city_txt">Please provide your city's name</asp:RequiredFieldValidator>




    <asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server"
            ControlToValidate="rbl"
        ErrorMessage="Please provide your gender"
        style="z-index: 1; left: 735px; top: 484px; position: absolute; width: 261px"
        Font-Bold="True" ForeColor="#CC0000" ></asp:RequiredFieldValidator>

         <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server"
            ControlToValidate="state_txt"
        ErrorMessage="Please provide your state's name"
        style="z-index: 1; left: 735px; top: 891px; position: absolute; width: 261px"
        Font-Bold="True" ForeColor="#CC0000" ></asp:RequiredFieldValidator>

        <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server"
            ControlToValidate="zip_txt"
        ErrorMessage="Please provide the ZIP code"
        style="z-index: 1; left: 735px; top: 950px; position: absolute; width: 261px"
        Font-Bold="True" ForeColor="#CC0000" ></asp:RequiredFieldValidator>

        <asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server"
            ControlToValidate="add_2"
        ErrorMessage="Please provide your permanent address"
        style="z-index: 1; left: 734px; top: 1011px; position: absolute; width: 277px"
        Font-Bold="True" ForeColor="#CC0000" ></asp:RequiredFieldValidator>

        <asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server"
        ErrorMessage="RequiredFieldValidator" Font-Bold="True" ForeColor="#CC0000"

        style="z-index: 1; left: 734px; top: 1075px; position: absolute; width: 261px"
        ControlToValidate="city1_txt">Please provide the city's name</asp:RequiredFieldValidator>

        <asp:RequiredFieldValidator ID="RequiredFieldValidator10" runat="server"
            ControlToValidate="state_txt"
        ErrorMessage="Please provide the state's name"
        style="z-index: 1; left: 735px; top: 1127px; position: absolute; width: 261px"
        Font-Bold="True" ForeColor="#CC0000" ></asp:RequiredFieldValidator>

         <asp:RequiredFieldValidator ID="RequiredFieldValidator11" runat="server"
            ControlToValidate="num_txt"
        ErrorMessage="Please provide your contact number"
        style="z-index: 1; left: 735px; top: 1240px; position: absolute; width: 261px"
        Font-Bold="True" ForeColor="#CC0000" ></asp:RequiredFieldValidator>



         <asp:Label
        ID="Label1" runat="server"
        style="z-index: 1; left: 735px; top: 705px; position: absolute; width: 246px;"
        Visible="False" Font-Bold="True" ForeColor="#CC0000"></asp:Label> --%>

    </form>
    <script type="text/javascript">
        function vc(source, args) {


            if (fname_txt.Text == "First Name" ||
                lname_txt.Text == "Last Name"
                ) {
                document.write("working");
                args.IsValid = false;
            }

        }

       
</script>
</body>
</html>





My axps.cs page:





My axps.cs page:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Aptitude
{
    public partial class reg : System.Web.UI.Page
    {
        int count=0;
        protected void Page_Load(object sender, EventArgs e)
        {
            //this.rbl.RepeatDirection = RepeatDirection.Horizontal;
        }
     protected void vs(
    object source, ServerValidateEventArgs args)
        {
            if (fname_txt.Text == "First Name" || fname_txt.Text.Trim().Contains("") ||
                lname_txt.Text == "Last Name" || lname_txt.Text.Trim().Contains("")


                )
            {
                Response.Write(fname_txt.Text);
                Response.Write(fname_txt.Text.Trim());
                args.IsValid = true;
            }
            else
            {
                count++;
            }

        }
   /*  protected void vs1(
 object source, ServerValidateEventArgs args)
     {
         if (tbMyDate.Text.Trim().Contains("") )
         {
             args.IsValid = false;
         }
         else
         {
             count++;
         }

     }
*/

       protected void b1_Click(object sender, EventArgs e)
        {
          
            Response.Write(count);

        }


    }

}









In response to the debugging statements in vs function its showing first name as what i enter. Suppose Anne. So its not supposed to show error message....





In response to the debugging statements in vs function its showing first name as what i enter. Suppose Anne. So its not supposed to show error message....

推荐答案

i think you have left to add controltoValidate property of customValodator 



//actually i am unable to got your query please explain more details

//waiting for reply


//actually i am unable to got your query please explain more details
//waiting for reply


这篇关于服务器端customvalidator只是不工作!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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