我们用BoundColumn中的FooterText中来处理
我们使用DataGrid的属性生成器来增加FooterText,如
<asp:BoundColumn DataField="firstname" HeaderText="firstname" FooterText="<input type=text name=firstname>"></asp:BoundColumn>
最后显示的时候会在firstname列的Footer处生成一个textBox,对于事件处理,我们用
<script language="javascript">
function btAddNew()
{
__doPostBack('AddNew','');
}
</script>
来触发这个事件,其中AddNew是一个服务端控件linkbutton。
<%@ Page language="c#" Codebehind="WebForm2.aspx.cs" AutoEventWireup="false" Inherits="WebApplication3.WebForm2" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<script language="javascript">
function btAddNew()
{
__doPostBack('AddNew','');
}
</script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<table style="Z-INDEX: 101; LEFT: 64px; WIDTH: 640px; POSITION: absolute; TOP: 32px">
<tr>
<td style="HEIGHT: 22px"><asp:linkbutton id="ShowAddNew" runat="server">增加新记录</asp:linkbutton><asp:linkbutton id="AddNew" runat="server" Visible="False">LinkButton</asp:linkbutton><FONT face="宋体"></FONT></td>
</tr>
<tr>
<td><asp:datagrid id="DataGrid1" runat="server" Width="656px" Height="176px" AutoGenerateColumns="False"
ShowFooter="True" DataKeyField="employeeid" AllowPaging="True" PagerStyle-HorizontalAlign="Right">
<Columns>
<asp:BoundColumn DataField="employeeid" ReadOnly="True" HeaderText="employeeid"></asp:BoundColumn>
<asp:BoundColumn DataField="lastname" HeaderText="latname" FooterText="<SELECT name="ddl"><OPTION value=1 selected>AAA</OPTION><OPTION value=2>BBB</OPTION><OPTION value=3>CCC</OPTION></SELECT>"></asp:BoundColumn>
<asp:BoundColumn DataField="firstname" HeaderText="firstname" FooterText="<input type=text name=firstname>"></asp:BoundColumn>
<asp:BoundColumn DataField="city" HeaderText="city" FooterText="<input type=text name=city>"></asp:BoundColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="更新" HeaderText="操作" FooterText="<input type=button value='增加' onclick='btAddNew();'&gt"
CancelText="取消" EditText="<img src=images\edit.gif border='0'>"></asp:EditCommandColumn>
<asp:ButtonColumn Text="<div id="de" onclick="JavaScript:return confirm('确定删除吗?')"><img src="images/del.gif" border='0'></div>"
CommandName="Delete"></asp:ButtonColumn>
</Columns>
<PagerStyle HorizontalAlign="Right"></PagerStyle>
</asp:datagrid></td>
</tr>
</table>
</form>
</body>
</HTML>
WebForm2.aspx.cs
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;
using System.Configuration;
namespace WebApplication3
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm2 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
SqlConnection conn=null;
protected System.Web.UI.WebControls.LinkButton AddNew;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.LinkButton ShowAddNew;
private const String connString="DataBase=northwind;server=localhost;uid=sa;pwd=;";
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面s
conn=new SqlConnection(connString);
conn.Open ();
SqlDataAdapter adapter=new SqlDataAdapter("select * from employees",conn);
DataSet ds=new DataSet();
adapter.Fill(ds,"employees");
DataGrid1.DataSource =ds;
DataGrid1.DataBind();
this.DataGrid1.ShowFooter=false;
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.ShowAddNew.Click += new System.EventHandler(this.ShowAddNew_Click);
this.AddNew.Click += new System.EventHandler(this.AddNew_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void AddNew_Click(object sender, System.EventArgs e)
{
//在这里,我们得到新增的值,然后就可以做insert处理了
Response.Write(Request.Form["ddl"].ToString());
Response.Write(Request.Form["firstname"].ToString());
Response.Write(Request.Form["city"].ToString());
}
private void ShowAddNew_Click(object sender, System.EventArgs e)
{
this.DataGrid1.ShowFooter=true;
}
}
}
这种也会存在一些问题,比如FooterText中的控件不能绑定数据。而且也不能是WEB窗体控件. 可能也有解决办法把,我暂时不知道