ASP.NET C# - Disable <A Href

I want to disable the <A Href HTML controls on a page similar to how I would disable the .NET controls on a page.

Below is sample code.

foreach (Control c in Page.Controls)
 {
  foreach (Control ctrl in c.Controls)
  {
    if (ctrl is Panel)
    {
foreach (Control plnCtrl in ctrl.Controls)
{
        if (plnCtrl is TextBox)
         ((TextBox) plnCtrl).ReadOnly = (ctrlStatus == true) ? false : true;

        else if (plnCtrl is Button)
         ((Button) plnCtrl).Enabled = ctrlStatus;

Open in new window

LVL 2
CipherISAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

guru_samiCommented:
You can use asp.net HyperLink and check for it like you are doing for TextBox and Button.
If you need html <a> element, you will have to give runat="server" for those a tag and then do something like this:
if (plnCtrl  is HtmlAnchor)
{
     HtmlAnchor a = plnCtrl  as HtmlAnchor;
     a.Disabled = true;
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CipherISAuthor Commented:
Tried above code and set a breakpoint in it but does not step into the code and does not disable the <a href on the page.
0
guru_samiCommented:
did you make all <a> to have runat="server"?
0
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

CipherISAuthor Commented:
Like this?

<a href='Maint.aspx?ID=<%#DataBinder.Eval(Container, "dataItem.ServiceID")%>' runat="server">
0
CipherISAuthor Commented:
did not work
0
guru_samiCommented:
Is it inside of Panel or outside of it? Or inside some gridview or other similar controls?? Make sure you are finding it inside of proper parent?
0
CipherISAuthor Commented:
Ah, its in a repeater.  How would i disable it there?  This is my sample code that I'm testing with.

protected void Page_PreRender(object sender, EventArgs e)
    {
        bool status = false;

        foreach (Control c in Form.Controls)
        {
            foreach (Control ctrl in c.Controls)
            {
                if (ctrl.ID != null && !ctrl.ID.Contains("txtPlace"))
                {
                    if (ctrl is TextBox)
                        ((TextBox)ctrl).Enabled = status;
                    else if (ctrl is Button)
                        ((Button)ctrl).Enabled = status;
                    else if (ctrl is RadioButton)
                        ((RadioButton)ctrl).Enabled = status;
                    else if (ctrl is ImageButton)
                        ((ImageButton)ctrl).Enabled = status;
                    else if (ctrl is CheckBox)
                        ((CheckBox)ctrl).Enabled = status;
                    else if (ctrl is DropDownList)
                        ((DropDownList)ctrl).Enabled = status;
                    else if (ctrl is HyperLink)
                        ((HyperLink)ctrl).Enabled = status;
                    else if (ctrl is LinkButton)
                        ((LinkButton)ctrl).Enabled = status;
                    else if (ctrl is CheckBox)
                        ((CheckBox)ctrl).Enabled = status;
                    else if (ctrl is HtmlControl)
                        ((HtmlControl)ctrl).Disabled = status;
                    else if (ctrl is HtmlAnchor)
                    {
                        HtmlAnchor a = ctrl as HtmlAnchor;
                        a.Disabled = true;
                    }
                    else if (ctrl is HtmlControl)
                    {
                        HtmlControl a = ctrl as HtmlControl;
                        a.Disabled = true;
                    }
                    else if (ctrl is HtmlLink)
                    {
                        HtmlControl a = ctrl as HtmlControl;
                        a.Disabled = true;
                    }
                    else if (ctrl is Panel)
                    {
                        foreach (Control pnlCtrl in ctrl.Controls)
                        {
                            if (pnlCtrl.ID != null && !pnlCtrl.ID.Contains("txtPlace"))
                            {
                                if (pnlCtrl is TextBox)
                                    ((TextBox)pnlCtrl).Enabled = status;
                                else if (ctrl is Button)
                                    ((Button)pnlCtrl).Enabled = status;
                                else if (pnlCtrl is RadioButton)
                                    ((RadioButton)pnlCtrl).Enabled = status;
                                else if (pnlCtrl is ImageButton)
                                    ((ImageButton)pnlCtrl).Enabled = status;
                                else if (pnlCtrl is CheckBox)
                                    ((CheckBox)pnlCtrl).Enabled = status;
                                else if (pnlCtrl is DropDownList)
                                    ((DropDownList)pnlCtrl).Enabled = status;
                                else if (pnlCtrl is HyperLink)
                                    ((HyperLink)pnlCtrl).Enabled = status;
                                else if (pnlCtrl is LinkButton)
                                    ((LinkButton)pnlCtrl).Enabled = status;
                                else if (pnlCtrl is CheckBox)
                                    ((CheckBox)pnlCtrl).Enabled = status;
                                else if (pnlCtrl is HtmlControl)
                                    ((HtmlControl)pnlCtrl).Disabled = status;
                                else if (pnlCtrl is HtmlAnchor)
                                {
                                    HtmlAnchor a = ctrl as HtmlAnchor;
                                    a.Disabled = true;
                                }
                                else if (pnlCtrl is HtmlControl)
                                {
                                    HtmlControl a = pnlCtrl as HtmlControl;
                                    a.Disabled = true;
                                }
                                else if (pnlCtrl is HtmlLink)
                                {
                                    HtmlControl a = pnlCtrl as HtmlControl;
                                    a.Disabled = true;
                                }
                            }
                        }
                    }
                }
            }
        }
    }

Open in new window

0
CipherISAuthor Commented:
I added the "runat=server" to the <a href and it is mucking with the words it is displaying as

' add staff

instead of

add staff

which was how it was displaying
0
guru_samiCommented:
I didn't get the issue in your last post but with links in repeater, you can handler Repeater ItemDataBound event. Give your <a> an id as well and then do FindControl.

HtmlAnchor a = (HtmlAnchor )e.Item.FindControl("link1");
0
CipherISAuthor Commented:
Ok, so repeater I have to use ItemBound.  Was hoping to avoid that but it is what it is.  I still can't get the <a href control to disable.
0
guru_samiCommented:
Is it hitting the a.Disabled statement? I did try on a page and it does work for me. Though disabled link will still look as link, just not clickable.
0
CipherISAuthor Commented:
no.  It is NOT hitting the a.disabled statement.  I click on the link and it redirects to that page still.
0
guru_samiCommented:
If it doesn't hit the a.Disabled statement, then the issue is locating the control itself.
Are we still talking of the control inside the repeater or outside?
You will have to share full .aspx and the code-behind you tried.
0
CipherISAuthor Commented:
Its outside of repeater.  It is a link that I use for add new.  Like add new employee.
0
guru_samiCommented:
As I said it has to be an issue finding it due to control hierarchy. Without looking at the entire code, it's hard to point out.
0
CipherISAuthor Commented:
thx
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.