locked
How to Enable/Disable Href link at code behind RRS feed

  • Question

  • User1878568433 posted

    How to Enable/Disable Href link at code behind

    Hi...
    I want to Enable/Disable Href link according some conditions but how i don't know...???

    Only Enable/Disable not Visible/Hide...

    I have used following code but it's not working..

    <a href="User.aspx" id="href_user" runat="server">User</a>code behind
    if(session["Admin"]==null)
    {
    href_user.Disabled=false;
    }
    else
    {
    href_user.Disabled=true;
    }
    Friday, May 3, 2019 8:56 AM

All replies

  • User1724605321 posted

    Hi ,

    You could use CSS to achieve that , define css class in your page :

    <style>
            a.disabled {
                pointer-events: none;
               cursor: default;
           }
    </style>

    Then in code behind :

     href_user.Attributes.Add("class", "disabled");

    Also check the CSS pointer-events supports :

    https://caniuse.com/#feat=pointer-events 

    Best Regards,

    Nan Yu

    Friday, May 3, 2019 9:17 AM
  • User1878568433 posted

    I am getting error after adding runat="server"

    What to do

    Friday, May 3, 2019 10:39 AM
  • User1724605321 posted

    Hi sripadasatpathy ,

    Which error do you face ? Please show related code sample .

    Best Regards,

    Nan Yu

    Monday, May 6, 2019 1:59 AM
  • User-418973555 posted

    once you put a runat="server", maybe you wanna use aspnet web control eg. <asp:HyperLink ... etc>

    then try enable / disable them during code-behind

    aspx
    <asp:HyperLink ID="hllinkpage" Text="click to next page" NavigateUrl="User.aspx" CssClass="ifany" runat="server" />
    
    code behind
    if (Session["Admin"] == null)
        {
          hllinkpage.Enabled = false;
        }
        else
        {
          hllinkpage.Enabled = true;
        }
    

    Friday, May 10, 2019 5:51 AM
  • User-1038772411 posted

    Hi, Sripadasatpathy

    Please Refer below link hope you will get as you want.

    https://stackoverflow.com/questions/36193850/a-href-enable-disable-from-code-behind-asp-net-c-sharp

    Thanks.

    Friday, May 10, 2019 12:18 PM
  • User-2054057000 posted

    You can use asp:HyperLink control:

    <asp:HyperLink ID="HyperLink1"
                 runat="server"
                 Text="Go To CheckBox Status Example"
                 Font-Size="Large"
                 Font-Bold="true"
                 NavigateUrl="~/CheckBox/CheckBoxStatus.aspx">
    </asp:HyperLink>

    And then disable or enable it from code behind:

    HyperLink1.Enabled = false; //for disabling
    HyperLink1.Enabled = true; //for enabling

    Saturday, May 11, 2019 1:11 PM