日期:2014-05-17 浏览次数:20474 次
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default8_CSDN.aspx.cs" Inherits="_Default8_CSDN" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Get GridView's HeaderText and the DataField</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="Button1" runat="server" Text="Get GridView's DataField" OnClick="Button1_Click" /> <p /> <asp:Label ID="Label1" runat="server" Text=""></asp:Label> <p /> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"> <Columns> <asp:BoundField DataField="EmployeeID" HeaderText="ID" ReadOnly="True" /> <asp:TemplateField HeaderText="Name"> <EditItemTemplate> <asp:TextBox ID="gvtxtName" runat="server" Text='<%# Eval("EmployeeName") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="gvlbllName" runat="server" Text='<%# Bind("EmployeeName") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="EmployeeAge" HeaderText="Age" ReadOnly="True" /> </Columns> </asp:GridView> </div> </form> </body> </html>
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class _Default8_CSDN : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { GridView1.DataSource = GetTable(); GridView1.DataBind(); } } /// <summary> Button1 : 列出GridView的HeaderText和所绑定的DataField /// </summary> protected void Button1_Click(object sender, EventArgs e) { System.Text.StringBuilder sb = new System.Text.StringBuilder(); for (int i = 0; i < GridView1.Columns.Count; i++) { sb.Append("第" + (i+1).ToString() + "列 ========="); sb.Append("HeaderText: "); sb.Append(GridView1.Columns[i].HeaderText); sb.Append("========="); sb.Append("DataField: "); sb.Append("???"); // 请问这里怎么写,望高手指教。 sb.Append("<br/>"); } Label1.Text = sb.ToString(); } /// <summary> 创建DataTable /// </summary> /// <returns></returns> private DataTable GetTable() { DataTable dt