locked
DropDownList Validation not working inside Gridview RRS feed

  • Question

  • User-1426568392 posted

    I have a dropdownlist inside a gridview,the drop list should populate from database and default value should be SELECT.

    when i Click Save button it should check all the dropdownlist values. If any one is still SELECT it should prompt error select any values.

    When i normaly check without inside gridview it works fine and not inside gridview.

    I have attached my codings below.

    Help in this regard.

    Thanks

    <asp:GridView ID="GVResrv" runat="server" Width="100%" AutoGenerateColumns="false"
    TabIndex="1" Font-Names="Times New Roman" Font-Size="Smaller" HeaderStyle-HorizontalAlign="Center">
    <Columns >
    <asp:CommandField ShowSelectButton="true" HeaderText="Details"
    SelectText="Details" />
    <asp:TemplateField>
    <HeaderTemplate>
    <asp:CheckBox ID="chkHeader" runat="server" Checked="true" />
    </HeaderTemplate>
    <ItemTemplate>
    <asp:CheckBox ID="chkRow" runat="server" Checked="true" />
    </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField DataField="Vin" HeaderText="VinNumber" />
    <asp:TemplateField HeaderText="Sales Person">
    <ItemTemplate>
    <asp:TextBox ID="txtSalesPersonName" runat="server" Text='<%# Eval("Sales_Person_Name") %>' />
    </ItemTemplate>
    <ItemTemplate>
    <asp:DropDownList ID="ddlSalesPerson" runat="server" DataSourceID="SqlDataSource2"
    DataTextField="SalesPersonName" DataValueField="SalesPersonName" AppendDataBoundItems="true">
    <asp:ListItem Text="SELECT" Value="0" />
    </asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:VehicleConnectionString %>"
    SelectCommand="Select SPNID,SalesPersonName from R_ResrvSalesPerson">
    </asp:SqlDataSource>

    </ItemTemplate>
    </asp:TemplateField>
    </asp:GridView>

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function () {
    $("[id$=btnSave]").click(function () {
    if ($("[id$=ddlSalesPerson]").val() != 0) {
    //Selected option from dropdownlist


    return true;
    } else {
    //Not selected so alert user to select any option

    alert('Please select Sales Person Name');
    return false;
    }
    });
    });
    </script>

    <asp:GridView ID="GVResrv" runat="server" Width="100%" AutoGenerateColumns="false"
    TabIndex="1" Font-Names="Times New Roman" Font-Size="Smaller" HeaderStyle-HorizontalAlign="Center">
    <Columns >
    <asp:CommandField ShowSelectButton="true" HeaderText="Details"
    SelectText="Details" />
    <asp:TemplateField>
    <HeaderTemplate>
    <asp:CheckBox ID="chkHeader" runat="server" Checked="true" />
    </HeaderTemplate>
    <ItemTemplate>
    <asp:CheckBox ID="chkRow" runat="server" Checked="true" />
    </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField DataField="Vin" HeaderText="VinNumber" />
    <asp:TemplateField HeaderText="Sales Person">
    <ItemTemplate>
    <asp:TextBox ID="txtSalesPersonName" runat="server" Text='<%# Eval("Sales_Person_Name") %>' />
    </ItemTemplate>
    <ItemTemplate>
    <asp:DropDownList ID="ddlSalesPerson" runat="server" DataSourceID="SqlDataSource2"
    DataTextField="SalesPersonName" DataValueField="SalesPersonName" AppendDataBoundItems="true">
    <asp:ListItem Text="SELECT" Value="0" />
    </asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:VehicleConnectionString %>"
    SelectCommand="Select SPNID,SalesPersonName from R_ResrvSalesPerson">
    </asp:SqlDataSource>

    </ItemTemplate>
    </asp:TemplateField>
    </asp:GridView>

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function () {
    $("[id$=btnSave]").click(function () {
    if ($("[id$=ddlSalesPerson]").val() != 0) {
    //Selected option from dropdownlist


    return true;
    } else {
    //Not selected so alert user to select any option

    alert('Please select Sales Person Name');
    return false;
    }
    });
    });
    </script>

    Wednesday, October 19, 2016 5:52 AM

Answers

  • User1724605321 posted

    Hi sth_syed,

    One of the solution is to add a cssclass attribute to your dropdownlist :

      <asp:DropDownList ID="ddlSalesPerson" CssClass="dp" runat="server">

    Then loop all dropdownlists using jquery as :

      $(document).ready(function () {
                $("[id$=btnSave]").click(function () {
                    $(".dp").each(function () {
                        if (this.value != 0) {
                            //Selected option from dropdownlist
                            return true;
                        } else {
                            //Not selected so alert user to select any option
                            alert('Please select Sales Person Name');
                            return false;
                        }
    
                    })
                }
                  );
            });

    Best Regards,

    Nan Yu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 19, 2016 8:53 AM