If you want to add Control to the footer Row in GridView, you must use footer template in GridView also set ShowFooter property is true. Also if you find control in code you can use FindControl menthod . Use single line for accessing control in code
</FooterTemplate>
Before insertion
lets take an example
Suppose your footer row take one TextBox control with id property (id="nametxt")
<FooterTemplate>
<asp:TextBox ID="" runat="server"></asp:TextBox>
<asp:TextBox ID="" runat="server"></asp:TextBox>
</FooterTemplate>
Now find TextBox control in code with simple example
var nametextbox =GridView1.FooterRow.FindControl("nametxt")as TextBox;
Now again your footer row take one Label control with label ID property (ID="namelbl")
<%@ Page Language="C#" EnableEventValidation ="false" AutoEventWireup="true" CodeFile="additem.aspx.cs" Inherits="additem" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" ShowFooter ="true" AutoGenerateColumns ="false" OnRowCommand="GridView1_RowCommand">
<Columns >
<asp:TemplateField HeaderText ="Press button">
<ItemTemplate >
<asp:Label ID="Label1" runat="server" Text='<%# Eval("namet") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate >
<asp:Button ID="bt" runat ="server" Text ="Insert item" CommandName ="InsertRecord"/>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Insert value">
<FooterTemplate >
<asp:TextBox ID="txtCustomerID" runat="server" Text="" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Label ID="result" runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>
Codebehind<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" ShowFooter ="true" AutoGenerateColumns ="false" OnRowCommand="GridView1_RowCommand">
<Columns >
<asp:TemplateField HeaderText ="Press button">
<ItemTemplate >
<asp:Label ID="Label1" runat="server" Text='<%# Eval("namet") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate >
<asp:Button ID="bt" runat ="server" Text ="Insert item" CommandName ="InsertRecord"/>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText ="Insert value">
<FooterTemplate >
<asp:TextBox ID="txtCustomerID" runat="server" Text="" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Label ID="result" runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class additem : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page .IsPostBack)
{
binddata();
}
}
public void binddata()
{
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
con.Open();
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
cmd.CommandText = "Select * from [footerex]";
cmd.Connection = con;
System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(cmd);
System.Data.DataSet ds = new System.Data.DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName=="InsertRecord")
{
var nametx = GridView1.FooterRow.FindControl("namebox") as TextBox;
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True");
con.Open();
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
cmd.Parameters.AddWithValue("@name", nametx .Text);
cmd.CommandText = "insert into [footerex](namet)values(@name)";
cmd.Connection = con;
int a = cmd.ExecuteNonQuery();
if (a > 0)
{
result.Text = "Row Updated";
con.Close();
binddata();
}
else
{
result.Text = "failed";
}
}
}
}
Outputusing System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class additem : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page .IsPostBack)
{
binddata();
}
}
public void binddata()
{
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
con.Open();
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
cmd.CommandText = "Select * from [footerex]";
cmd.Connection = con;
System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(cmd);
System.Data.DataSet ds = new System.Data.DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName=="InsertRecord")
{
var nametx = GridView1.FooterRow.FindControl("namebox") as TextBox;
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True");
con.Open();
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
cmd.Parameters.AddWithValue("@name", nametx .Text);
cmd.CommandText = "insert into [footerex](namet)values(@name)";
cmd.Connection = con;
int a = cmd.ExecuteNonQuery();
if (a > 0)
{
result.Text = "Row Updated";
con.Close();
binddata();
}
else
{
result.Text = "failed";
}
}
}
}
Before insertion
After Insertion
Tidak ada komentar:
Posting Komentar