Purvi's work and search in .net technologies.
Pages
Home
ASPDotnet with C#
List of C and C++ Programs with Code Output and Explanation
C Programming
Learn C++
Projects
Useful Links
Health TIPs
Tuesday, 2 April 2013
Validate only certain selected GridView Row Controls like TextBox using ASP.Net Validators
Validate only certain selected GridView Row Controls like TextBox using ASP.Net Validators
HTML Markup
The HTML Markup contains a GridView with TextBox in the TemplateField ItemTemplate and RequiredField Validators for each TextBox. Each GridView Row also has a Button that will validate the GridView row it belongs. When you press the Button if any TextBox in the particular GridView Row is not filled with value the RequiredField Validators will be raised.
To make this happen, I have set
ValidationGroup
property for each Validator as well as the Button in a particular GridView row using
Container.DataItemIndex
, so that a unique Validation Group is set for each specific row and the Button control inside each GridView Row will validate only the controls belonging to its own row.
<
asp
:
GridView
ID
="GridView1"
HeaderStyle-BackColor
="#3AC0F2"
HeaderStyle-ForeColor
="White"
RowStyle-BackColor
="#A1DCF2"
AlternatingRowStyle-BackColor
="White"
AlternatingRowStyle-ForeColor
="#000"
runat
="server"
AutoGenerateColumns
="false">
<
Columns
>
<
asp
:
BoundField
DataField
="Item"
HeaderText
="Item"
/>
<
asp
:
TemplateField
HeaderText
="Price">
<
ItemTemplate
>
<
asp
:
TextBox
ID
="txtPrice"
runat
="server"
Text
='
<%
#
Eval("Price")
%>
'></
asp
:
TextBox
><
br
/>
<
asp
:
RequiredFieldValidator
ID
="rfvPrice"
ControlToValidate
="txtPrice"
runat
="server"
ErrorMessage
="Required"
ForeColor
="Red"
ValidationGroup
=
'
<%
#
"Group_" + Container.DataItemIndex
%>
'></
asp
:
RequiredFieldValidator
>
</
ItemTemplate
>
</
asp
:
TemplateField
>
<
asp
:
TemplateField
HeaderText
="Quantity">
<
ItemTemplate
>
<
asp
:
TextBox
ID
="txtQuantity"
runat
="server"
Text
='
<%
#
Eval("Quantity")
%>
'></
asp
:
TextBox
><
br
/>
<
asp
:
RequiredFieldValidator
ID
="rfvQuantity"
ControlToValidate
="txtQuantity"
runat
="server"
ErrorMessage
="Required"
ForeColor
="Red"
ValidationGroup
=
'
<%
#
"Group_" + Container.DataItemIndex
%>
'></
asp
:
RequiredFieldValidator
>
</
ItemTemplate
>
</
asp
:
TemplateField
>
<
asp
:
TemplateField
HeaderText
="Quantity">
<
ItemTemplate
>
<
asp
:
Button
ID
="btnSelect"
runat
="server"
Text
="Select"
ValidationGroup
=
'
<%
#
"Group_" + Container.DataItemIndex
%>
'/>
</
ItemTemplate
>
</
asp
:
TemplateField
>
</
Columns
>
</
asp
:
GridView
>
Namespaces
using
System.Data;
Binding the GridView
Here I am binding GridView using a DataTable with some dummy values.
protected
void
Page_Load(
object
sender,
EventArgs
e)
{
if
(!IsPostBack)
{
DataTable
dt =
new
DataTable
();
dt.Columns.AddRange(
new
DataColumn
[3] {
new
DataColumn
(
"Item"
),
new
DataColumn
(
"Price"
),
new
DataColumn
(
"Quantity"
) });
dt.Rows.Add(
"Shirt"
);
dt.Rows.Add(
"Football"
, 299, 10);
dt.Rows.Add(
"Shirt"
, 545, 15);
dt.Rows.Add(
"Disc"
, 99, 20);
dt.Rows.Add(
"Watch"
, 200, 45);
dt.Rows.Add(
"Clock"
, 670, 97);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
Demo
View Demo
Downloads
Download Sample
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment