【问题标题】:Setting HyperLinkField to a Javascript Url将 HyperLinkField 设置为 Javascript 网址
【发布时间】:2010-06-18 10:57:00
【问题描述】:

我有一个问题,我在 Asp.net GridView 中的超链接字段不接受将打开弹出对话框的 Javascript 函数。

我正在使用以下sn-p

 <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 函数工作?

【问题讨论】:

    标签: asp.net .net gridview hyperlink


    【解决方案1】:

    我认为您必须在不使用 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,因为这是by design

    【讨论】:

      【解决方案2】:

      您还可以使用绑定字段,然后在 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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-09-28
        • 2010-10-07
        • 2021-11-13
        • 1970-01-01
        • 2018-01-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多