利用ASP.NET程式動態產生GridView的TemplateField欄位,並且繫結資料欄位
最近在MSDN看到一個不錯的範例...
介紹如何動態產生GridView的TemplateField
小弟透過範例的參考也寫了一個簡單的範例
分享給大家呀....c#範例
Template.aspx
01 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Template.aspx.cs" Inherits="Template" %>
02
03 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
04 <html xmlns="http://www.w3.org/1999/xhtml">
05 <head id="Head1" runat="server">
06 <title>TemplateField</title>
07 </head>
08 <body>
09 <form id="form1" runat="server">
10 <div>
11 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id"
12 DataSourceID="SqlDataSource1">
13 </asp:GridView>
14 </div>
15 <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString %>"
16 SelectCommand="SELECT [id], [name] FROM [user]"></asp:SqlDataSource>
17 </form>
18 </body>
19 </html>
20
02
03 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
04 <html xmlns="http://www.w3.org/1999/xhtml">
05 <head id="Head1" runat="server">
06 <title>TemplateField</title>
07 </head>
08 <body>
09 <form id="form1" runat="server">
10 <div>
11 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id"
12 DataSourceID="SqlDataSource1">
13 </asp:GridView>
14 </div>
15 <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString %>"
16 SelectCommand="SELECT [id], [name] FROM [user]"></asp:SqlDataSource>
17 </form>
18 </body>
19 </html>
20
Template.aspx.cs
01 using System;
02 using System.Data;
03 using System.Configuration;
04 using System.Collections;
05 using System.Web;
06 using System.Web.Security;
07 using System.Web.UI;
08 using System.Web.UI.WebControls;
09 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11
12 public partial class Template : System.Web.UI.Page
13 {
14 protected void Page_Load(object sender, EventArgs e)
15 {
16 if (!IsPostBack)
17 {
18
19 //宣告TemplateField
20 TemplateField customField;
21
22 //產生一個新的TemplateField
23 customField = new TemplateField();
24
25 //設定TemplateField的Header
26 customField.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, "編號");
27
28 //設定TemplateField的Data
29 customField.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "id");
30
31 //加入TemplateField到GridView
32 this.GridView1.Columns.Add(customField);
33
34 customField = new TemplateField();
35 customField.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, "名稱");
36 customField.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "name");
37 this.GridView1.Columns.Add(customField);
38
39 }
40 }
41 }
42
43 public class GridViewTemplate : ITemplate
44 {
45 private DataControlRowType templateType;
46 private string columnName;
47
48 public GridViewTemplate(DataControlRowType type, string colname)
49 {
50 templateType = type;
51 columnName = colname;
52 }
53
54 public void InstantiateIn(System.Web.UI.Control container)
55 {
56 switch (templateType)
57 {
58 case DataControlRowType.Header:
59
60 Literal lc = new Literal();
61
62 lc.Text = "<B>" + columnName + "</B>";
63
64 container.Controls.Add(lc);
65
66 break;
67
68 case DataControlRowType.DataRow:
69
70 Label data = new Label();
71
72 data.DataBinding += new EventHandler(data_DataBinding);
73
74 container.Controls.Add(data);
75
76 break;
77
78 default:
79
80 break;
81 }
82 }
83
84 private void data_DataBinding(object sender, EventArgs e)
85 {
86 Label l = (Label)sender;
87
88 GridViewRow row = (GridViewRow)l.NamingContainer;
89
90 l.Text = DataBinder.Eval(row.DataItem, columnName).ToString();
91 }
92
93 }
02 using System.Data;
03 using System.Configuration;
04 using System.Collections;
05 using System.Web;
06 using System.Web.Security;
07 using System.Web.UI;
08 using System.Web.UI.WebControls;
09 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11
12 public partial class Template : System.Web.UI.Page
13 {
14 protected void Page_Load(object sender, EventArgs e)
15 {
16 if (!IsPostBack)
17 {
18
19 //宣告TemplateField
20 TemplateField customField;
21
22 //產生一個新的TemplateField
23 customField = new TemplateField();
24
25 //設定TemplateField的Header
26 customField.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, "編號");
27
28 //設定TemplateField的Data
29 customField.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "id");
30
31 //加入TemplateField到GridView
32 this.GridView1.Columns.Add(customField);
33
34 customField = new TemplateField();
35 customField.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, "名稱");
36 customField.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, "name");
37 this.GridView1.Columns.Add(customField);
38
39 }
40 }
41 }
42
43 public class GridViewTemplate : ITemplate
44 {
45 private DataControlRowType templateType;
46 private string columnName;
47
48 public GridViewTemplate(DataControlRowType type, string colname)
49 {
50 templateType = type;
51 columnName = colname;
52 }
53
54 public void InstantiateIn(System.Web.UI.Control container)
55 {
56 switch (templateType)
57 {
58 case DataControlRowType.Header:
59
60 Literal lc = new Literal();
61
62 lc.Text = "<B>" + columnName + "</B>";
63
64 container.Controls.Add(lc);
65
66 break;
67
68 case DataControlRowType.DataRow:
69
70 Label data = new Label();
71
72 data.DataBinding += new EventHandler(data_DataBinding);
73
74 container.Controls.Add(data);
75
76 break;
77
78 default:
79
80 break;
81 }
82 }
83
84 private void data_DataBinding(object sender, EventArgs e)
85 {
86 Label l = (Label)sender;
87
88 GridViewRow row = (GridViewRow)l.NamingContainer;
89
90 l.Text = DataBinder.Eval(row.DataItem, columnName).ToString();
91 }
92
93 }