This article will show you how you can validate gridview
textbox value by requiredfieldvalidator in asp.net using c#.net.
Some of my previous articles are as follows: Asp.Net
Login Form With User Role Selection By Dropdownlist In Asp.net Using C#.Net,
Populate
Data in GridView on DropdownList Selected Role in Asp.net Using C#.net, Random
Character and String Generation Using C#.Net, Windows Application and Linq ,
Strong
Random String Password Generation Using C#.Net.
So for this article
first we will create a new asp.net application and add the gridview. After
adding all the controls your .aspx page will look as shown below.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1"
%>
<!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>Bind and
Validate GridView TextBox Value by RequiredFieldValidator In Asp.Net Using C#
</title>
</head>
<body>
<form id="form1" runat="server">
<table width="100%" cellpadding="4" cellspacing="4">
<tr>
<td align="center">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Save" Width="89px" />
</td>
</tr>
<tr>
<td align="center">
<asp:Label ID="lblmessage" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td align="left">
<asp:GridView ID="GridView1" runat="server" Width="100%" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="Name"
HeaderText="Name"
/>
<asp:BoundField DataField="Address"
HeaderText="Address"
/>
<asp:BoundField DataField="UserType"
HeaderText="UserType"
/>
<asp:TemplateField>
<HeaderTemplate>
Percentage
</HeaderTemplate>
<ItemTemplate>
<asp:TextBox ID="TextBox1"
runat="server"></asp:TextBox>
<asp:RequiredFieldValidator
ID="RequiredFieldValidator1"
runat="server"
ControlToValidate="TextBox1"
ErrorMessage="*"
ForeColor="Red"></asp:RequiredFieldValidator>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
</tr>
</table>
</form>
</body>
</html>
|
In above code please check the item template. In this I have
added RequiredFieldValidator and textbox. Now when you click on button you will
get the error message as astirc.
Now add the below code into the into your .cs page to bind
the gridview with sql server database table.
using System;
using System.Data.SqlClient;
using System.Data;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected
void Page_Load(object
sender, EventArgs e)
{
if
(!IsPostBack)
{
BindGridView();
}
}
private
void BindGridView()
{
SqlConnection
con = new SqlConnection(System.Configuration.ConfigurationManager.
ConnectionStrings["con"].ToString());
try
{
DataTable
objdt = new DataTable();
string
query = "select * from UserDetail;";
SqlDataAdapter
da = new SqlDataAdapter(query,
con);
con.Open();
da.Fill(objdt);
con.Close();
if
(objdt.Rows.Count > 0)
{
GridView1.DataSource =
objdt;
GridView1.DataBind();
}
}
catch
{
con.Close();
}
}
protected
void Button1_Click(object
sender, EventArgs e)
{
lblmessage.Text = "Thanks for providing values.";
}
}
}
|
Now we have done run the application and check the output.
0 comments:
Please let me know your view