I'm working on .NET 2.0
I've a simple test webform with a GridView. The grid has a few bound columns, a template field with a drop down list (with hard coded values) and a asp:Button field.
It also has a RequiredFieldValidator, to ensure that the user has chosen a value other than default, before he submits the form and a ValidationSummary control.
The first value in the drop down list is "choose one of the following ...".
The intention here is to force user to choose a value from the drop down for the row in which he clicks the button.
My problem is, validation is happening for all the rows instead of happening only on the row he clicked!! So, even when he chooses a value in the dropdown and clicks the submit button on a row, the validation fails as the dropdown in other rows still have their default value selected!
I think this is a pretty simple scenario. Can someone please point what I might be doing wrong?
The code looks like this
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID"
DataSourceID="SqlDataSource1" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True"
SortExpression="ID" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
<asp:BoundField DataField="Age" HeaderText="Age" SortExpression="Age" />
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Value="-1" Selected="True">choose one of the following ...</asp:ListItem>
<asp:ListItem Value="1">item1text</asp:ListItem>
<asp:ListItem Value="2">item2text</asp:ListItem>
<asp:ListItem Value="3">item3text</asp:ListItem>
<asp:ListItem Value="4">item4text</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="DropDownList1"
ErrorMessage="RequiredFieldValidator" InitialValue="-1" ValidationGroup="testGroup">*</asp:RequiredFieldValidator>
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField ButtonType="Button" Text="Button" ValidationGroup="testGroup" CausesValidation="True" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:db1ConnectionString %>"
ProviderName="<%$ ConnectionStrings:db1ConnectionString.ProviderName %>" SelectCommand="SELECT [ID], [FirstName], [LastName], [Age] FROM [PersonalDetails]">
</asp:SqlDataSource>
<br />
<asp:Label ID="lblResult" runat="server" Text="Label"></asp:Label><br />
<br />
<br />
<asp:ValidationSummary ID="ValidationSummary1" runat="server" ValidationGroup="testGroup" />
</div>
</form>
</body>
</html>
//Code behind is doing nothing significant
codebehind file
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
string commandName = (string)e.CommandName;
string commandArgument = (string)e.CommandArgument;
lblResult.Text = "Command complete";
}