一、了解Template
AlternatingItemTemplate定義交替行的內容和外觀,如果沒有規定模板,則使用ItemTemplate;
EditItemTemplate定義當前正在編輯的行的內容和外觀。該模板包含輸入字段,而且還可能包含驗證程序;
FooterTemplate定義該行的頁腳的內容和外觀;
HeaderTemplate定義該行的標題的內容和外觀;
ItemTemplate定義該行的默認內容和外觀。
二、模板應用
aspx代碼
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ConferenceNo,VerNum,AttendeeCategory,Attendee" DataSourceID="SqlDataSource1" onrowcommand="GridView1_RowCommand" onrowdatabound="GridView1_RowDataBound" onrowcreated="GridView1_RowCreated"> <Columns> 其它字段 <asp:TemplateField> <ItemTemplate> <asp:Button ID="btnDel" runat="server" CommandArgument='<%# Eval("ConferenceNo") %>' onclick="btnDel_Click" Text="del" /> <asp:LinkButton ID="LinkButton1" runat="server" CommandArgument='<%# Eval("ConferenceNo") %>' CommandName="2">Link1</asp:LinkButton> <asp:LinkButton ID="LinkButton2" runat="server" CommandName="3">Link2</asp:LinkButton> <asp:LinkButton ID="LinkButton3" runat="server" CommandArgument='<%# ((GridViewRow)Container).RowIndex %>' CommandName="4">Link3</asp:LinkButton> <asp:LinkButton ID="LinkButton4" runat="server">Link4</asp:LinkButton> </ItemTemplate> </asp:TemplateField> <asp:ButtonField CommandName="1" Text="按鈕" /> </Columns> </asp:GridView>
aspx.cs代碼
/// <summary> /// 2、模板中自定義Button和CommandArgument /// </summary> protected void btnDel_Click(object sender, EventArgs e) { string strCommandArgument = ((Button)sender).CommandArgument; ClientScript.RegisterStartupScript(this.GetType(), "alter", "alert('" + strCommandArgument + "')",true); } /// <summary> /// 1、ButtonField和RowCommand /// </summary> protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { //1、ButtonField和RowCommand if (e.CommandName == "1") { //在ButtonField中CommandArgument屬性是當前行索引(RowIndex)不需要開發人員設置 int intRowIndex = int.Parse(e.CommandArgument.ToString()); string strConferenceNo = GridView1.Rows[intRowIndex].Cells[0].Text.ToString(); ClientScript.RegisterStartupScript(this.GetType(), "alter", "alert('" + strConferenceNo + "')", true); } //3、模板中自定義Button和RowCommand if (e.CommandName == "2") { //自定義Button中CommandArgument屬性是開發人員設置 string strConferenceNo = e.CommandArgument.ToString(); ClientScript.RegisterStartupScript(this.GetType(), "alter", "alert('" + strConferenceNo + "')", true); } //4、模板中自定義Button和RowCommand if (e.CommandName == "3") { //在RowDataBound針對模板中自定義Button的CommandArgument賦值 int intRowIndex = int.Parse(e.CommandArgument.ToString()); string strConferenceNo = GridView1.Rows[intRowIndex].Cells[0].Text.ToString(); ClientScript.RegisterStartupScript(this.GetType(), "alter", "alert('" + strConferenceNo + "')", true); } //5、模板中自定義Button和RowCommand if (e.CommandName == "4") { //CommandArgument='<%# ((GridViewRow)Container).RowIndex %>' int intRowIndex = int.Parse(e.CommandArgument.ToString()); string strConferenceNo = GridView1.Rows[intRowIndex].Cells[0].Text.ToString(); ClientScript.RegisterStartupScript(this.GetType(), "alter", "alert('" + strConferenceNo + "')", true); } } /// <summary> /// 行綁定事件 /// 1、常用於行選擇事件注冊 /// 2、特殊數據處理 /// </summary> protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { //4、針對模板中自定義Button的CommandArgument賦值 if (e.Row.RowType == DataControlRowType.DataRow) { LinkButton lnk = (LinkButton)e.Row.FindControl("LinkButton2"); lnk.CommandArgument = e.Row.RowIndex.ToString(); } } /// <summary> /// GridView行創建后 /// </summary> protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) { //5、針對模板中自定義Button的CommandArgument賦值 if (e.Row.RowType == DataControlRowType.DataRow) { LinkButton lnk = (LinkButton)e.Row.FindControl("LinkButton4"); lnk.Click += new EventHandler(lnk_Click);//按+=再按2次Tab鍵,實現快速注冊事件 } } void lnk_Click(object sender, EventArgs e) { //獲取當前行 GridViewRow grdRow = (GridViewRow)((LinkButton)sender).Parent.Parent; string strConferenceNo = grdRow.Cells[0].Text.ToString(); ClientScript.RegisterStartupScript(this.GetType(), "alter", "alert('" + strConferenceNo + "')", true); }