Validate textboxes and dropdowns inside GridView
<p>I've tried looking for this across StackOverflow and other sites, but can't find anything that matches my issue.  Basically, I have a gridview with a dropdown menu and a textbox contained in the rows.  The users need to select an option from the dropdown and depending on the option they select, they may also need to add some data to the textbox in the same row.  As part of this I want to run validation to alerts users as to which fields they've not completed - but I want to try and do this in asp.net C# if possible (not using JavaScript, or JQuery, or anything else if I can avoid it).</p>

<p>I've been able to get so far: if the dropdown isn't selected, or text isn't entered, the gridview will validate - but it puts an error message against every row, even if only one row is incorrect, whereas I only want the validation message to appear on those rows which aren't.</p>

<p>My code is:</p>

<pre><code>&lt;asp:UpdatePanel ID="upCurrentServicesGrid" UpdateMode="Conditional" runat="server"&gt;
    &lt;ContentTemplate&gt;
        &lt;asp:GridView ID="grdviewCurrentSystems" CssClass="table-data currentSystemTable" UseAccessibleHeader="true" AutoGenerateColumns="false" runat="server"&gt;
            &lt;Columns&gt;
                &lt;asp:BoundField DataField="systemName" HeaderText="Service" ItemStyle-Width="10%"/&gt;

                &lt;asp:TemplateField HeaderText="Status" ItemStyle-Width="39%"&gt;
                    &lt;ItemTemplate&gt;
                        &lt;asp:DropDownList ID="aspdropSystemStatus" CausesValidation="false" runat="server" /&gt;
                            &lt;div style="display:none"&gt;
                            &lt;asp:TextBox ID="RowId" runat="server" Text='&lt;%# Eval("id") %&gt;' /&gt;
                            &lt;/div&gt;
                            &lt;div style="display:none"&gt;
                                &lt;asp:TextBox ID="SelectedVal" runat="server" Text='&lt;%# Eval("systemStatus") %&gt;' /&gt;
                            &lt;/div&gt;
                    &lt;/ItemTemplate&gt;
                &lt;/asp:TemplateField&gt;
                &lt;asp:TemplateField HeaderText="Further information" ItemStyle-Width="50%"&gt;
                    &lt;ItemTemplate&gt;
                        &lt;div&gt;
                            &lt;asp:TextBox ID="txtSystemStatusInfo" TextMode="MultiLine" MaxLength="300" CssClass="textarea1" runat="server" Text='&lt;%# Eval("systemInformation") %&gt;' /&gt;
                                &lt;asp:CustomValidator
                                    CssClass="CustomValidator1"
                                    OnServerValidate="grdviewFurtherInfo_Validate" 
                                    Display="Dynamic"
                                    runat="server"
                                    ValidationGroup="valgrpLocal"&gt;
                                        &lt;asp:ValidationMessageLocal 
                                            CssClass="aspvalmsgLocal" 
                                            ID="aspvalSystemStatus" 
                                            Title="Service statuses" 
                                            TextPassed="" 
                                            TextFailed="Check Status or Further Information fields" 
                                            runat="server" /&gt;
                                    &lt;/asp:CustomValidator&gt;
                        &lt;/div&gt;
                    &lt;/ItemTemplate&gt;
                &lt;/asp:TemplateField&gt;
                &lt;asp:TemplateField HeaderText="Delete" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle"&gt;
                    &lt;ItemTemplate&gt;
                        &lt;div&gt;
                            &lt;asp:LinkButton ID="lnkbtnDelSystem" CausesValidation="false" OnClick="lnkbtnDelSystem_Click" OnClientClick="return confirmDelete(this.id); return false;" CommandArgument='&lt;%# Eval("id") %&gt;' runat="server"&gt;
                                &lt;span class="btn btn-small btn-spinner btn-grid"&gt;X&lt;/span&gt; 
                            &lt;/asp:LinkButton&gt;
                        &lt;/div&gt;
                    &lt;/ItemTemplate&gt;
                &lt;/asp:TemplateField&gt;
            &lt;/Columns&gt;
        &lt;/asp:GridView&gt;
    &lt;/ContentTemplate&gt;
&lt;/asp:UpdatePanel&gt;
</code></pre>

<p>And my code behind is:</p>

<pre><code>protected void grdviewFurtherInfo_Validate(object sender, ServerValidateEventArgs e)
{

    bool valid = true;
    foreach (GridViewRow row in grdviewCurrentSystems.Rows)
    {
        usercontrols_asp_formctl_dropdownlist aspDrop = (usercontrols_asp_formctl_dropdownlist)row.FindControl("aspdropSystemStatus");
        usercontrols_asp_formctl_textbox aspTxt = (usercontrols_asp_formctl_textbox)row.FindControl("txtSystemStatusInfo");


        if (aspDrop.SelectedValue == "")
        {
            valid = false;
        }
        else if (aspDrop.SelectedValue != "1" &amp;&amp; string.IsNullOrEmpty(aspTxt.Text))
        {
            valid = false;
        }
        e.IsValid = valid;
    }


}
</code></pre>

<p>Can anyone please advise?</p>

<p>Thank you! :)</p>