日期:2014-05-17 浏览次数:20541 次
<%@ Page Language="C#" EnableViewState="true" EnableEventValidation="false" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> public System.Data.DataTable CreateDataTable() { System.Data.DataTable dataTable1 = new System.Data.DataTable("BlogUser"); System.Data.DataRow dr; dataTable1.Columns.Add(new System.Data.DataColumn("Id", typeof(System.Int32))); dataTable1.Columns.Add(new System.Data.DataColumn("Title", typeof(System.String))); for (int i = 0; i < 8; i++) { dr = dataTable1.NewRow(); dr[0] = i; dr[1] = Guid.NewGuid().ToString(); dataTable1.Rows.Add(dr); } return dataTable1; } protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { Data_g.DataSource = this.CreateDataTable(); Data_g.DataBind(); } } protected void Data_g_ItemCommand(object source, DataGridCommandEventArgs e) { if (e.CommandName == "See") { Response.Write("See 引发"); } if (e.CommandName == "SeeSee") { Response.Write("SeeSee 引发"); } Response.Write("<li>该行Id=" + Data_g.DataKeys[e.Item.ItemIndex].ToString()); } protected void see3_Click(object sender, EventArgs e) { Button s = sender as Button; Response.Write("<li>方法3得到的Id=" + s.CommandArgument); } protected void see4_Click(object sender, EventArgs e) { Button s = sender as Button; DataGridItem item = s.NamingContainer as DataGridItem; Response.Write("<li>方法4得到的Id" + Data_g.DataKeys[item.ItemIndex].ToString()); } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title>无标题页</title> </head> <body> <form id="form1" runat="server"> <asp:DataGrid ID="Data_g" runat="server" OnItemCommand="Data_g_ItemCommand" DataKeyField="Id" AutoGenerateColumns="false"> <Columns> <asp:ButtonColumn ButtonType="PushButton" CommandName="SeeSee" Text="查看密保卡方法1"></asp:ButtonColumn> <asp:TemplateColumn> <ItemTemplate> <asp:Button ID="see" runat="server" Text="查看密保卡方法2" CommandName="See" /> </ItemTemplate> </asp:TemplateColumn>