2

编辑:

var dropDownControls = $('#<%=GridView1.ClientID %> select option:selected');
var checkbox = $.......checkbox ..... 
  for(index = 0; index < dropDownControls.length; index++)
  {
      if (checkbox.checked) //my code gets exaclty what checkbox i checked
      {
      if(dropDownControls[index].selectedIndex == 0)
      {
          flag = false;
          break;
      }
      }
  }

上面的代码有效

我在gridivew 外面有我的按钮,我正在尝试验证gridivew 内部的下拉列表。

<asp:Button ID="btn" runat="server" Text="Submit" OnClick="btn_Click"  CausesValidation="true"/>


<asp:GridView ID="GVInputMapping" runat="server" AutoGenerateColumns="False" DataKeyNames="Id"
                             EnableModelValidation="True" onrowdatabound="GVInputMapping_RowDataBound">
<Columns>
<asp:BoundField DataField="Name" ControlStyle-Width="250px" HeaderText="Name" SortExpression="Name" />
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox id="checkbox1" runat="server"/>
<asp:DropDownList runat="server" ID="ddldetail">
<asp:ListItem Selected="True" Value="0">Select me</asp:ListItem>
<asp:ListItem Value="1">abc</asp:ListItem>
<asp:ListItem Value="2">GHt</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="requiredDDL" runat="server" 
              ControlToValidate="ddldetail" ErrorMessage="Please select" InitialValue="Select me"  Display="Dynamic"></asp:RequiredFieldValidator>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
4

3 回答 3

1

RequiredFieldValidator's设置InitialValue0而不是“选择我”,因为默认选择的项目的值为 0

<asp:ListItem Selected="True" Value="0">Select me</asp:ListItem>

除此之外,它应该可以正常工作。

编辑:您的评论显示您使用 jQuery 来启用修复DropDownList选择的 CheckBox。用户现在应该已经选择了一些东西。所以你的要求实际上是验证器不应该处于活动状态。所以最初禁用所有验证器(Enabled="false")。

由于您正在处理客户端上的复选框,因此我建议在选中验证器时启用它,并在未选中时禁用它。您可以使用 (Mini-) Validation-Client-Side API,尤其是该ValidatorEnable(val, enable)功能。您只需要对验证器的引用。但这对你来说应该不是问题。

于 2012-05-11T20:48:54.923 回答
1
credit goes to ahaliav fox

http://stackoverflow.com/questions/10566599/how-to-control-asp-net-validator-controls-client-side-validation


 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);
        }
于 2012-05-14T22:39:12.407 回答
0

我认为最好的方法是使用 customValidator 并使用客户端脚本进行验证。然后使用验证摘要来显示错误或需求

于 2012-05-11T20:48:47.077 回答