Link to home
Start Free TrialLog in
Avatar of CipherIS
CipherISFlag for United States of America

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of guru_sami
guru_sami
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CipherIS

ASKER

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.
did you make all <a> to have runat="server"?
Like this?

<a href='Maint.aspx?ID=<%#DataBinder.Eval(Container, "dataItem.ServiceID")%>' runat="server">
did not work
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?
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

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
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");
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.
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.
no.  It is NOT hitting the a.disabled statement.  I click on the link and it redirects to that page still.
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.
Its outside of repeater.  It is a link that I use for add new.  Like add new employee.
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.
thx