DataKeyNames屬性很多時(shí)候我們需要在
GridView的RowCommand之類的事件中需要獲取當(dāng)前行的一些關(guān)聯(lián)性的數(shù)據(jù)值。但這些數(shù)據(jù)值又沒有直接體現(xiàn)在GridView的列中。這個(gè)時(shí)候該怎么辦呢?
有的同學(xué)喜歡用隱藏列的方式,把需要使用但不顯示的字段綁定到此列上,同時(shí)設(shè)置列寬為0或不顯示,使用時(shí)可以用常規(guī)的取某行某列的方式來獲取數(shù)據(jù)。
但是在Framework 2.0中,我們可以采用DataKeyNames的方式來獲取此類數(shù)據(jù)。
Grup 為我們想使用但不需要顯示的列。(如果有多個(gè)字段,使用逗號(hào)分開)
.aspx
<asp:GridView ID="GridView1" runat="server" DataKeyNames="Grup" OnRowCommand="GridView1_RowCommand" >
.cs
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
// 獲取當(dāng)前行索引
int index = Convert.ToInt32(e.CommandArgument);
// 取出當(dāng)前行數(shù)據(jù)鍵值對(duì)象中的值
string strGrup = ((GridView)sender).DataKeys[index].Values["Grup"].ToString();
}
PageIndexChanged
在單擊某一頁導(dǎo)航按鈕時(shí),但在 GridView 控件處理分頁操作之后發(fā)生。此事件通常用于以下情形:在用戶定位到該控件中的另一頁之后,您需要執(zhí)行某項(xiàng)任務(wù)。
//翻頁
protected void AspNetPager1_PageChanging(object src, PageChangingEventArgs e)
{
AspNetPager1.CurrentPageIndex = e.NewPageIndex;
string recordNumber = "0";
DataTable dt = GetSearchResult(AspNetPager1.CurrentPageIndex.ToString(), ref recordNumber);
if (dt != null && dt.Rows.Count > 0)
{
GridDataBind(dt, recordNumber);
}
}
PageIndexChanging
在單擊某一頁導(dǎo)航按鈕時(shí),但在 GridView 控件處理分頁操作之前發(fā)生。此事件通常用于取消分頁操作。
.aspx <PagerTemplate> 與<columns>同級(jí)別
<PagerTemplate>
<table width="100%">
<tr>
<td style="text-align: right">
第<asp:Label ID="lblPageIndex" runat="server" Text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1 %>' />頁
共<asp:Label ID="lblPageCount" runat="server" Text='<%# ((GridView)Container.Parent.Parent).PageCount %>' />頁
<asp:LinkButton ID="btnFirst" runat="server" CausesValidation="False" CommandArgument="First"
CommandName="Page" Text="首頁" />
<asp:LinkButton ID="btnPrev" runat="server" CausesValidation="False" CommandArgument="Prev"
CommandName="Page" Text="上一頁" />
<asp:LinkButton ID="btnNext" runat="server" CausesValidation="False" CommandArgument="Next"
CommandName="Page" Text="下一頁" />
<asp:LinkButton ID="btnLast" runat="server" CausesValidation="False" CommandArgument="Last"
CommandName="Page" Text="尾頁" />
<asp:TextBox ID="txtNewPageIndex" runat="server" Width="20px" Text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1 %>' />
<asp:LinkButton ID="btnGo" runat="server" CausesValidation="False" CommandArgument="-1"
CommandName="Page" Text="GO" /><!-- here set the CommandArgument of the Go Button to '-1' as the flag -->
</td>
</tr>
</table>
</PagerTemplate>
.cs
protected void gvShippingApply_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView theGrid = sender as GridView; // refer to the GridView
int newPageIndex = 0;
if (-2 == e.NewPageIndex)
{ // when click the "GO" Button
TextBox txtNewPageIndex = null;
//GridViewRow pagerRow = theGrid.Controls[0].Controls[theGrid.Controls[0].Controls.Count - 1] as GridViewRow; // refer to PagerTemplate
GridViewRow pagerRow = theGrid.BottomPagerRow; //GridView較DataGrid提供了更多的API,獲取分頁塊可以使用BottomPagerRow 或者TopPagerRow,當(dāng)然還增加了HeaderRow和FooterRow
if (null != pagerRow)
{
txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox; // refer to the TextBox with the NewPageIndex value
}
if (null != txtNewPageIndex)
{
newPageIndex = int.Parse(txtNewPageIndex.Text) - 1; // get the NewPageIndex
}
}
else
{ // when click the first, last, previous and next Button
newPageIndex = e.NewPageIndex;
}
// check to prevent form the NewPageIndex out of the range
newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
newPageIndex = newPageIndex >= theGrid.PageCount ? theGrid.PageCount - 1 : newPageIndex;
// specify the NewPageIndex
theGrid.PageIndex = newPageIndex;
this.BindData();//重新綁定數(shù)據(jù)
}
RowCancelingEdit
在單擊某一行的“取消”按鈕時(shí),但在 GridView 控件退出編輯模式之前發(fā)生。此事件通常用于停止取消操作。
RowCommand
當(dāng)單擊 GridView 控件中的按鈕時(shí)發(fā)生。此事件通常用于在控件中單擊按鈕時(shí)執(zhí)行某項(xiàng)任務(wù)。
RowCreated
當(dāng)在 GridView 控件中創(chuàng)建新行時(shí)發(fā)生。此事件通常用于在創(chuàng)建行時(shí)修改行的內(nèi)容。
RowDataBound
在 GridView 控件中將數(shù)據(jù)行綁定到數(shù)據(jù)時(shí)發(fā)生。此事件通常用于在行綁定到數(shù)據(jù)時(shí)修改行的內(nèi)容。
RowDeleted
在單擊某一行的“刪除”按鈕時(shí),但在 GridView 控件從數(shù)據(jù)源中刪除相應(yīng)記錄之后發(fā)生。此事件通常用于檢查刪除操作的結(jié)果。
RowDeleting
在單擊某一行的“刪除”按鈕時(shí),但在 GridView 控件從數(shù)據(jù)源中刪除相應(yīng)記錄之前發(fā)生。此事件通常用于取消刪除操作。
RowEditing
發(fā)生在單擊某一行的“編輯”按鈕以后,GridView 控件進(jìn)入編輯模式之前。此事件通常用于取消編輯操作。
RowUpdated
發(fā)生在單擊某一行的“更新”按鈕,并且 GridView 控件對(duì)該行進(jìn)行更新之后。此事件通常用于檢查更新操作的結(jié)果。
RowUpdating
發(fā)生在單擊某一行的“更新”按鈕以后,GridView 控件對(duì)該行進(jìn)行更新之前。此事件通常用于取消更新操作。
SelectedIndexChanged
發(fā)生在單擊某一行的“選擇”按鈕,GridView 控件對(duì)相應(yīng)的選擇操作進(jìn)行處理之后。此事件通常用于在該控件中選定某行之后執(zhí)行某項(xiàng)任務(wù)。
SelectedIndexChanging
發(fā)生在單擊某一行的“選擇”按鈕以后,GridView 控件對(duì)相應(yīng)的選擇操作進(jìn)行處理之前。此事件通常用于取消選擇操作。
Sorted
在單擊用于列排序的超鏈接時(shí),但在 GridView 控件對(duì)相應(yīng)的排序操作進(jìn)行處理之后發(fā)生。此事件通常用于在用戶單擊用于列排序的超鏈接之后執(zhí)行某個(gè)任務(wù)。
Sorting
在單擊用于列排序的超鏈接時(shí),但在 GridView 控件對(duì)相應(yīng)的排序操作進(jìn)行處理之前發(fā)生。此事件通常用于取消排序操作或執(zhí)行自定義的排序例程。