如何取得GridView中的HyperLinkField 的Text

來源:互聯網
上載者:User
我們在取BoundField的時候只要
string txt = this.GridView1.Rows[0].Cells[1].Text;
就可以取得秀在畫面的文字
但是HyperLinkField就沒那麼順利了
string txt = this.GridView1.Rows[0].Cells[0].Text;
若你執行上述的程式碼 , 你只會拿到一個空字串

由於HyperLinkField的Text屬性 是用來固定超連結的文字 ,
MSDN有提到
If the DataTextField and Text properties are both set, the DataTextField property takes precedence.

所以我們都是用DataTextField與資料做連繫 , 這時候問題來了 , 若我們要取得這個資料呢 ?
如資料秀 A123456789 我們要它做KEY去做一些處理 .
我們可以用

1
2
  HyperLink href = (HyperLink)row.Cells[0].Controls[0];            string text = href.Text;            

來取得超連結的文字.
以下為範例:
-----------------------------------------------------------
Default3.aspx.cs
-----------------------------------------------------------

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
using System;            using System.Data;            using System.Data.SqlClient;            using System.Configuration;            using System.Collections;            using System.Web;            using System.Web.Security;            using System.Web.UI;            using System.Web.UI.WebControls;            using System.Web.UI.WebControls.WebParts;            using System.Web.UI.HtmlControls;                         public partial class Default3 : System.Web.UI.Page            {            protected void Page_Load(object sender, EventArgs e)            {            if (IsPostBack)            {            foreach( TableRow row in this.GridView1.Rows){            // 取得Cell內的超連結控制項 , 並轉換型別 , 用於 HyperLinkField             HyperLink href = (HyperLink)row.Cells[0].Controls[0];            // 取得Cell內的文字 , 用於 BoundField             string txt = row.Cells[1].Text;                         Response.Write( href.Text + " " + row.Cells[1].Text + "<br>" );            }            }            }            }            

-----------------------------------------------------------
Default3.aspx
-----------------------------------------------------------

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" Title="Untitled Page" %>            <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">            <Columns>            <asp:HyperLinkField DataNavigateUrlFields="FID" DataNavigateUrlFormatString="~/Default.aspx?FID={0}"            DataTextField="FID" HeaderText="FID" Target="_self"  />            <asp:BoundField DataField="EID" HeaderText="EID" SortExpression="EID" />            <asp:BoundField DataField="DESC" HeaderText="DESC" SortExpression="DESC" />            </Columns>            </asp:GridView>            <asp:Button ID="Button1" runat="server" Text="Button" /><br />            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"            ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="SELECT [FID], [EID], [DESC] FROM [RECORD]">            </asp:SqlDataSource>            <br />            </asp:Content>            

--------------

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.