views:

156

answers:

4

Hi All!

I'm trying to access a <li> tag in my first master page file. I tried FindControl(..) but it allways returns null.

Structure:

  • First Master Page (which contains <li id="element" runat="server">
  • Second Master Page
  • Default.aspx (need to access here)

What do I need to do to access the li element?

A: 

You might have forgotten runat=server

Code would help.

BioBuckyBall
it is already set..
Kaan
again, code would help.
BioBuckyBall
A: 

Is it possible you don't have your master pages inheriting correctly?

ddc0660
checked, it is correct :S
Kaan
+1  A: 

You would usually access a server-side control like so:

Page.Master.FindControl("controlID");

However, if your tag is not set to runat="server", you'll have to find it another way, such as getting the resulting Response.Content and changing it at some point.

EDIT: Since you're using nested master pages, you may need to go further back in the control hierarchy if you want to reach the "root" master and find a control in it.

Maybe: Control li = Page.Master.Master.FindControl("controlID")

SirDemon
here the code:Control myControl1 = Page.Master.FindControl("element"); if (myControl1 == null) Response.Write("errrr");and yes it prints "errr"
Kaan
Added another possible solution to take into account your nested Master pages situation.
SirDemon
A: 

You need to create a function which do find control recursively. SOmething like:

public control FIndControlEx(COntrolsCollection controls, string id)
{

   foreeach(Control ctrl in controls)
   {
      if (ctrl.id == id)
         return ctrl;
      var result = FindCOntrolEx(ctrl.Controls, id);
      if (result != null)
        return result;
   }
}
Sergey Osypchuk