problem formatting URL in script

N

neverstill

hi-

I have <a> tags in my DataList. For the href property, I want to do
something like this:
<a href='~/Default.aspx?show=Support&disp=Faq&p=<%#
DataBinder.Eval(Container.DataItem, "name")%>' runat=server>some link</a>

Looks pretty straight forward, but when I add the runat=server property it
stops parsing the <% %> block correctly.

The workaround has been to make a method that will take 2 strings,
concatentate them, then return the result, something like this:
public string BuildUri(string a, string b)
{
return a + b;
}

then in the <a> tag I do this:
<a href='<%#BuildUri("~/Default.aspx?show=Support&disp=Faq&p=",
DataBinder.Eval(Container.DataItem, "name"))%>' runat=server>some link</a>


That works. It's a pain though cuase a) I don't understand why I need to do
it in the first place and b) my needs have changed, sometimes I will be
combining 2,3 even 4 strings to form the URI, seems dumb to have 4
overloaded methods that take more and more strings ;)


So, what is causing this? This must have come up in the past. I tried
searching google, but I'm not sure what terms to use to describe the
problem.

If anyone has a solution (other than a function), I would love to hear it.
Thanks.

-Steve
 
S

Shan

Why do you need a runat tag for anchor tags? Since it is
inside the Datalist control,which has a runat tag I dont
think you need a runat tag for Anchor links.

Thanks,
-Shan
-----Original Message-----
hi-

I have <a> tags in my DataList. For the href property, I want to do
something like this:
<a href='~/Default.aspx?show=Support&disp=Faq&p=<%#
DataBinder.Eval(Container.DataItem, "name")%>'
 
N

neverstill

It's not an Anchor Tag, it's a reg. <a href> tag, I need the runat cause I'm
using the '~' to get to the VirtualRoot (dev server vs prod. server)
 
S

Shan

if you just use Default.aspx then it will take relative to
the current position. You can also store the path in the
web.config file and pull it from there..

BTW anchor tag == <a href>

-Shan
 
N

neverstill

you're right about anchor tag, I was confused with.... well, i dunno, but I
was ;)

problem with the relative thing is that I have nested controls(IE:
Navigation user control) that when nested using relative paths I get doulbe
up like:
Default.aspxDefault.aspx?show...

I remember I went through that a year ago... drove me nuts, then I
discovered the magic of the "~"....
 
M

mikeb

neverstill said:
hi-

I have <a> tags in my DataList. For the href property, I want to do
something like this:
<a href='~/Default.aspx?show=Support&disp=Faq&p=<%#
DataBinder.Eval(Container.DataItem, "name")%>' runat=server>some link</a>

Looks pretty straight forward, but when I add the runat=server property it
stops parsing the <% %> block correctly.

The workaround has been to make a method that will take 2 strings,
concatentate them, then return the result, something like this:
public string BuildUri(string a, string b)
{
return a + b;
}

then in the <a> tag I do this:
<a href='<%#BuildUri("~/Default.aspx?show=Support&disp=Faq&p=",
DataBinder.Eval(Container.DataItem, "name"))%>' runat=server>some link</a>


That works. It's a pain though cuase a) I don't understand why I need to do
it in the first place and b) my needs have changed, sometimes I will be
combining 2,3 even 4 strings to form the URI, seems dumb to have 4
overloaded methods that take more and more strings ;)

I can't help you with why there's a problem in the first place; however,
I can make a suggestion that might make your workaround a bit easier.

Instead of having your own BuildUri method that simply concatenates 2
strings with the prospect of having to create several overloads to deal
with varying numbers of string parameters, you can simply call the
String.Concat() static method - it already has a full set of overloads
to handle however many strings you want to serve up.
 
N

neverstill

Hi Mike,

well that will save me some time for sure, but can I call string.Concat() in
a script block? I STILL don't understand what you can and can't do in the
*.aspx <%%> script blocks. I'll try it and see what happens.

Either way, thanks for the suggestion!

-SK
 
M

mikeb

neverstill said:
Hi Mike,

well that will save me some time for sure, but can I call string.Concat() in
a script block? I STILL don't understand what you can and can't do in the
*.aspx <%%> script blocks. I'll try it and see what happens.

Either way, thanks for the suggestion!

You can use any .NET class from any assembly (yours, the Framework's or
a third party's) in an aspx script block provided:

1) the assembly is available at runtime (either by residing in the
bin folder or in the GAC), and

2) the assembly is referenced (either using an @Assembly directive
in the aspx file or an <add assembly ...> element in web.config)

Remember that the aspx file gets parsed into a C# or VB.NET file
(depending on the language specified in the @Page directive) which gets
compiled just like any other C# or VB.NET file. So if you call a method
in your script block that is not allowed because the assembly is not
referenced, you'll get a compilation error when you first access the page.
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top