How to find Control in TemplateField of GridView

How to find Control in TemplateField of GridView

Introduction

This tutorial we have learn How to find Control in TemplateField of GridView.Here we have explain attendance system using the .net control like gridview,textbox,radiobutton,image control using asp.net C# with sql server database and visual studio.

Step :1 First we need to add sql table “reg

registration

Step 2: Now you can add another table for attendance entry And give table name “record

Records

Step 3: For data access layer  add class DAL.CS

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

public class BAL
{
SqlConnection con = new SqlConnection(@”Data Source=(LocalDB)\v11.0;AttachDbFilename”);

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

<%@ 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 code behind file as following. Here we have  find id of controls using the find control of item template.

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.”);
}
}

Now, you have try this code and run your web application and see out put

How to find Control in TemplateField of GridView

Hi friend, if you have any query regarding, How to find Control in TemplateField of GridView, Then feel free to comment on below comment section.

SEE MORE

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *