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去做一些處理 .
我們可以用
HyperLink href = (HyperLink)row.Cells[0].Controls[0]; string text = href.Text; |
來取得超連結的文字.
以下為範例:
-----------------------------------------------------------
Default3.aspx.cs
-----------------------------------------------------------
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; partial Default3 : System.Web.UI.Page Page_Load(object sender, EventArgs e) (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 + ); |
-----------------------------------------------------------
Default3.aspx
-----------------------------------------------------------
<%@ Page Language= MasterPageFile= AutoEventWireup= CodeFile= Inherits= Title= %> <asp:Content ID= ContentPlaceHolderID= Runat=> <asp:GridView ID= runat= AutoGenerateColumns= DataSourceID=> <Columns> <asp:HyperLinkField DataNavigateUrlFields= DataNavigateUrlFormatString= DataTextField= HeaderText= Target= /> <asp:BoundField DataField= HeaderText= SortExpression= /> <asp:BoundField DataField= HeaderText= SortExpression= /> </Columns> </asp:GridView> <asp:Button ID= runat= Text= /><br /> <asp:SqlDataSource ID= runat= ConnectionString= ProviderName= SelectCommand=> </asp:SqlDataSource> <br /> </asp:Content> |
--------------