I have Gridview with Dropdownlist which contains data as show below .Gridview contains multiple rows of data. Submit button is also on form but outside gridview.
If any of the rows of gridview dropdownlist contain value as "Select" and click Submit button ,give message as "Please select value".
i have my button outside the gridivew and i am trying to validate the dropdownlist which is inside the gridivew.
i tried but the problem is that it validates for all the rows in the gridview instead of the one row which i am validating...
if i have 10 rows and the error displays for all the 10 rows instead of just one row which i am working on... i tried with uniquie validationgroup but does not help either something like this:
nisarkhan
Contributor
2402 Points
1472 Posts
Validating DropdownList (inside) Gridview and submit button outside the gridview
May 10, 2012 03:26 AM|LINK
I have Gridview with Dropdownlist which contains data as show below .Gridview contains multiple rows of data. Submit button is also on form but outside gridview.
If any of the rows of gridview dropdownlist contain value as "Select" and click Submit button ,give message as "Please select value".
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: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>
--------------------------
Don't forget to click "Mark as Answer" on the post(s) that helped you.
karthicks
All-Star
31376 Points
5421 Posts
Re: Validating DropdownList (inside) Gridview and submit button outside the gridview
May 10, 2012 05:20 AM|LINK
hi, your code fine. only thing change from InitialValue="Select me" to InitialValue="0"
bcos you should give 0 (value) not text
Sample:
Karthick S
vijay_myl
Contributor
5070 Points
1068 Posts
Re: Validating DropdownList (inside) Gridview and submit button outside the gridview
May 10, 2012 05:22 AM|LINK
hi..Try the below code..
You have to set validation group and change the intial value as 0..Below code is working now..
<asp:Button ID="btn" runat="server" Text="Submit" OnClick="btn_Click" CausesValidation="true" ValidationGroup ="gg"/> <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: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="0" ValidationGroup ="gg" Display="Dynamic"></asp:RequiredFieldValidator> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>My .NET blog
Submit Article
nisarkhan
Contributor
2402 Points
1472 Posts
Re: Validating DropdownList (inside) Gridview and submit button outside the gridview
May 10, 2012 12:58 PM|LINK
i tried but the problem is that it validates for all the rows in the gridview instead of the one row which i am validating...
if i have 10 rows and the error displays for all the 10 rows instead of just one row which i am working on... i tried with uniquie validationgroup but does not help either something like this:
ValidationGroup='<%# string.Format("Group_{0}", Eval("Id")) %>'--------------------------
Don't forget to click "Mark as Answer" on the post(s) that helped you.
richkyrs
Member
436 Points
500 Posts
Re: Validating DropdownList (inside) Gridview and submit button outside the gridview
May 11, 2012 01:47 AM|LINK
see the part were it says id assuming you have this as the id in your database?
nisarkhan
Contributor
2402 Points
1472 Posts
Re: Validating DropdownList (inside) Gridview and submit button outside the gridview
May 11, 2012 02:06 AM|LINK
yes it does
--------------------------
Don't forget to click "Mark as Answer" on the post(s) that helped you.
richkyrs
Member
436 Points
500 Posts
Re: Validating DropdownList (inside) Gridview and submit button outside the gridview
May 11, 2012 02:26 AM|LINK
strange have you tried
ValidationGroup= runat="server"'<%# string.Format("Group_{0}", Eval("Id")) %>''richkyrs
Member
436 Points
500 Posts
Re: Validating DropdownList (inside) Gridview and submit button outside the gridview
May 11, 2012 02:37 AM|LINK
rather than this {0} try it with {1} usually that means true or false
nisarkhan
Contributor
2402 Points
1472 Posts
Re: Validating DropdownList (inside) Gridview and submit button outside the gridview
May 11, 2012 02:38 AM|LINK
let me try your suggestions
--------------------------
Don't forget to click "Mark as Answer" on the post(s) that helped you.
richkyrs
Member
436 Points
500 Posts
Re: Validating DropdownList (inside) Gridview and submit button outside the gridview
May 11, 2012 02:45 AM|LINK
proper way of having a dropdown to work is this which i find works every time
<asp:DropDownList ID="ddlGender" runat="server" style="text-align: left"
Width="120px">
<asp:ListItem Value="-1">Select</asp:ListItem>
<asp:ListItem Value="Male">Male</asp:ListItem>
<asp:ListItem Value="Female">Female</asp:ListItem>
</asp:DropDownList>
thats just an example