2010-06-18 56 views
5

我有一个问题,我的Asp.net GridView中的超链接字段没有接受将会打开一个弹出对话框的Javascript函数。将HyperLinkField设置为Javascript Url

我使用下面的代码片断

<asp:GridView> 
    <asp:HyperLinkField 
     DataTextField="SomeColumn" HeaderText="Some Column Text" 
     SortExpression="SomeColumn" 
     HeaderStyle-HorizontalAlign="Left" 
     DataNavigateUrlFormatString="javascript:LaunchSomePopupdialog({0})" 
     DataNavigateUrlFields="Id" 
     ItemStyle-Font-Underline="true" /> 
</asp:GridView> 

然而,当我使用页面的URL,它的工作原理,如:

DataNavigateUrlFormatString="~/SomeOtherPage.aspx?Id={0}" 

有没有一种办法可以让这个工作,我的JavaScript功能?

回答

7

我认为你不必使用asp:hyperlinkfield就可以将它更改为模板字段内的普通标记。然后你可以这样做:

<asp:TemplateField HeaderText="Some Column Text" ItemStyle-Font-Underline="true"> 
    <ItemTemplate> 
<a href="#" onclick="javascript:LaunchYourStuff('<%#Eval("YourColumnID")%>')"><%#Eval("YourColumnDisplayText")%></a> 
    </ItemTemplate> 
</asp:TemplateField> 

所有的asp:hyperlinkfield属性都放在templateField标签上。

编辑

你不能把JavaScript中的HyperLinkField字段,因为这是by design

1

您也可以使用绑定字段,然后更改RowDataBound事件的文本。 (这将允许EnableSortingAndPagingCallbacks工作):

<asp:BoundField DataField="ID" HtmlEncode="False" /> 

确保HtmlEncode为false!

Protected Sub gv_RowDatabound(sender As Object, e As GridViewRowEventArgs) Handles gv.RowDataBound 
    If e.Row.RowType <> DataControlRowType.DataRow Then 
     Return 
    End If 
    Dim drv = TryCast(e.Row.DataItem, DataRowView) 
    If drv Is Nothing Then 
     Throw New Exception("drv is nothing") 
    End If 
    Const detailsCol As Integer = 4 
    e.Row.Cells(detailsCol).Text = String.Format("<a href='javascript:popUp(""Details.aspx?ID={0}"");'>Details</a>", drv("ID")) 
End Sub