This article will show you how you can use you required field
validation control for validating the textbox control present inside a gridview
control. In this article I have shown how to bind the gridview control and
validate the textbox control.
Some of my previous articles are as follows:
Highlight
GridView Row on Mouseover Using CSS in Asp.Net C#.Net, Email
Address Validation in Javascript in Asp.Net, jQuery
Validation for Terms and Conditions Checkbox in Asp.Net, Validation
for Accepting Terms and Conditions Checkbox in Asp.Net MVC Using jQuery,
Email
Validation in Windows Application C#.Net and VB.Net, RadioButtonList
Validation Using jQuery in Asp.Net, Validate
Selection Of Checkbox In GirdView Using Asp.net C# and jQuery | Force User To
Select One CheckBox Of GridView Before Submit in Asp.net Using javascript.
So for this article first we will create a new asp.net
application and add a gridview control in it. After adding gridview control
edit column for add textbox and requiredfieldvalidation control in it. After
that your code will look as shown below.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="draganddropgridvirecell.aspx.cs"
Inherits="ProjectDemo_Asp.et.draganddropgridvirecell"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>GridView
Validation Of TextBox Using RequiredFieldValidator Control Asp.Net
</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" EmptyDataText="There are no data records to display."
BorderStyle="Solid" style="color: Black">
<Columns>
<asp:TemplateField HeaderText="NAME">
<ItemTemplate>
<asp:TextBox ID="TextBox1"
runat="server"
Text='<%# Bind("author_name") %>'></asp:TextBox>
<asp:RequiredFieldValidator
ID="RequiredFieldValidator1"
runat="server"
ErrorMessage="*"
style="color: #FF0000"
ControlToValidate="TextBox1"></asp:RequiredFieldValidator>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="PUBLISHER NAME">
<ItemTemplate>
<asp:TextBox ID="TextBox2"
runat="server"
Text='<%# Bind("publisher_name") %>'></asp:TextBox>
<asp:RequiredFieldValidator
ID="RequiredFieldValidator2"
runat="server"
ErrorMessage="*"
style="color: #FF0000"
ControlToValidate="TextBox2"></asp:RequiredFieldValidator>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="PUBLISH YEAR">
<ItemTemplate>
<asp:TextBox ID="TextBox3"
runat="server"
Text='<%# Bind("publication_year") %>'></asp:TextBox>
<asp:RequiredFieldValidator
ID="RequiredFieldValidator3"
runat="server"
ErrorMessage="*"
style="color: #FF0000"
ControlToValidate="TextBox3"></asp:RequiredFieldValidator>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="PRICE">
<ItemTemplate>
<asp:TextBox ID="TextBox4"
runat="server"
Text='<%# Bind("retail_price") %>'></asp:TextBox>
<asp:RequiredFieldValidator
ID="RequiredFieldValidator4"
runat="server"
ErrorMessage="*"
ControlToValidate="TextBox4"
style="color:
#FF0000"></asp:RequiredFieldValidator>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#66CCFF" />
</asp:GridView>
<asp:Label ID="Label1" runat="server" Text="" style="color: #FF0000"></asp:Label>
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" />
<br />
</div>
</form>
</body>
</html>
|
Now here is the code of the .cs page.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;
namespace ProjectDemo_Asp.et
{
public partial class draganddropgridvirecell : System.Web.UI.Page
{
public
string connectionstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=|DataDirectory|\\bookstore.mdb;Persist Security Info=False;";
protected
void Page_Load(object
sender, EventArgs e)
{
DataTable
_objdt = new DataTable();
_objdt = GetDataFromDataBase();
if
(_objdt.Rows.Count > 0)
{
GridView1.DataSource =
_objdt;
GridView1.DataBind();
}
}
///
/// Function for binding retribing the data from database
///
public DataTable GetDataFromDataBase()
{
DataTable _objdt = new DataTable();
string querystring = "select * from Books;";
OleDbConnection _objcon = new OleDbConnection(connectionstring);
OleDbDataAdapter _objda = new OleDbDataAdapter(querystring, _objcon);
_objcon.Open();
_objda.Fill(_objdt);
return _objdt;
}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "Thanks for submitting form.";
}
}
}
|
In above code first I have bind the gridview control and
after that on button click message have been displayed in gridview have been validated successfully.
Now run the page to view the output.
Now remove first row value and then check. You will get the error message.
DOWNLOAD
0 comments:
Please let me know your view