GridView的DataKeyNames屬性 轉載的

來源:互聯網
上載者:User

偶今天用到這個了,轉載  "事在人為"樓主的,原文地址: http://www.cnblogs.com/andhm/archive/2010/05/07/1730024.html

 

DataKeyNames表示主鍵的列名,可以通過GridViewEntity.DataKeys[RowIndex]["ColumsName"]來擷取他的值,當然它是不會顯示出來的,其實我是在一個用SqlDataSource中發現的,看到了有這麼一個主鍵而實現更新,當然在我們多層開發中也不能缺少這個。當我們在用Template時怎麼取得值呢?我們可以把那個列也設為DataKeyNames中,記得多個要用","隔開。
如果沒有設定成DataKeyNames,那隻能通過GridViewEntity.Rows[RowIndex].Cell[Index].Text來得到值了,不知老兄有沒有看到一些HTML字元呢? 這個最是經常見得了,不用當心我們可以用HttpUnility.HtmlDecode()來解決他!

 

 

在我們使用GridView的過程中,經常會遇到這樣對問題,我們選擇某一行進行編輯,或選擇某一行刪除或者.......時,我們需要擷取當前行的某些資訊,尤其是當前行的主鍵資訊,主鍵資訊一般不顯示在頁面,此時我們有三種方法來處理。 第一種是使用DataKeyNames ,這裡要重點介紹的。 第二種是使用按鈕的CommandArgument屬性邦定需要的資訊。 第三種是最古老最通用的方法使用隱藏的方法顯示。

第一種方法:使用DataKeyNames,DataKeyNames可邦定一列,也可邦定多列 前台:DataKeyNames="FID"   綁定一個值 後台:GridView1.DataKeys[e.Row.RowIndex].Value.ToString();

前台:DataKeyNames="FID,FName"   綁定兩個值 後台:GridView1.DataKeys[e.Row.RowIndex].Values[0].ToString(); 後台:GridView1.DataKeys[e.Row.RowIndex].Values[1].ToString(); 或者 後台:GridView1.DataKeys[e.Row.RowIndex].Values["FID"].ToString(); 後台:GridView1.DataKeys[e.Row.RowIndex].Values["FName"].ToString();

第二種方法:是用按鈕的CommandArgument屬性邦定需要的資訊。

典型的例子:下載附件列(有附件的顯示下載連結,無附件的顯示為空白) <asp:TemplateColumn HeaderText="附件"> <HeaderStyle Width="7%"></HeaderStyle> <ItemTemplate> <asp:LinkButton id="LinkButton1" CommandName="download" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "attached_file")%>' runat="server" Visible='<%# ((DataBinder.Eval(Container.DataItem, "attached_file").ToString()) != "") %>'>下載</asp:LinkButton> </ItemTemplate> </asp:TemplateColumn>

在dgHKStock_ItemCommand事件中: if ("download" == e.CommandName) {     mybc.SystemOverTime0();          // 判斷Session是否到期     mybc.RightManage("16010500");    // 判斷使用者是否有開啟此網頁的許可權     mybc.HTTP_DownloadFile(e.CommandArgument.ToString()); // 取得當前存貨資訊的附件存放路徑

}

 

最古老最通用的方法使用隱藏的方法顯示。

<style type="text/css">     .test{         display:none;     }     </style>

<asp:TemplateField HeaderText="姓名" SortExpression="name" ItemStyle-CssClass="test" HeaderStyle-CssClass="test"> <ItemTemplate>     <asp:Label ID="Label1" runat="server" Text='<%# Eval("name") %>' ></asp:Label>     </ItemTemplate>    </asp:TemplateField>

 

把隱藏列轉成模版列,通過FindControl訪問模版列的內容:

<asp:TemplateField Visible="false">                 <ItemTemplate>                 <asp:Label runat="server" Text='<%#Eval("id") %>' ID="lblId"></asp:Label>                 </ItemTemplate> </asp:TemplateField>

 

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)     {         Label lblId=GridView1.Rows[e.NewEditIndex].Cells[0].FindControl("lblId") as Label;         Response.Write(lblId.Text);     }

偶今天用到這個了,轉載  "事在人為"樓主的,原文地址: http://www.cnblogs.com/andhm/archive/2010/05/07/1730024.html

 

DataKeyNames表示主鍵的列名,可以通過GridViewEntity.DataKeys[RowIndex]["ColumsName"]來擷取他的值,當然它是不會顯示出來的,其實我是在一個用SqlDataSource中發現的,看到了有這麼一個主鍵而實現更新,當然在我們多層開發中也不能缺少這個。當我們在用Template時怎麼取得值呢?我們可以把那個列也設為DataKeyNames中,記得多個要用","隔開。
如果沒有設定成DataKeyNames,那隻能通過GridViewEntity.Rows[RowIndex].Cell[Index].Text來得到值了,不知老兄有沒有看到一些HTML字元呢?&nbsp;這個最是經常見得了,不用當心我們可以用HttpUnility.HtmlDecode()來解決他!

 

 

在我們使用GridView的過程中,經常會遇到這樣對問題,我們選擇某一行進行編輯,或選擇某一行刪除或者.......時,我們需要擷取當前行的某些資訊,尤其是當前行的主鍵資訊,主鍵資訊一般不顯示在頁面,此時我們有三種方法來處理。 第一種是使用DataKeyNames ,這裡要重點介紹的。 第二種是使用按鈕的CommandArgument屬性邦定需要的資訊。 第三種是最古老最通用的方法使用隱藏的方法顯示。

第一種方法:使用DataKeyNames,DataKeyNames可邦定一列,也可邦定多列 前台:DataKeyNames="FID"   綁定一個值 後台:GridView1.DataKeys[e.Row.RowIndex].Value.ToString();

前台:DataKeyNames="FID,FName"   綁定兩個值 後台:GridView1.DataKeys[e.Row.RowIndex].Values[0].ToString(); 後台:GridView1.DataKeys[e.Row.RowIndex].Values[1].ToString(); 或者 後台:GridView1.DataKeys[e.Row.RowIndex].Values["FID"].ToString(); 後台:GridView1.DataKeys[e.Row.RowIndex].Values["FName"].ToString();

第二種方法:是用按鈕的CommandArgument屬性邦定需要的資訊。

典型的例子:下載附件列(有附件的顯示下載連結,無附件的顯示為空白) <asp:TemplateColumn HeaderText="附件"> <HeaderStyle Width="7%"></HeaderStyle> <ItemTemplate> <asp:LinkButton id="LinkButton1" CommandName="download" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "attached_file")%>' runat="server" Visible='<%# ((DataBinder.Eval(Container.DataItem, "attached_file").ToString()) != "") %>'>下載</asp:LinkButton> </ItemTemplate> </asp:TemplateColumn>

在dgHKStock_ItemCommand事件中: if ("download" == e.CommandName) {     mybc.SystemOverTime0();          // 判斷Session是否到期     mybc.RightManage("16010500");    // 判斷使用者是否有開啟此網頁的許可權     mybc.HTTP_DownloadFile(e.CommandArgument.ToString()); // 取得當前存貨資訊的附件存放路徑

}

 

最古老最通用的方法使用隱藏的方法顯示。

<style type="text/css">     .test{         display:none;     }     </style>

<asp:TemplateField HeaderText="姓名" SortExpression="name" ItemStyle-CssClass="test" HeaderStyle-CssClass="test"> <ItemTemplate>     <asp:Label ID="Label1" runat="server" Text='<%# Eval("name") %>' ></asp:Label>     </ItemTemplate>    </asp:TemplateField>

 

把隱藏列轉成模版列,通過FindControl訪問模版列的內容:

<asp:TemplateField Visible="false">                 <ItemTemplate>                 <asp:Label runat="server" Text='<%#Eval("id") %>' ID="lblId"></asp:Label>                 </ItemTemplate> </asp:TemplateField>

 

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)     {         Label lblId=GridView1.Rows[e.NewEditIndex].Cells[0].FindControl("lblId") as Label;         Response.Write(lblId.Text);     }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.