2010-06-18 57 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