(1)不允許重新綁定的情況下,編碼設定ShowHeader,ShowFooter屬性:
頁面上放一個 <asp:GridView ID="GridView1" runat="server" AllowPaging="True" PageSize="5" ShowFooter="True" AllowSorting="True"> </asp:GridView>
不使用DataSource控制項,而是用編程方式對其進行綁定; 有兩個按鈕,一個是將ShowHeader,ShowFooter屬性設定為True,另一個則是設定為False。執行,點擊ButtonF,一次沒反應,點兩次,Header和Footer都不見了;再點擊ButtonT,Header和Footer可能就不會再顯示了。
用DataSource控制項進行綁定就不會有這個問題,但是,這會自動從資料庫讀取資料並重新綁定。
1protected void Page_Load(object sender, EventArgs e)
2 {
3 if (!IsPostBack) this.BindTestData();
4 }
5 protected void BindTestData()
6 {
7 //編碼擷取未經處理資料並綁定到GridView
8 }
9 protected void ButtonT_Click(object sender, EventArgs e)
10 {
11 this.GridView1.ShowHeader = true;
12 this.GridView1.ShowFooter = true;
13 //this.GridView1.AllowPaging = true;
14
15 }
16 protected void ButtonF_Click(object sender, EventArgs e)
17 {
18 this.GridView1.ShowHeader = false;
19 this.GridView1.ShowFooter = false;
20 //this.GridView1.AllowPaging = false;
21
22 }
(2)設定GridView的HeaderStyle.Font.Bold屬性為False,沒有效果:
這隻是個小問題而已,但是明顯是不對的。GridView控制項的列頭,會產生<th></th>標籤,但是如果使用者佈建了該屬性為False,應該要有所體現,然而沒有,我們不得不通過下面的代碼才能讓列頭不用粗體顯示:1protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
2 {
3 if (e.Row.RowType == DataControlRowType.Header)
4 {
5 foreach(TableCell cell in e.Row.Cells)
6 cell.Attributes.Add("style","FONT-WEIGHT:normal");
7 }
8 }
9
(3)在asp.net 2.0中使用DataGrid控制項,在下面的特定情況下Pager的樣式會有變化:
<asp:DataGrid ID="DataGrid1" runat="server" AllowPaging="True" PageSize="2" AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn DataField="id" HeaderText="ID"></asp:BoundColumn>
<asp:BoundColumn DataField="name" HeaderText="NAME"></asp:BoundColumn>
<asp:BoundColumn DataField="sex" HeaderText="SEX"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="選擇"><ItemTemplate>
<asp:LinkButton id="LinkButton1" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Name") %>' CommandName="Select"></asp:LinkButton>
</ItemTemplate></asp:TemplateColumn>
</Columns>
<PagerStyle HorizontalAlign="Center" />
</asp:DataGrid>
綁定到DataSource控制項或編碼綁定到DataView,執行,嘗試驗擊模版列中的LinkButton,Pager的樣式會發生變化,原先整行的Pager變成了2個儲存格。