2

I have a website. from each page of website I want to call a function which will receive a parameter of type Page. Each page will pass reference of itself to that function.

That function will hide and show some control on that page based on some logic.

Now I am not sure how to pass the page parameter. If I pass "this", I am unable to find any controls which I want to hide or show. This is my function

public static void Implement(string pageName, Page objPage)
    {
        if (pageName == "MANAGEMENT")
        {
            HyperLink obj = (HyperLink) objPage.FindControl("hlSave");
            if (obj != null)
            {
                obj.Visible = false;
            }
        }
    }

but objPage.FindControl("hlSave"); always returns null

Any idea whats wrong here?

2
  • @Knvn, no i'm not using Master Pages Commented Aug 29, 2011 at 17:36
  • even though you are not using master page, the FindControlRecursive method that I have provided has to find it(with poor performance). Did you checked it? Commented Aug 30, 2011 at 8:29

1 Answer 1

2

If you are using master page then that might causing FindControl to return null. In that case you can use:

HyperLink obj = (HyperLink)objPage.Master.FindControl("ContentPlaceHolderID").FindControl("hlSave");

or you can recursively find hlSave using below method:

    public static Control FindControlRecursive(Control Root, string Id)
    {
        if (Root.ID == Id)
            return Root;

        foreach (Control Ctl in Root.Controls)
        {
            Control FoundCtl = FindControlRecursive(Ctl, Id);
            if (FoundCtl != null)
                return FoundCtl;
        }

        return null;
    }

you can use it like:

HyperLink obj = (HyperLink)FindControlRecursive(objPage, "hlSave");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.