使用ASP.NET和DataGrid,如何将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 :(得分: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