Search code examples
c#asp.nethref

Get href value with onServerClick


I'm working with asp.net and c#

I got a list of link and i want to get the href value in the code behind.

So far I have:

<li><a href="#?id='101'" runat="server" onServerClick="room_click">Room 101</a></li>
<li><a href="#?id='102'" runat="server" onServerClick="room_click">Room 102</a></li>
<li><a href="#?id='103'" runat="server" onServerClick="room_click">Room 103</a></li>

protected void room_click(object sender, EventArgs e)
{
}

The problem is I can't find a solution to get the href value, I tried the anchor or regex but it doesn't work.


Solution

  • You have to cast the sender to an HtmlAnchor so you can access the property:

        public void room_click(object sender, EventArgs e)
        {
            var href = ((System.Web.UI.HtmlControls.HtmlAnchor)sender).HRef;
        }