Problem with asp:hyperlink

  • Thread starter Thread starter ashtek
  • Start date Start date
A

ashtek

I have an <ASP:HYPERLINK> on a datagrid (templatecolumn) and I need to
open a new window with no toolbar and specific height & width onclick
of the link. I also want to pass the PK to this new window.

<ASP:HYPERLINK ID="lnkNewWin" RUNAT="server" NAVIGATEURL="#"
ONCLICK="javascript:window.open('NewWin.aspx?PK=<%#
DataBinder.Eval(Container.DataItem,"PK")%>','','toolbar=0,width=600,height=600,scollbars=1')"
TEXT="Click"></ASP:HYPERLINK>

The above code throws an error "Server tag is not well formed".

I am using ONCLICK because I want to specify toolbar=no and height &
width for the new window.

If I replace this <%# DataBinder.Eval(Container.DataItem,"PK")%>
with a numeric value (say 10) then it works fine.

Anyone knows what is wrong with the syntax?

Thanks,
Ashish
 
Hi Ashish,

The property binding syntax must encapsulate the entire value of the
property and must be surrounded by apostrophes, not quotes, IIRC. Try this
instead:

onclick='<%#
string.Format("javascript:window.open('NewWin.aspx?PK={0},'','toolbar=0," +
"width=600,height=600,scollbars=1')",
DataBinder.Eval(Container.DataItem, "PK")) %>'
 
Thank you for your help dave.
I tried the code you have given but I am still seeing the same error -
"server tag not well formed".

-Ashish.
 
Thank you for your help Dave.
I tried the code you have given but I am still seeing the same error -
"server tag not well formed". By the way what is IIRC?
Thanks,
-Ashish
 
Hi Ashish,

The reason it doesn't work is because the string being bound contains
apostrophes, so ASP.NET thinks that your string is ending earlier than it
should when it's being parsed. Complex javascript like that should be
extracted into a script:

<script type="text/javascript">
function showNewWindow(pk)
{
window.open("NewWin.aspx?PK=" + pk, "",
"toolbar=0,width=600,height=600,scollbars=1");
}
</script>

And in the template use the following code:

<asp:HyperLink ID="lnkNewWin" runat="server" NavigateUrl="#"
Text="Click" onclick="javascript:showNewWindow(this.pk)"
pk='<%# DataBinder.Eval(Container.DataItem, "PK") %>'>
</asp:HyperLink>

I'm taking advantage of DHTML expando by adding a custom attribute: pk.

Be sure to place the entire data-binding expression on one line.

If you're using the 2.0 framework you can use <%# Eval("PK") %> instead of
the entire data-binding expression.

BTW, the acronym IIRC stands for "If I remember correctly"

(The acronym BTW stands for "by the way" :)
 
Hi Ashish,

Apparently the javascript I gave you doesn't work in FireFox (I tested it on
a hunch after I submitted my reply - sorry ;).

Try the following, which works for me in FireFox and IE7:

onclick="javascript:showNewWindow(this.getAttribute('pk'));"

If you test other browsers, please let me know if this doesn't work for you.
 
Yes, this works fine!!!
Thanks a lot Dave. It was a great idea to have an attribute PK & pass
its value to a function.
Thank you again,
Ashish.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top