我們在取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){
HyperLink href = (HyperLink)row.Cells[0].Controls[0];
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>
|
--------------