Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to control ASP.NET Validator Controls Client Side validation
    text
    copied!<p>Below is the output of my page after it runs the below code:</p> <p>I'm using <code>ValidatorEnable</code> client side validation in my situation (if you know better way to do that, let me know.)</p> <p>My question is:</p> <p>Ihave a gridview with checkbox and a dropdownlist next to each other as shown in the screen shot.</p> <p>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.</p> <p>Problem:</p> <p>But the problem is: its validating for all the rows instead of the one i have clicked.</p> <p>How can i enforce to validate that particular row the user checked on the checkbox?</p> <p><img src="https://i.stack.imgur.com/nwvmB.png" alt="enter image description here"></p> <pre><code> &lt;asp:GridView ID="gv" runat="server" AutoGenerateColumns="False" DataKeyNames="Id" OnRowDataBound="gv_RowDataBound" EnableModelValidation="True"&gt; &lt;Columns&gt; &lt;asp:BoundField DataField="ID" ControlStyle-Width="250px" HeaderText="ID" SortExpression="ID" /&gt; &lt;asp:BoundField DataField="FirstName" ControlStyle-Width="250px" HeaderText="FirstName" SortExpression="FirstName" /&gt; &lt;asp:BoundField DataField="LastName" ControlStyle-Width="250px" HeaderText="LastName" SortExpression="LastName" /&gt; &lt;asp:TemplateField&gt; &lt;ItemTemplate&gt; &lt;asp:CheckBox ID="checkbox1" runat="server" /&gt; &lt;asp:DropDownList ID="ddl_PaymentMethod" runat="server"&gt; &lt;asp:ListItem Value="-1"&gt;----&lt;/asp:ListItem&gt; &lt;asp:ListItem Value="0"&gt;Month&lt;/asp:ListItem&gt; &lt;asp:ListItem Value="1"&gt;At End&lt;/asp:ListItem&gt; &lt;asp:ListItem Value="2"&gt;At Travel&lt;/asp:ListItem&gt; &lt;/asp:DropDownList&gt; &lt;asp:RequiredFieldValidator ID="requiredDDL" runat="server" ControlToValidate="ddl_PaymentMethod" ErrorMessage="Please select" InitialValue="-1" Display="Dynamic"&gt;&lt;/asp:RequiredFieldValidator&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText="Value"&gt; &lt;ItemTemplate&gt; &lt;asp:TextBox ID="txt_Value" runat="server" Width="58px" Text="0"&gt;&lt;/asp:TextBox&gt; &lt;/ItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; 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)); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload