Find Control using Gridview in asp.net
Introduction : This tutorial explain how to use find control in gridview to find the specific control in asp.net. here in this tutorial we explain how to use attendance system using the .net control like gridview,textbox,radiobutton,image control using asp.net C# with sql server database and visual studio 2010.
Step :1 First you can add database with Registration Table and give table name "reg"
Step 2: Now you can add another table for attendence entry & give table name "record"
Step 4: For Query Logic add class BAL.CS
Step : 5 Now you can add WebForm and add BoundControl in gridview as the following, here
"SqlDataSource1" Select the user from the table Reg these user are display in gridview and enter the entry according user name like given below image
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="GridView_Find_Control.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server" aria-orientation="vertical">
<div>
<asp:Button ID="btn_save" runat="server" Text="SAVE" OnClick="btn_save_Click" />
</div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
<Columns>
<asp:TemplateField HeaderText="Name" SortExpression="Name">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="In_Time" SortExpression="Name">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:TextBox ID="txt_intime" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Out_Time" SortExpression="Name">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:TextBox ID="txt_outtime" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Gender" SortExpression="Name">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatDirection="Horizontal">
<asp:ListItem>Male</asp:ListItem>
<asp:ListItem>Female</asp:ListItem>
</asp:RadioButtonList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Image" SortExpression="Name">
<EditItemTemplate>
<asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:FileUpload ID="FileUpload1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:c %>" SelectCommand="SELECT [Name] FROM [REg]"></asp:SqlDataSource>
</form>
</body>
</html>
Step 6 :After adding this bound control in source file than after add the code in the codebehind file in following. here findcontrol can find the id of the controller.
namespace GridView_Find_Control
{
public partial class WebForm1 : System.Web.UI.Page
{
DAL da = new DAL();
BAL ba = new BAL();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn_save_Click(object sender, EventArgs e)
{
foreach (GridViewRow gvr in GridView1.Rows)
{
Label name = (Label)gvr.FindControl("Label1");
TextBox intime = (TextBox)gvr.FindControl("txt_intime");
TextBox outtime = (TextBox)gvr.FindControl("txt_outtime");
RadioButtonList rd = (RadioButtonList)gvr.FindControl("RadioButtonList1");
FileUpload fup = (FileUpload)gvr.FindControl("FileUpload1");
da.Name = name.Text;
da.In_Time = intime.Text;
da.Out_Time = outtime.Text;
da.Gender = rd.Text;
if (fup.HasFile)
{
string File = Path.GetFileName(fup.FileName);
fup.SaveAs(MapPath("~/Image/") + File);
da.Image = ("~/Image/") + File;
}
ba.Insert_Data(da);
}
Response.Write("Data Inserted Successfuly.");
}
}
}
OutPut:
Step :1 First you can add database with Registration Table and give table name "reg"
Step 2: Now you can add another table for attendence entry & give table name "record"
Step 3: For data access layer add class DAL.CS
namespace GridView_Find_Control
{
public class DAL
{
public int Id { get; set; }
public string Name { get; set; }
public string In_Time { get; set; }
public string Out_Time { get; set; }
public string Gender { get; set; }
public string Image { get; set; }
}
}
{
public class DAL
{
public int Id { get; set; }
public string Name { get; set; }
public string In_Time { get; set; }
public string Out_Time { get; set; }
public string Gender { get; set; }
public string Image { get; set; }
}
}
Step 4: For Query Logic add class BAL.CS
namespace GridView_Find_Control
{
public class BAL
{
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=D:\GridView_Find_Control\GridView_Find_Control\App_Data\MYDB.mdf;Integrated Security=True");
public void Insert_Data(DAL Da)
{
con.Open();
SqlCommand cmd = new SqlCommand("Insert into record Values('" + Da.Name + "','" + Da.In_Time + "','" + Da.Out_Time + "','" + Da.Gender + "','" + Da.Image + "')", con);
cmd.ExecuteNonQuery();
con.Close();
}
}
}
{
public class BAL
{
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=D:\GridView_Find_Control\GridView_Find_Control\App_Data\MYDB.mdf;Integrated Security=True");
public void Insert_Data(DAL Da)
{
con.Open();
SqlCommand cmd = new SqlCommand("Insert into record Values('" + Da.Name + "','" + Da.In_Time + "','" + Da.Out_Time + "','" + Da.Gender + "','" + Da.Image + "')", con);
cmd.ExecuteNonQuery();
con.Close();
}
}
}
Step : 5 Now you can add WebForm and add BoundControl in gridview as the following, here
"SqlDataSource1" Select the user from the table Reg these user are display in gridview and enter the entry according user name like given below image
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="GridView_Find_Control.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server" aria-orientation="vertical">
<div>
<asp:Button ID="btn_save" runat="server" Text="SAVE" OnClick="btn_save_Click" />
</div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
<Columns>
<asp:TemplateField HeaderText="Name" SortExpression="Name">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="In_Time" SortExpression="Name">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:TextBox ID="txt_intime" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Out_Time" SortExpression="Name">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:TextBox ID="txt_outtime" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Gender" SortExpression="Name">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatDirection="Horizontal">
<asp:ListItem>Male</asp:ListItem>
<asp:ListItem>Female</asp:ListItem>
</asp:RadioButtonList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Image" SortExpression="Name">
<EditItemTemplate>
<asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:FileUpload ID="FileUpload1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:c %>" SelectCommand="SELECT [Name] FROM [REg]"></asp:SqlDataSource>
</form>
</body>
</html>
Step 6 :After adding this bound control in source file than after add the code in the codebehind file in following. here findcontrol can find the id of the controller.
namespace GridView_Find_Control
{
public partial class WebForm1 : System.Web.UI.Page
{
DAL da = new DAL();
BAL ba = new BAL();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btn_save_Click(object sender, EventArgs e)
{
foreach (GridViewRow gvr in GridView1.Rows)
{
Label name = (Label)gvr.FindControl("Label1");
TextBox intime = (TextBox)gvr.FindControl("txt_intime");
TextBox outtime = (TextBox)gvr.FindControl("txt_outtime");
RadioButtonList rd = (RadioButtonList)gvr.FindControl("RadioButtonList1");
FileUpload fup = (FileUpload)gvr.FindControl("FileUpload1");
da.Name = name.Text;
da.In_Time = intime.Text;
da.Out_Time = outtime.Text;
da.Gender = rd.Text;
if (fup.HasFile)
{
string File = Path.GetFileName(fup.FileName);
fup.SaveAs(MapPath("~/Image/") + File);
da.Image = ("~/Image/") + File;
}
ba.Insert_Data(da);
}
Response.Write("Data Inserted Successfuly.");
}
}
}
OutPut:
Sign up here with your email
ConversionConversion EmoticonEmoticon