2

I am trying to enable/disable a "a" tag that's in my html code through my C# back code. I know if it was a button then I would just use, xxx.Visible="false/true" but i guess "a" tag does not have any features for backend code. Here is what I have,

c#:

if (Session["SessionUsersLoginId"] == null)
    { 

        alreadyMemberLabel.Visible = true;
        SignInButton.Visible = true;
        forgotdButton.Visible = true;
        passwordLabel.Visible = true;
        passwordTextBox.Visible = true;
        Right here should disable visibility for "login_pop" a tags
    }

Html:

 <span style="width:32%; float:right;">
                <div class="panel">
            <a href="#login_form" id="login_pop">Log In</a>
            <a href= "AddUsers.aspx" id="login_pop">Sign Up</a>
         <span style="width:32%; float:right;">             
            <asp:Button ID="forgotdButton" CssClass="btn-link forgotPass" runat="server" Height="25px" Text="Forgot Password?" 
                                onclick="forgotdButton_Click" Width="135px" /> 

             </span>

        </div>

Maybe there is a way to enable/disable through id? Thanks you

haysam
  • 401
  • 5
  • 11
  • 16

4 Answers4

3

Firstly, you have two same IDs on different elements (login_pop) they should be unique.

Second, you can make the <a> tag a server component and that way you can access it from your code-behind:

HTML

<a href="#login_form" id="login_pop" runat="server">Log In</a>

C#

login_pop.Disabled = true;
CodingIntrigue
  • 75,930
  • 30
  • 170
  • 176
  • 1
    Enabled is not a property of HTML controls. Disabled, yes but not enabled. At least in .NET 3.5 it is like this. – Sayan Nov 28 '13 at 11:30
  • Good point. http://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmlcontrol.disabled(v=vs.110).aspx – CodingIntrigue Nov 28 '13 at 11:38
  • Sadly this won't work. There is no way to [disable a link](http://stackoverflow.com/questions/2091168/disable-a-link-using-css). But there is a hack to do that in the aforementioned link. – Sayan Nov 28 '13 at 11:41
  • This will not work in FF and chrome, but will work in IE for sure! – Pranav Bilurkar Aug 17 '16 at 03:41
1

First off, you will not be able to do anything, in code-behind, to the anchor tag if you do not add runat="server" to the anchor element, like this:

<a href="#login_form" id="login_pop" runat="server">Log In</a>

Once you do that then you will be able to modify the attributes of the anchor tag like this:

In code-behind, this will disable the link:

login_pop.Atributes["disabled"] = "disabled";
Karl Anderson
  • 34,606
  • 12
  • 65
  • 80
  • This won't disable the link. Well, it will, but I can remove the disabled attribute using developer tools and, lo and behold, the button works. Very dangerous. I've actually just discovered this and am attempting to find a solution. I'll post a productive reply once I figure it out. – brad May 06 '14 at 17:21
0

Add both an anchor and a label control. Set the visibility property of the label to none. In order to disable the anchor, use css to hide the anchor and set the visibility property of the label.

Dave
  • 649
  • 5
  • 13
0

on your code behind, doing something like this works too. aTag.HRef = "";