|
网友问这个问题,写了一个简单的例子: 前台: 1<%@ Page language="c#" Codebehind="WebForm4.aspx.cs" AutoEventWireup="false" Inherits="WebApplication25.WebForm4" %> 2<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > 3<HTML> 4 <HEAD> 5 <title>WebForm4</title> 6 <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1"> 7 <meta name="CODE_LANGUAGE" Content="C#"> 8 <meta name="vs_defaultClientScript" content="JavaScript"> 9 <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> 10 </HEAD> 11 <body> 12 <form id="Form1" method="post" runat="server"> 13 <asp:DataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False" DataKeyField="EmployeeID"> 14 <Columns> 15 <asp:TemplateColumn> 16 <ItemTemplate> 17 <input type="hidden" id="SelectedID" runat="server" value='<%# Container.ItemIndex%>' name="SelectedID"/> 18 <asp:CheckBox ID="chkExport" Runat="server" /> 19 </ItemTemplate> 20 </asp:TemplateColumn> 21 <asp:BoundColumn DataField="EmployeeID" HeaderText="EmployeeID"></asp:BoundColumn> 22 <asp:TemplateColumn HeaderText="FirstName"> 23 <ItemTemplate> 24 <asp:Label ID="lab_FirstName" Runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"FirstName")%>'> 25 </asp:Label> 26 <asp:TextBox ID="txt_FirstName" Runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"FirstName")%>' Visible="False"> 27 </asp:TextBox> 28 </ItemTemplate> 29 </asp:TemplateColumn> 30 <asp:TemplateColumn HeaderText="LastName"> 31 <ItemTemplate> 32 <asp:Label ID="lab_LastName" Runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"LastName")%>'> 33 </asp:Label> 34 <asp:TextBox ID="txt_LastName" Runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"LastName")%>' Visible="False"> 35 </asp:TextBox> 36 </ItemTemplate> 37 </asp:TemplateColumn> 38 </Columns> 39 </asp:DataGrid> 40 <asp:Button id="Button1" runat="server" Text="编辑"></asp:Button> 41 <asp:Button id="Button2" runat="server" Text="更新"></asp:Button> 42 </form> 43 </body> 44</HTML> 45 46后台: 47 48using System; 49using System.Collections; 50using System.ComponentModel; 51using System.Data; 52using System.Data.SqlClient; 53using System.Drawing; 54using System.Web; 55using System.Web.SessionState; 56using System.Web.UI; 57using System.Web.UI.WebControls; 58using System.Web.UI.HtmlControls; 59 60namespace WebApplication25 61{ 62 /**//// <summary> 63 /// WebForm4 的摘要说明。 64 /// </summary> 65 public class WebForm4 : System.Web.UI.Page 66 { 67 protected System.Web.UI.WebControls.Button Button1; 68 protected System.Web.UI.WebControls.Button Button2; 69 protected System.Web.UI.WebControls.DataGrid DataGrid1; 70 protected System.Web.UI.HtmlControls.HtmlInputHidden SelectID; 71 72 private void Page_Load(object sender, System.EventArgs e) 73 { 74 // 在此处放置用户代码以初始化页面 75 if(!IsPostBack) 76 { 77 SetBind(); 78 } 79 } 80 81 private void SetBind() 82 { 83 SqlConnection conn=new SqlConnection("server=srvxglai-sha;uid=sa;pwd=321413;database=northwind"); 84 SqlDataAdapter da=new SqlDataAdapter("select * from Employees",conn); 85 DataSet ds=new DataSet(); 86 da.Fill(ds,"table1"); 87 this.DataGrid1.DataSource=ds.Tables["table1"]; 88 this.DataGrid1.DataBind(); 89 } 90 91 92 Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码 93 override protected void OnInit(EventArgs e) 94 { 95 // 96 // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。 97 // 98 InitializeComponent(); 99 base.OnInit(e); 100 } 101 102 /**//// <summary> 103 /// 设计器支持所需的方法 - 不要使用代码编辑器修改 104 /// 此方法的内容。 105 /// </summary> 106 private void InitializeComponent() 107 { 108 this.Button1.Click += new System.EventHandler(this.Button1_Click); 109 this.Button2.Click += new System.EventHandler(this.Button2_Click); 110 this.Load += new System.EventHandler(this.Page_Load); 111 112 } 113 #endregion 114 115 private void Button1_Click(object sender, System.EventArgs e) 116 { 117 foreach(DataGridItem di in this.DataGrid1.Items) 118 { 119 if(((CheckBox)di.FindControl("chkExport")).Checked==true) 120 { 121 ((TextBox)di.FindControl("txt_FirstName")).Visible=true; 122 ((TextBox)di.FindControl("txt_LastName")).Visible=true; 123 ((Label)di.FindControl("lab_FirstName")).Visible=false; 124 ((Label)di.FindControl("lab_LastName")).Visible=false; 125 } 126 } 127 128 } 129 130 private void Button2_Click(object sender, System.EventArgs e) 131 { 132 foreach(DataGridItem di in this.DataGrid1.Items) 133 { 134 if(((CheckBox)di.FindControl("chkExport")).Checked==true) 135 { 136 if(((TextBox)di.FindControl("txt_FirstName")).Text!=((Label)di.FindControl("lab_FirstName")).Text||((TextBox)di.FindControl("txt_LastName")).Text!=((Label)di.FindControl("lab_LastName")).Text) 137 { 138 Response.Write("记录号:"+this.DataGrid1.DataKeys[int.Parse(((HtmlInputHidden)di.FindControl("SelectedID")).Value)]+",FirstName:"+((TextBox)di.FindControl("txt_FirstName")).Text+",LastName:"+((TextBox)di.FindControl("txt_LastName")).Text+"<br>"); 139 } 140 ((TextBox)di.FindControl("txt_FirstName")).Visible=false; 141 ((TextBox)di.FindControl("txt_LastName")).Visible=false; 142 ((Label)di.FindControl("lab_FirstName")).Visible=true; 143 ((Label)di.FindControl("lab_LastName")).Visible=true; 144 ((CheckBox)di.FindControl("chkExport")).Checked=false; 145 } 146 } 147 } 148 } 149} 150
|