2010-06-09 81 views
0

使用ASP.NET和DataGrid,如何將HyperLinkColumn綁定到多個字段?ASP.NET:將HyperLinkColumn綁定到多個字段

Dim detail As New HyperLinkColumn 
    With detail 
     .Text = "View Details" 
     .HeaderText = "" 
     .NavigateUrl = "\TeamDetail.aspx?Account={0}&Broker={1}" 
     .DataNavigateUrlField = "AccountKey, BrokerNumberKey" 
    End With 

我希望在HyperLinkColumn上有一個數據綁定事件,但沒有這樣的運氣。

回答

0

您需要使用TemplateColumn。這將爲您提供可在ItemDataBound事件中使用的命名控件。

ASP

<asp:TemplateColumn> 
    <ItemTemplate> 
     <asp:HyperLink runat="server" ID="LinkColumn" NavigateUrl="" Text="View Details"></asp:HyperLink> 
    </ItemTemplate> 
</asp:TemplateColumn> 

VB

Private Sub ReportGrid_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles ReportGrid.ItemDataBound 
    Const target = "/TeamsDetail.aspx?Account={0}&Broker={1)  Select Case e.Item.ItemType 
     Case ListItemType.Item, ListItemType.AlternatingItem 
      Dim ctrl = CType(e.Item.FindControl("LinkColumn"), HyperLink) 
      Dim row = CType(e.Item.DataItem, DataRowView) 
      If ctrl IsNot Nothing Then 

       Dim accountKey = CInt(row("PrincipalAccountKey")) 
       Dim brokerNumberKey = CInt(row("BrokerNumberKey")) 
       ctrl.NavigateUrl = String.Format(target, accountKey, brokerNumberKey) 
      End If 
    End Select 
End Sub 
相關問題