HyperLinkField's in GridView can't have javascript calls in the DataNavigateUrlFormatString property

Last post 06-30-2007 1:42 PM by Rivelyn. 6 replies.

Sort Posts:

  • Hmm [^o)] HyperLinkField's in GridView can't have javascript calls in the DataNavigateUrlFormatString property

    06-27-2007, 7:32 AM
    • Member
      11 point Member
    • JHoxley
    • Member since 06-27-2007, 11:18 AM
    • Posts 3

    Hello all,

    In a nutshell: If I put "javascript:ShowMapLocation({0},{1};" in the DataNavigateUrlFormatString it silently fails and generates *no* clickable link. If I remove the ":" from the string then it generates a clickable link but is, obviously, unusable. How can I get the ASP.NET's GridView control to generate a link pointing to a javascript function?

    I've successfully got a SQL Server 2005 query filling an ASP.NET GridView control working how I want. Part of the data returned is a longitude/latitude coordinate that we were compositing into a DataNavigateUrlFormatString field in a HyperLinkField to just point to a commercial mapping website. It was just a simple placeholder, but the code did the trick - the GPS coordinates were inserted into the URL which was usable in the final webpage.

    I now want to integrate Virtual Earth and actually embed the map directly into the page. I have a proof-of-concept that has regular HTML hyperlinks calling a javascript function that binds the map to a <div> tag. Works just fine.

    My problem is putting the two together. I simply cannot find anyway of getting the GridView control's HyperLinkField column type to generate a valid javascript link. I've tried catching events and writing code to force it in, I've tried escape sequences to trick it...

    From searching the internet it seems I'm not the only person to have this problem, but none of the pages I've seen offer a proper solution and some even suggest it's a bug in how ASP.NET parses/generates the strings.

    Would someone be so kind as to confirm whether this usage is actually possible (if it is a bug and/or impossible I'll stop trying!) and if so, how I might go about it?

    Best regards,
    Jack

  • Re: HyperLinkField's in GridView can't have javascript calls in the DataNavigateUrlFormatString property

    06-27-2007, 8:52 AM
    • Participant
      1,020 point Participant
    • Rivelyn
    • Member since 06-20-2006, 9:57 PM
    • Posts 532

    You and I are working on the same thing.

    http://forums.asp.net/p/1126413/1773081.aspx#1773081

    With a small difference, I am trying to pass an ItemID into a java code string from a gridview to display the item detials in a java popup window.

    If I get a solution for my problem I will most definately share since it will also correct you problem.

    www.someguy.ca rantings from some Canadian guy
    Follow me on twitter as well twitter.com/SomeCanadianGuy
  • Re: HyperLinkField's in GridView can't have javascript calls in the DataNavigateUrlFormatString property

    06-27-2007, 9:55 AM
    Answer
    • Member
      11 point Member
    • JHoxley
    • Member since 06-27-2007, 11:18 AM
    • Posts 3

    Thanks for that link - I thought I'd run a good enough search of this forum but that thread was just what I needed Smile

    I've now solved my problem:

    <asp:HyperLinkField DataNavigateUrlFields="Longitude,Latitude" DataNavigateUrlFormatString="javascript:ShowMapLocation({0},{1})"
         DataTextField="Tracking" DataTextFormatString="{0}" HeaderText="Tracking"
         ShowHeader="False" Target="_blank">
         <ItemStyle HorizontalAlign="Center" />
    </asp:HyperLinkField>

     Now becomes:

    <asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderText="Tracking" SortExpression="Tracking">
        <ItemTemplate>
            <asp:HyperLink ID="idTracking" runat="server"  NavigateUrl='<%# "ShowMapLocation(" + Eval("Longitude", "{0}") + "," + Eval("Latitude","{0}") + ")" %>' Text='<%# Eval("Tracking") %>'/>                                            
        </ItemTemplate>
    </asp:TemplateField> 

    With a supporting piece of CS:

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            HyperLink hl = (HyperLink)e.Row.FindControl("idTracking");
    
            hl.NavigateUrl = "javascript:" + hl.NavigateUrl;
        }
    
    }
    I'm sure there is a better way of solving this, but I'm only doing web development because I have to - it's not my normal area Wink
    So far as I've been able to test it appears work just fine!
    Best of luck solving your problem!
    Jack
  • Re: HyperLinkField's in GridView can't have javascript calls in the DataNavigateUrlFormatString property

    06-29-2007, 12:06 PM
    • Member
      6 point Member
    • ToddDev
    • Member since 06-17-2007, 4:39 PM
    • Posts 7

    Thanks everyone,

    I been frustrated for days trying to figure this Javascript/hyperlink/pop-up window. Thanks to you I am closer to my resolution.

    One question, how do I pass a string value as a second parameter to the Javascript function. Thanks

     

  • Re: HyperLinkField's in GridView can't have javascript calls in the DataNavigateUrlFormatString property

    06-29-2007, 12:22 PM
    • Member
      11 point Member
    • JHoxley
    • Member since 06-27-2007, 11:18 AM
    • Posts 3

    I use:  

    Eval("Engineer","\"{0}\"")

     To send the engineer's textual/string name to the JS function. The key is to add quotation marks around the value...

    hth
    Jack

  • Re: HyperLinkField's in GridView can't have javascript calls in the DataNavigateUrlFormatString property

    06-29-2007, 12:33 PM
    • Member
      6 point Member
    • ToddDev
    • Member since 06-17-2007, 4:39 PM
    • Posts 7

    Thanks, I cheated a little by using the replace() in the rowdatabound event. I will try your method though. Thanks

  • Re: HyperLinkField's in GridView can't have javascript calls in the DataNavigateUrlFormatString property

    06-30-2007, 1:42 PM
    • Participant
      1,020 point Participant
    • Rivelyn
    • Member since 06-20-2006, 9:57 PM
    • Posts 532

    The fix to my problem was solved somewhat based on this thread.

    Fist I replace my Gridview with a Repeater, I just created the same look as the gridview using a table then dropped the table into the Repeater template.

    It looks like so,

    <asp:Repeater ID="ItemData" runat="server">

    <ItemTemplate>

    <table border="0" cellpadding="0" cellspacing="0" style="width: 95%; position: relative;">

    <tr>

    <td align="left" style="border-bottom: dimgray 1px dotted" valign="middle">

    <asp:LinkButton ID="ItemLink" runat="server" OnClientClick='<%# Eval("ItemID") %>'

    Style="position: relative" Text='<%# Eval("ItemName") %>'></asp:LinkButton>

    </td>

    </tr>

    </table>

    </ItemTemplate>

    </asp:Repeater>

    And the code beside looks like this,

    Protected Sub ItemData_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles ItemData.ItemDataBound

    Dim hl As LinkButton = DirectCast(e.Item.FindControl("ItemLink"), LinkButton)

    hl.OnClientClick = "javascript:window.open('../ItemDetail/Default.aspx?ItemID=" & hl.OnClientClick & "', 'MyWindow', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,location=0,left = 390,top = 362,width=500,height=300');"

    End Sub

    When the link button is clicked it runs the window.open command and inserts the ItemID

    Thanks Everyone

    www.someguy.ca rantings from some Canadian guy
    Follow me on twitter as well twitter.com/SomeCanadianGuy
Page 1 of 1 (7 items)