2016-12-05 75 views
1

我有來自sql的數據源的gridview。目前,其中一欄是總擔保人,所有數據都鏈接到相同的頁面。但我想使標題的「總列」超鏈接或可點擊其他頁面,而不是全面擔保人欄中的數據。如何使grdview列超鏈接到其他頁面?

下面

是我使用的代碼:

  <asp:HyperLinkField DataTextField="Total Guarantor" DataNavigateUrlFields="Total Guarantor" DataNavigateUrlFormatString="guarantoedetails.aspx?custno={0}" Text="Total Guarantor" HeaderText="Total Guarantor" /> 

+0

那麼你正在嘗試是,當你點擊總計列,那麼它應該重定向到與細節另一個頁面來獲得相關的行數據。是嗎? –

+0

不,現在,Total列中的數據正被超鏈接/重定向到其他頁面(就像代碼一樣)。而不是數據,我想要總列(標題)超鏈接到其他頁面。 – mir

+0

標題可以作爲HyperLink。但是,這樣做的意義何在,您希望如何重定向到其他頁面? –

回答

0

可以使用OnRowCreated事件這一點。

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) 
{ 
    //check if the row is the header row 
    if (e.Row.RowType == DataControlRowType.Header) 
    { 
     //the header cell number where to put the link 
     int cellNumber = 1; 

     //cast the cell in the header 
     TableCell headerCell = e.Row.Cells[cellNumber]; 

     //create a hyperlink 
     HyperLink link = new HyperLink(); 
     link.Text = "myLink"; 
     link.NavigateUrl = "/otherPage.aspx"; 

     //remove existing controls from the cell 
     headerCell.Controls.Clear(); 

     //add the hyperlink to the cell 
     headerCell.Controls.Add(link); 
    } 
} 

VB

Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs) 
    'check if the row is the header row 
    If (e.Row.RowType = DataControlRowType.Header) Then 
     'the header cell number where to put the link 
     Dim cellNumber As Integer = 1 

     'cast the cell in the header 
     Dim headerCell As TableCell = e.Row.Cells(cellNumber) 

     'create a hyperlink 
     Dim link As HyperLink = New HyperLink 
     link.Text = "myLink" 
     link.NavigateUrl = "/otherPage.aspx" 

     'remove existing controls from the cell 
     headerCell.Controls.Clear 

     'add the hyperlink to the cell 
     headerCell.Controls.Add(link) 
    End If   
End Sub 
相關問題