wrong syntax when I try to set Javascript code in C# Code Behind.

M

mesut

Hi there,

how are you colleagues? I try to set a linkaddress in code behind for
a <asp:hyperlink> server control. but I think I have some syntax
problem. I don't know how to fix it.

What's wrong with the code below? I have put a hyperlink on the aspx
page. And I add the attributes of the hyperlink in my code-behind. I
set the attributes in code behind because I need the AddressGuid ID
value. I concattenate the value and make an URL variable. Then I add
the "onclick" event with Value "URL" in the hyperlink attribute. When
I run this code I get Systax error.

I assume it's the "less than" sign. Can someone advice what's wrong
with my code below?

The code Compiles well w/o any problem.

thanks

mesut

ASPX page code looks like:
--------------------------------------
<asp:HyperLink ID="lnkCreditLimit"
runat="server">Credit Limit</asp:HyperLink>


C# Code behind looks like:
--------------------------------------
string URL = "<script language=\'javascript\' type=\"text/javascript
\"> window.open('" + ResolveUrl
("~/Financial/CreditLimit.aspx?AddressGuid=" +
Request.QueryString["AddressGuid"].ToString())
+

"','name','height=255,width=250,toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,
resizable=no ,modal=yes');</script>";

lnkCreditLimit.Attributes.Add("onclick",URL);


Rendering looks like:
------------------------------
<a id="ctl00_ContentPlaceHolder1_lnkCreditLimit" onclick="&lt;script
language='javascript' type=&quot;text/javascript&quot;> window.open('/
Art/Financial/CreditLimit.aspx?
AddressGuid=f0e10cca-9116-4b21-81cc-974c930276a4','name','height=255,width=250,toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no ,modal=yes');&lt;/
script>">Credit Limit</a>
 
M

Marc Gravell

Actually this is more a question for an ASP.NET forum; but the minus/
dash is fine. The problem here is that you shouldn't include a
<script> block in an event; just the javascript itself. Try:

string URL = "window.open('" + ResolveUrl
("~/Financial/CreditLimit.aspx?AddressGuid=" +

Request.QueryString["AddressGuid"].ToString())
+
"','name','height=255,width=250,toolbar=no,directories=no,status=no,menubar­
=no,scrollbars=no,
resizable=no ,modal=yes');";

There are a few ways it can be further tidied, but try that first...

Marc
 
M

mesut

Thanks Marc. You're great!!! it works...

2 last things regarding this:
---------------------------------------
- Why is the link not underlined?. I mean when I click on it it opens
the window page, but I just see "credit limit" text w/o underline. I
mean I can't see whehter that is a hyperlink or not. (maybe I'm doing
not the right way).

- your speak: there are a few ways. Could you please let me know what
the "Best practice " is???


see the current rendered code:
------------------------------------------------------
<a id="ctl00_ContentPlaceHolder1_lnkBankAccount" href="../Financial/
BankAccount.aspx?AddressGuid=00eb7e21-cf16-467e-
b751-1febdfa29cba">Bank Account</a>&nbsp;&nbsp;
<a id="ctl00_ContentPlaceHolder1_lnkCreditLimit"
onclick="window.open('/Art/Financial/CreditLimit.aspx?
AddressGuid=00eb7e21-cf16-467e-
b751-1febdfa29cba','name','height=255,width=250,toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no ,modal=yes');">Credit
Limit</a>
 
M

Marc Gravell

No idea on the underline (I haven't done much ASP.NET); I'd guess at
css but don't ask me what...

Re tidying; I might have used string.Format with a {0} (etc) in place
of the query-string lookup, so that the overall value was clear at a
glance; alternatively I believe that ASP.NET allows something similar
on the markup page with <% etc; but not my area...

Marc
 
G

Göran Andersson

mesut said:
Thanks Marc. You're great!!! it works...

2 last things regarding this:
---------------------------------------
- Why is the link not underlined?. I mean when I click on it it opens
the window page, but I just see "credit limit" text w/o underline. I
mean I can't see whehter that is a hyperlink or not. (maybe I'm doing
not the right way).

That's because your anchor tag is not a link, it's an anchor. You need
an href attribute in the tag for the browser to render it as a link.

A common way to add an harmless href attribute to the tag is to use
href="#". That means a link to a nameless anchor at the current page,
which would move you to the top of the page if you followed the link.

Now, you don't want the user to actually follow the link when it's
clicked (as that will scroll the page to the top), so you should add
"return false;" at the end of your code in the onclick attribute. When
the onclick event code returns false, the link is not followed.

Another alternative is to add the same url in the href attribute as you
use in the window.open call, and add target="_blank" in the tag. That
way, if Javascript is disabled in the browser, the link will still open
the page in a new window.
- your speak: there are a few ways. Could you please let me know what
the "Best practice " is???


see the current rendered code:
------------------------------------------------------
<a id="ctl00_ContentPlaceHolder1_lnkBankAccount" href="../Financial/
BankAccount.aspx?AddressGuid=00eb7e21-cf16-467e-
b751-1febdfa29cba">Bank Account</a>&nbsp;&nbsp;
<a id="ctl00_ContentPlaceHolder1_lnkCreditLimit"
href="#"

onclick="window.open('/Art/Financial/CreditLimit.aspx?
AddressGuid=00eb7e21-cf16-467e-
b751-1febdfa29cba','name','height=255,width=250,toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no ,modal=yes');

return false;
 
M

mesut

thanks Göran. that fixed my problem with my link and
and also thanks for the other extra free tips :)
 

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

Top