Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable "a" href tag using code behind(c#) in asp.net 4.0

Tags:

html

c#

asp.net

For certain condition I want to disable my tag and I have tried following so far

//codebehind
if(condition)
{
    aTag.Disabled.Equals(true);
}

//.aspx page
<a id="aTag" runat="server"></a>

it's not working, I don't know whats the matter, If anybody know please give me the better solution, thanks.

like image 653
Learner Avatar asked Dec 09 '22 11:12

Learner


2 Answers

You can use javascript:void(0); to kill the link.

<a id="aTag" runat="server">You cannot click me!</a>

if(condition)
{
    aTag.HRef = "javascript:void(0);";
}
like image 121
Win Avatar answered Dec 11 '22 00:12

Win


You could try using the disabled html attribute .. not sure it works for links though.

aTag.Attributes.Add("disabled","disabled");

Update: disabled doesn't work for links

Maybe what you need is to remove the value in the href.

aTag.Attributes["href"]= "#";

Or remove it.

aTag.Attributes.Remove("href");
like image 31
scartag Avatar answered Dec 11 '22 02:12

scartag