views:

125

answers:

3

Hi,

Is it possible to call a class's static property to set the navigateurl property?

<asp:HyperLink ID="hlRegister" NavigateUrl="<%= SomeClass.Property %>"  runat="server" />

without using codebehind ofcourse!

A: 

sure, in the code behind:

hl.NavigateUrl = Class.Static().ToString();
craigmoliver
without using cod behind...
Anonymous Bug
why can't you use the code behind?
craigmoliver
+1  A: 

You don't need code behind. You can just try it, like i just did. I created a simple page with exactly the code you have, and then created a class called SomeClass with a property named Property. It worked fine for me the way that you have it set up above.

Edit: Ok, it didn't compile with an error.. but It's giving me not the result I'm looking for.

http://localhost:3061/Sample/%3C%=%20SomeClass.Property.ToString()%20%%3E

using:

public static class SomeClass
{
    public static string Property
    {
        get { return "http://www.google.com"; }
    }
}

and

<asp:HyperLink ID="hlRegister" NavigateUrl='<%= SomeClass.Property.ToString() %>' Text="Goooooogle" runat="server" />
stephenbayer
wow.. i know i've done something like this in the past, but i really can't get it to work.. something is wrong with my gray matter at the moment.
stephenbayer
A: 

You can do this, but to avoid a syntax error you must modify your example to be as follows.

    <asp:HyperLink ID="hlRegister" 
        NavigateUrl='<%= SomeClass.Property %>'  runat="server" />

Notice the small difference of using single quotes rather than double around the script.

However, one might really ask why not just do it in the codebehind.

Mitchel Sellers
I've been trying that and haven't been able to get it to work. Maybe my computer is messed up or something, but now it's driving me crazy.
stephenbayer
You might want to check to make sure that you have the full namespace referenced for the SomeClass. You by default do not have any namespaces imported, so at times it is best to just fully identify the class.
Mitchel Sellers