Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bind <a> href to Code Behind property

I want to set a public property in my code behind file, and then use that property to set the href property of several HtmlGenericControl <a> tags. Here is what I've tried:

Code behind:

public partial class className: System.Web.UI.MasterPage
{
        private string _linkValue = "";
        public string linkValue {
            get { return _linkValue; }
        }

        protected void Page_Load (object sender, EventArgs e)
        {
             SetLink();
        }

        private void SetLink()
        {
             _linkValue = "myUrl";
        }
}

.aspx file

<ul>
     <li><a runat="server" href="<%= linkValue %>">Link 1</a></li>
     <li><a runat="server" href="<%= linkValue %>">Link 2</a></li>
     <li><a runat="server" href="<%= linkValue %>">Link 3</a></li>
</ul>

Instead of the href being set to "myUrl", the href is %3C%25=%20linkValue%25%3E1

like image 435
dmr Avatar asked Oct 22 '22 03:10

dmr


1 Answers

If that is all you want to do to those <a> tags, you don't need to make them server-side controls. Get rid of the runat="server" and it should work.

like image 123
mikeagg Avatar answered Oct 24 '22 02:10

mikeagg