[C#][ASP.Net]GirdView動態新增cell(含其他元件)

摘要:[C#][ASP.Net]GirdView動態新增cell(含其他元件)

如果只是單純的動態新增一個cell不含任何其他元件,則把下列這行程式

1 //設定cell的文字內容
2 e.Row.Cells[e.Row.Cells.Add(new TableCell())].Text = "Phone";

放到RowDataBound事件中,如果放在e.Row.RowType == DataControlRowType.Header區段中如下

01 if (e.Row.RowType == DataControlRowType.Header)
02 {
03     //新增一個cell
04     e.Row.Cells[e.Row.Cells.Add(new TableCell())].Text = "Phone Number";
05
06
    //設定cell的外觀樣式
07     foreach (TableCell cell in e.Row.Cells)
08     {
09         cell.Width = intCellWidth;
10         cell.BorderColor = Color.Black;
11         cell.BorderWidth = 2;
12         cell.Font.Bold = true;
13         cell.Font.Size = FontUnit.Small;
14     }

15 }

則代表新增GridView的Title;同理放在e.Row.RowType == DataControlRowType.DataRow區段中則代表要新增GirdView的body;而放在e.Row.RowType == DataControlRowType.Footer則是新增在尾列。

如果要新增cell同時要內含一個TextBox元件該如何做?

01 //建立TextBox物件
02 TextBox oTB = new TextBox();
03 //開始設定其屬性
04 oTB.ID = "TextBox1";
05 oTB.AutoPostBack = true;
06 oTB.EnableViewState = false;
07 oTB.Attributes.Add("onfocus", "this.select();");
08 //設定TextBox的TextChanged事件
09 oTB.TextChanged += new EventHandler(TextBox_TextChanged);
10 oTB.Text = "1-800-551-1155";
11 //最後把這個TextBox物件加到cell,大功告成
12 e.Row.Cells[e.Row.Cells.Add(new TableCell())].Controls.Add(oTB);
13 oTB = null;

若要變成其他物件可以此類推。

以下範例是整個GridView都用動態方式建立,並給定一個DataTable作為資料來源。

首先先在畫面上放個GirdView,並給一個隱藏的BoundField把整個GirdView給撐起來:

1 <asp:GridView ID="GirdView1" runat="server" AutoGenerateColumns="False" BorderColor="Black"
2   BorderWidth="2px" EnableTheming="True" OnRowDataBound="GirdView1_RowDataBound">
3   <RowStyle BackColor="Cornsilk" BorderColor="Black" HorizontalAlign="Left" />
4   <Columns>
5     <asp:BoundField HeaderText="HiddenField" Visible="False" />
6   </Columns>
7   <HeaderStyle BackColor="CadetBlue" BorderColor="Black" Font-Size="Smaller" />
8   <AlternatingRowStyle BackColor="LightCyan" />
9 </asp:GridView>

後置程式碼:

這邊偷懶所以直接利用SqlDataSource撈出資料當作來源,不然一般我都是手工寫BLL和DAL,可讓程式保有很大的彈性和擴充性。

1 DataTable dt = ((DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty)).Table;
2 GridView1.DataSource = dt;
3 GridView1.DataBind();

接下來設定GridView1的RowDataBound事件:

01 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
02 {
03     if (e.Row.RowType == DataControlRowType.Header)
04     {
05         //增加cell並設定其值
06         for (int i = 0; i < dt.Columns.Count; i++)
07         {
08             e.Row.Cells[e.Row.Cells.Add(new TableCell())].Text = dt.Columns[i].Caption;
09         }

10
11
        //設定cell外觀屬性
12         foreach (TableCell cell in e.Row.Cells)
13         {
14             cell.Width = intCellWidth;
15             cell.BorderColor = Color.Black;
16             cell.BorderWidth = 2;
17             cell.Font.Bold = true;
18             cell.Font.Size = FontUnit.Small;
19         }

20     }

21     else if (e.Row.RowType == DataControlRowType.DataRow)
22     {
23         TextBox oTB;
24
25
        for (int i = 0; i < dt.Columns.Count; i++)
26         {
27             oTB = new TextBox();
28             //開始設定屬性值
29             oTB.ID = "TextBox" + (i + 1);
30             oTB.Width = 70;
31             oTB.AutoPostBack = true;
32             oTB.EnableViewState = true;
33             oTB.Style.Add("text-align", "center");
34             //這邊我針對某個欄位設定和其他不同的限制
35             oTB.MaxLength = (i == 3) ? 2 : 30;
36             //設定TextChanged事件
37             oTB.TextChanged += new EventHandler(TextBox_TextChanged);
38             //把資料來源繫結到TextBox上
39             oTB.Text = Convert.ToString(DataBinder.Eval(e.Row.DataItem, dt.Columns[i].Caption));
40             //最後把這個物件加到cell
41             e.Row.Cells[e.Row.Cells.Add(new TableCell())].Controls.Add(oTB);
42             //回收繼續使用
43             oTB = null;
44         }

45
46
        //設定cell外觀屬性
47         //......
48     }

49 }

 

10/5/2009 補充:

動態產生的東西每次postback之後都會消失,你自己必須要再次建立一遍,資料也要重新DataBind()。如果要紀錄client端所輸入的值,要利用網頁postback回server的機會把它除存下來,最常用也最好用的莫過於TextChanged事件。