enabling/disabling <a> tag through c# code

15,681

Solution 1

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;

Solution 2

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";
Share:
15,681
haysam
Author by

haysam

Updated on June 13, 2022

Comments

  • haysam
    haysam almost 2 years

    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