datagrid中动态添加按钮实例
<%@ Page language="c#" Codebehind="WebForm4.aspx.cs" AutoEventWireup="false" Inherits="DGrid.WebForm4" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm4</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False" ShowFooter="True">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "name", "{0}") %>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox Runat="server" ID="txtName"></asp:TextBox>
</FooterTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "age", "{0}") %>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtAge" Runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateColumn>
<asp:ButtonColumn Text="刪除">
</asp:ButtonColumn>
</Columns>
</asp:DataGrid>
</form>
</body>
</HTML>
---------------------------------------------------------------------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace DGrid
{
/// <summary>
/// WebForm4 的摘要说明。
/// </summary>
public class WebForm4 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected DataSet dsEmployee = new DataSet();
protected Button myButton = new Button();
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
this.Admin();
}
}
private void Admin()
{
SqlConnection Conn = new SqlConnection("server=.;database=test;uid=sa");
string strSQL = "select * from aaa";
SqlDataAdapter adapter = new SqlDataAdapter(strSQL,Conn);
adapter.Fill(dsEmployee);
this.DataGrid1.DataSource = dsEmployee;
this.DataGrid1.DataBind();
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
this.DataGrid1.ItemCreated += new DataGridItemEventHandler(DataGrid1_ItemCreated);
this.DataGrid1.ItemCommand += new DataGridCommandEventHandler(DataGrid1_ItemCommand);
}
#endregion
private void DataGrid1_ItemCreated(object sender, DataGridItemEventArgs e)
{
if(e.Item.ItemType==ListItemType.Footer)
{
myButton.ID = "btnAddNew";
myButton.Text = "确定";
myButton.CommandName = "AddNew";
e.Item.Cells[2].Controls.Add(myButton);
}
}
private void DataGrid1_ItemCommand(object source, DataGridCommandEventArgs e)
{
if(e.CommandName=="AddNew")
{
string name = ((TextBox)e.Item.Cells[0].FindControl("txtName")).Text.Trim();
string age = ((TextBox)e.Item.Cells[1].FindControl("txtAge")).Text.Trim();
SqlConnection Conn = new SqlConnection("server=.;database=test;uid=sa");
string strSQL = "insert into aaa (name,age) values ('"+name+"','"+age+"')";
Conn.Open();
SqlCommand Comm = new SqlCommand(strSQL,Conn);
Comm.ExecuteNonQuery();
Conn.Close();
this.Admin();
}
}
}