issoa
issoa

Reputation: 47

How to validate dropdownlist control inside the gridview

edit:

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;
      }
      }
  }

the above code works

i have my button outside the gridivew and i am trying to validate the dropdownlist which is inside the 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>

Upvotes: 2

Views: 5720

Answers (3)

Nick Kahn
Nick Kahn

Reputation: 20078

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);
        }

Upvotes: 1

Tim Schmelter
Tim Schmelter

Reputation: 460158

Set the RequiredFieldValidator's InitialValue to 0 instead of "Select me" since the item that is selected by default has a value of 0

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

Apart from that it should work fine.

Edit: Your comments revealed that you use jQuery to enable a CheckBox that fixes the DropDownList selection. The user should have selected something now. So your requirement actualy is that no the validator should be active. So disable all Validators initially(Enabled="false").

Since you're handling the checkbox click on clientside anyway, i would recommend to enable the validator if it's checked and disable it when it's unchecked. You can use the (Mini-)Validation-Client-Side API especially the ValidatorEnable(val, enable) function. You only need a reference to the validator. But that should not a problem for you.

Upvotes: 1

Jean Claude Abela
Jean Claude Abela

Reputation: 907

I think the best way is to use a customValidator and use client side scripts to validate. Then use a validation summary to display the error or the requirement

Upvotes: 0

Related Questions