如何控制ASP.NET验证程序控件客户端验证 [英] How to control ASP.NET Validator Controls Client Side validation

查看:222
本文介绍了如何控制ASP.NET验证程序控件客户端验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的页面的输出它运行低于code后:

我用在我的处境 ValidatorEnable 客户端验证(如果你知道更好的方式来做到这一点,让我知道。)

我的问题是:

IHAVE与复选框,并彼此相邻一个DropDownList的GridView如图所示的屏幕截图。

所以,如果用户在选中复选框它使DropDownList的距离来选择。
如果用户点击提交按钮它验证,并要求用户选择从DropDownList中的任何选项。

问题:

但问题是:它的所有行,而不是一个我点击确认

如何可以强制验证该特定行的用户的复选框选中?

 < ASP:GridView控件ID =GV=服务器的AutoGenerateColumns =FALSE的DataKeyNames =IDOnRowDataBound =gv_RowDataBound
            EnableModelValidation =真>
            <柱体和GT;
                < ASP:BoundField的数据字段=IDControlStyle-WIDTH =250像素的HeaderText =IDSORTEX pression =ID/>
                < ASP:BoundField的数据字段=姓ControlStyle-WIDTH =250像素的HeaderText =姓
                    SORTEX pression =名字/>
                < ASP:BoundField的数据字段=姓氏ControlStyle-WIDTH =250像素的HeaderText =姓氏
                    SORTEX pression =姓氏/>
                < ASP:的TemplateField>
                    <&ItemTemplate中GT;
                        < ASP:复选框ID =checkbox1=服务器/>
                        < ASP:DropDownList的ID =ddl_PaymentMethod=服务器>
                                    < ASP:ListItem的值= - 1> ----< / ASP:ListItem的>
                                    < ASP:ListItem的值=0>当月< / ASP:ListItem的>
                                    < ASP:ListItem的值=1>对于结束< / ASP:ListItem的>
                                    < ASP:ListItem的值=2>对于旅游与LT; / ASP:ListItem的>
                                < / ASP:DropDownList的>
                        < ASP:的RequiredFieldValidator ID =requiredDDL=服务器的ControlToValidate =ddl_PaymentMethod
                            的ErrorMessage =请选择与InitialValue = - 1显示=动态>< / ASP:&的RequiredFieldValidator GT;
                    < / ItemTemplate中>
                < / ASP:的TemplateField>
                < ASP:的TemplateField的HeaderText =值>
                    <&ItemTemplate中GT;
                        < ASP:文本框ID =txt_Value=服务器WIDTH =58px文本=0>< / ASP:文本框>
                    < / ItemTemplate中>
                < / ASP:的TemplateField>
            < /专栏>
        < / ASP:GridView的>
保护无效gv_RowDataBound(对象发件人,GridViewRowEventArgs E)
{
 如果(e.Row.RowType == DataControlRowType.DataRow)
 {
    复选框checkbox1 =(复选框)e.Row.FindControl(checkbox1);
    DropDownList的ddl_PaymentMethod =(DropDownList的)e.Row.FindControl(ddl_PaymentMethod);
    ddl_PaymentMethod.Attributes.Add(禁用,已禁用);   checkbox1.Attributes.Add(的onclick,的javascript:EnableCheckBox('+ ddl_PaymentMethod.ClientID +','+ checkbox1.ClientID +'));
 }
}功能EnableCheckBox(DDL,CHK){  VAR ddl_PaymentMethod =的document.getElementById(DDL);
  VAR _chkbox =的document.getElementById(CHK);
  如果(_chkbox.checked){
      ddl_PaymentMethod.disabled = FALSE;
      validateCheckBox(ddl_PaymentMethod,真);
   }
   其他{
      ddl_PaymentMethod.disabled = TRUE;
      validateCheckBox(ddl_PaymentMethod,FALSE);
   }
}功能validateCheckBox(DDL,州){
    ValidatorEnable(的document.getElementById(ddl.id,状态));
}


解决方案

得到它为你工作?
GridView的:

 < ASP:GridView控件ID =GV=服务器的AutoGenerateColumns =FALSE的DataKeyNames =IDOnRowDataBound =gv_RowDataBound>
        <柱体和GT;
            < ASP:BoundField的数据字段=IDControlStyle-WIDTH =250像素的HeaderText =IDSORTEX pression =ID/>
            < ASP:BoundField的数据字段=姓ControlStyle-WIDTH =250像素的HeaderText =姓
                SORTEX pression =名字/>
            < ASP:BoundField的数据字段=姓氏ControlStyle-WIDTH =250像素的HeaderText =姓氏
                SORTEX pression =姓氏/>
            < ASP:的TemplateField>
                <&ItemTemplate中GT;
                    < ASP:复选框ID =checkbox1=服务器/>
                    < ASP:DropDownList的ID =drpPaymentMethod=服务器>
                                < ASP:ListItem的值= - 1> ----< / ASP:ListItem的>
                                < ASP:ListItem的值=0>当月< / ASP:ListItem的>
                                < ASP:ListItem的值=1>对于结束< / ASP:ListItem的>
                                < ASP:ListItem的值=2>对于旅游与LT; / ASP:ListItem的>
                            < / ASP:DropDownList的>
                    < ASP:的RequiredFieldValidator ID =RFV与InitialValue = - 1的ControlToValidate =drpPaymentMethod启用=假显示=静态=服务器的ErrorMessage =的RequiredFieldValidator>< / ASP:的RequiredFieldValidator>                < / ItemTemplate中>
            < / ASP:的TemplateField>
            < ASP:的TemplateField的HeaderText =值>
                <&ItemTemplate中GT;
                    < ASP:文本框ID =txt_Value=服务器WIDTH =58px文本=0>< / ASP:文本框>
                < / ItemTemplate中>
            < / ASP:的TemplateField>
        < /专栏>
    < / ASP:GridView的>

CS:

 保护无效gv_RowDataBound(对象发件人,GridViewRowEventArgs E)
    {
        如果(e.Row.RowType == DataControlRowType.DataRow)
        {
            复选框checkbox1 = e.Row.FindControl(checkbox1)的复选框;
            的RequiredFieldValidator RFV = e.Row.FindControl(RFV)作为的RequiredFieldValidator;
            DropDownList的drpPaymentMethod =(DropDownList的)e.Row.FindControl(drpPaymentMethod);
            //你可以通过本而不是myDiv.ClientID,并从DOM元素得到ID
            checkbox1.Attributes.Add(onclick事件,UpdateValidator('+ checkbox1.ClientID +,+ drpPaymentMethod.ClientID +,+ rfv.ClientID +'););
            如果(!checkbox1.Checked)
                drpPaymentMethod.Attributes.Add(禁用,已禁用);
        }
    }

JavaScript的:

 函数UpdateValidator(chkID,drpID,validatorid){
        //使只有复选框被选中的验证
        VAR enableValidator = $(#+ chkID)。是(:检查);        如果(enableValidator)
            $('#'+ drpID).removeAttr('禁用');
        其他
            $('#'+ drpID).attr('残疾','禁用');        变种VV = $('#'+ validatorid).VAL();        ValidatorEnable(的document.getElementById(validatorid),enableValidator);
    }

Below is the output of my page after it runs the below code:

I'm using ValidatorEnable client side validation in my situation (if you know better way to do that, let me know.)

My question is:

Ihave a gridview with checkbox and a dropdownlist next to each other as shown in the screen shot.

So, if the user checked on the checkbox it enabled the dropdownlist to select from. if the user clicks on submit button it validates and ask the user to select any options from the dropdownlist.

Problem:

But the problem is: its validating for all the rows instead of the one i have clicked.

How can i enforce to validate that particular row the user checked on the checkbox?

    <asp:GridView ID="gv" runat="server" AutoGenerateColumns="False" DataKeyNames="Id" OnRowDataBound="gv_RowDataBound"
            EnableModelValidation="True">
            <Columns>
                <asp:BoundField DataField="ID" ControlStyle-Width="250px" HeaderText="ID" SortExpression="ID" />
                <asp:BoundField DataField="FirstName" ControlStyle-Width="250px" HeaderText="FirstName"
                    SortExpression="FirstName" />
                <asp:BoundField DataField="LastName" ControlStyle-Width="250px" HeaderText="LastName"
                    SortExpression="LastName" />
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:CheckBox ID="checkbox1" runat="server" />
                        <asp:DropDownList ID="ddl_PaymentMethod" runat="server">
                                    <asp:ListItem Value="-1">----</asp:ListItem>
                                    <asp:ListItem Value="0">Month</asp:ListItem>
                                    <asp:ListItem Value="1">At End</asp:ListItem>
                                    <asp:ListItem Value="2">At Travel</asp:ListItem>
                                </asp:DropDownList>
                        <asp:RequiredFieldValidator ID="requiredDDL" runat="server" ControlToValidate="ddl_PaymentMethod"
                            ErrorMessage="Please select" InitialValue="-1" Display="Dynamic"></asp:RequiredFieldValidator>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Value">
                    <ItemTemplate>
                        <asp:TextBox ID="txt_Value" runat="server" Width="58px" Text="0"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>


protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
 if (e.Row.RowType == DataControlRowType.DataRow)
 {
    CheckBox checkbox1 = (CheckBox)e.Row.FindControl("checkbox1");
    DropDownList ddl_PaymentMethod = (DropDownList)e.Row.FindControl("ddl_PaymentMethod");
    ddl_PaymentMethod.Attributes.Add("disabled", "disabled");

   checkbox1.Attributes.Add("onclick", "javascript:EnableCheckBox('" + ddl_PaymentMethod.ClientID + "','" + checkbox1.ClientID + "')"); 
 }
}

function EnableCheckBox(ddl, chk) {

  var ddl_PaymentMethod = document.getElementById(ddl);
  var _chkbox = document.getElementById(chk);
  if (_chkbox.checked) {
      ddl_PaymentMethod.disabled = false;
      validateCheckBox(ddl_PaymentMethod, true);
   }
   else { 
      ddl_PaymentMethod.disabled = true;
      validateCheckBox(ddl_PaymentMethod, false);
   }
}

function validateCheckBox(ddl, state) {
    ValidatorEnable(document.getElementById(ddl.id, state));             
}

解决方案

got it working for you... gridview:

<asp:GridView ID="gv" runat="server" AutoGenerateColumns="False" DataKeyNames="Id"  OnRowDataBound="gv_RowDataBound">
        <Columns>
            <asp:BoundField DataField="ID" ControlStyle-Width="250px" HeaderText="ID" SortExpression="ID" />
            <asp:BoundField DataField="FirstName" ControlStyle-Width="250px" HeaderText="FirstName"
                SortExpression="FirstName" />
            <asp:BoundField DataField="LastName" ControlStyle-Width="250px" HeaderText="LastName"
                SortExpression="LastName" />
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:CheckBox ID="checkbox1" runat="server" />
                    <asp:DropDownList ID="drpPaymentMethod" runat="server">
                                <asp:ListItem Value="-1">----</asp:ListItem>
                                <asp:ListItem Value="0">Month</asp:ListItem>
                                <asp:ListItem Value="1">At End</asp:ListItem>
                                <asp:ListItem Value="2">At Travel</asp:ListItem>
                            </asp:DropDownList>
                    <asp:RequiredFieldValidator ID="rfv" InitialValue="-1" ControlToValidate="drpPaymentMethod" Enabled="false" Display="Static" runat="server" ErrorMessage="RequiredFieldValidator"></asp:RequiredFieldValidator>

                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Value">
                <ItemTemplate>
                    <asp:TextBox ID="txt_Value" runat="server" Width="58px" Text="0"></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

CS:

protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            CheckBox checkbox1 = e.Row.FindControl("checkbox1") as CheckBox;
            RequiredFieldValidator rfv = e.Row.FindControl("rfv") as RequiredFieldValidator;
            DropDownList drpPaymentMethod = (DropDownList)e.Row.FindControl("drpPaymentMethod");
            // you can just pass "this" instead of "myDiv.ClientID" and get the ID from the DOM element
            checkbox1.Attributes.Add("onclick", "UpdateValidator('" + checkbox1.ClientID + "','" + drpPaymentMethod.ClientID + "','" + rfv.ClientID + "');");
            if (!checkbox1.Checked)
                drpPaymentMethod.Attributes.Add("disabled", "disabled");
        }
    }

javascript:

function UpdateValidator(chkID, drpID, validatorid) {
        //enabling the validator only if the checkbox is checked
        var enableValidator = $("#" + chkID).is(":checked");

        if (enableValidator)
            $('#' + drpID).removeAttr('disabled');
        else
            $('#' + drpID).attr('disabled', 'disabled');

        var vv = $('#' + validatorid).val();

        ValidatorEnable(document.getElementById(validatorid), enableValidator);
    }

这篇关于如何控制ASP.NET验证程序控件客户端验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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