connectseekers.com

Sunday, 10 August 2014

GridView Validation Of TextBox Using RequiredFieldValidator Control Asp.Net Using C#.Net

8/10/2014 - By Pranav Singh 0

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.


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

About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com
Email Id : contact@aspdotnet-pools.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Top jQuery Plugins

Global Classified : Connectseekers.com
© 2014
back to top