Anonymous Bug
Anonymous Bug

Reputation:

asp.net hyperlnk control

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!

Upvotes: 1

Views: 251

Answers (3)

Mitchel Sellers
Mitchel Sellers

Reputation: 63126

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.

Upvotes: 1

stephenbayer
stephenbayer

Reputation: 12431

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" />

Upvotes: 1

craigmoliver
craigmoliver

Reputation: 6562

sure, in the code behind:

hl.NavigateUrl = Class.Static().ToString();

Upvotes: 0

Related Questions