Jonas Stawski

Everything .NET and More

GridView, HyperLinkField, and JavaScript what the #%$@!

When you always think something is going to go very smooth you know there will be something to make it rough. Of course I'm talking about code. As the title might have given it away, I'm talking about the GridView. To be more specific: a GridView with a HyperLinkField and JavaScript.
In 2005, a HyperlinkField cannot have javascript. For some reason if you do something like this:

<asp:HyperLinkField HeaderText="PLAN IMG" DataTextField="PLAN_IMG" Text="{0}" SortExpression="PLAN_IMG" DataNavigateUrlFields="PLAN_IMG" DataNavigateUrlFormatString="javascript:MM_openBrWindow('../images/elevations/{0}','img','');"/>

The grid will be rendered without a hyperlink. How to solve this problem, you may ask. After reading this blog I came up with a solution. Use a BoundField with a dataformatstring of an anchor.

<asp:BoundField DataFormatString="<a href=javascript:MM_openBrWindow('../images/elevations/{0}','img','');>{0}</a>" DataField="PLAN_IMG" HeaderText="PLAN_IMG" SortExpression="PLAN_IMG" />

Please note that I don't have any spaces in the href=. Hope this helps you guys.

Happy Programming!

Comments (20) -

I used this solution and it works, but I get a weird situation in that when the child page pops up (correctly), the parent page blanks out with only text being "[object]". If I refresh the parent page, the text changes to "null".

any help would be greatly appreciated.

Reply

Did you add the javascript in front of the href? Please add the code here so that everyone can see it.

Reply

You are now promoted to my personal hero! Smile
It works perfect. I used it in code behind with VB.

Reply

Hmmm, just 1 problem, it works fine with firefox but it does nothing at all in IE. :s

Reply

I have facing the same problem here.I am also getting the problem of(when the child page pops up (correctly), the parent page blanks out with only text being "[object]". ).
I have used javascript in href also.
I have one more problem
I want to pass a value as we pass in the hyperlink field using datanavigationfields.
Please help me.
Thanku

Reply

Thanks to all.I have found the solutions of hyperlink problem in gridview.
I tried many solutions but nothing was working.One solution is change the hyperlink column into the template column.If you dont know the syntex of template column then just go into the design view.check the smart tag of the gridview.Where you can find a link "change this column into template column" just click that and it seems that hyperlink field is working.
Thankyou

Reply

Shirish, please post your code here so everyone else can see it.

Reply

Wim, that might be a problem with your JavaScript. Usually JavaScript works very differently between browsers. I suggest you set the JavaScript into a function and test it without the GridView. Make sure it works well on all your supported browsers and once it works move forward to use it on the GridView

Reply

To solve this problem, you could also derived from the HyperLinkField class and override the FormatDataNavigateUrlValue method. Once you only need to affect the NavigateUrl property with the result of the base class.... Enjoy

internal sealed class HyperLinkField : System.Web.UI.WebControls.HyperLinkField
{
protected override string FormatDataNavigateUrlValue(object[] dataUrlValues)
{
return NavigateUrl = base.FormatDataNavigateUrlValue(dataUrlValues);
}
}

Reply

Dear Joans,
I am sorry,I didnt visit your blog.so didnt read th reply.Actually there is no code require for that.Just convert the hyperlink field in the templatefield(from the design view).
The hyperlinkfield will be the same as we do in datagrid.
if you require what i did exactly then plz inform me .i will post it here.
thanks

Reply

My issue is in regards to making a whole row in a gridview linkable... not just the data itself. Also, if possible, having the cursor change when scrolling over each row.

Reply

Nice ;p
However, any tricks to make it works when you have spaces in your {0} expression ?

Reply

[OBJECT] window.
wrap your javascript call in a void()
I was running into the object problem with the hyperlink in the DataGrid, so now instead of
window.open() I'm using void(window.open())

for the case that window.open() works in FireFox but now IE, be sure that your window name doesn't have any spaces.

window.open('http://mysite.com','myWindowName');
not
window.open('http://mysite.com','my Window Name')

Reply

Hello guys,
I have tried doing both of these javascript:void but I am not able to see the output as I want... soemtimes a smal window pops up but the tool bar are there even though I have disabled them.
Can anybody post the perfect code for this.
Thanks...i would really be greatful!
Regards,
T

Reply

Please look at this post for more control over the columns
www.jstawski.com/.../...-Over-GridView-Column.aspx

Reply

Thank you so much

Reply

shirish can u please please post ur exact code.....i am facing problems with the parent window going to null sort.....do please tell wht have u done exactly...many many thankx...

Reply

HEY IT IS WORKING FINE .. GOOD SOLUTION. BUT I M GETTING MORE GREEDY. CAN I PASS MORE THAN ONE DYNAMIC PARAMTER ?In  DataFormatString as querystring ?
PLEASE HELP

Reply

If you're getting more greedy then check this out:
www.jstawski.com/.../...-Over-GridView-Column.aspx

Reply

It doesn't come out as a hyperlink for me....

<asp:BoundField DataField="Popp" DataFormatString="<a href=javascript:openPopup('Popp.aspx?WOID={0}','CC');>{0}" HeaderText="Popp" SortExpression="WOID" />

My open Popup function requires 2 parameters
Oh I so hope I can make this work!
Thanks.

Reply

Pingbacks and trackbacks (2)+

Add comment

biuquote
Loading