Dynamically assigning properties on a non data bound ASP.NET control

Posted by thinknow on Stack Overflow See other posts from Stack Overflow or by thinknow
Published on 2010-03-18T16:28:01Z Indexed on 2010/03/18 16:31 UTC
Read the original article Hit count: 339

Filed under:

For example, let's say I have a HyperLink:

<asp:HyperLink runat="server" Text="Foo" NavigateUrl="foo.aspx" />

How can I set the NavigateUrl on the server side, without having to go the code-behind?

This doesn't work of course:

<asp:HyperLink runat="server" Text="Foo" NavigateUrl="<%= urlString %>" />

(where urlString might be a string I created earlier in the page)

And this doesn't work because the HyperLink is not within a data bound control:

<asp:HyperLink runat="server" Text="Foo" NavigateUrl='<%# urlString %>' />

I guess I could just use a standard anchor element:

<a href="<%= urlString %>">Foo</a>

But I'd rather not mix up HTML and ASP.NET controls, and it would be handy to be able to do this for other controls.

Surely there must be a way?

© Stack Overflow or respective owner

Related posts about ASP.NET