views:

168

answers:

3

I have a number of pages

<%@ Page Language="C#" AutoEventWireup="True" CodeBehind="MyPage.aspx.cs" Inherits="MyPage " MasterPageFile="~/Site.master"  %>
<asp:Content ContentPlaceHolderID="commonForm" runat="server">
 <asp:Table runat="server">
  <asp:TableRow>
   <asp:TableCell ID="cellControl" />
  </asp:TableRow>
 </asp:Table>
</asp:Content>

public partial class MyPage : MySuperPage { }

and a super class for them:

public abstract class MySuperPage : Page
{
    public MySuperPage()
    {
        this.Load += new EventHandler(PageLoad);
    }

    // my own method
    protected void PageLoad(object sender, EventArgs e)
    {
        var c = this.FindControl("cellControl"); // null!
    }

    // auto event handling
    protected void Page_Load(object sender, EventArgs e)
    {
        var c = this.FindControl("cellControl"); // null!
    }
}

Why neither method can't find a control?

A: 

Seems to me you are trying to find control in the Page control collection what's wrong. You have to search table cell in Table control.

Upd. If you are using master page you can access its controls directly from page. First you have to declare master type:

<%@ MasterType VirtualPath="~/MasterPage.master" %>

Then declare public property (which can be some control too):

public string MyTitle
{
    get { return "BaseMaster Title"; }
}

Then you will able to write:

string text = Master.MyTitle;

or

Master.FindControl('Table1');
sashaeve
@sash: as far as FindControl() searches recursively it has to find my control too. didn't it?
abatishchev
No, you can not serach controls inside containers (GridView, ListView, Table and so on).
sashaeve
@sash: Why so this.FindControl("Table1") also doesn't work if masterpage is used. Without it all works fine. Is masterpage a root of this evil?
abatishchev
I've updated the answer regarding master page.
sashaeve
+1  A: 

This is a duplicate of http://stackoverflow.com/questions/839794/finding-a-control-in-a-page-from-a-page-base-class

Paul Manzotti
@Paul Manzotti: Yea, question is the same, but solution given is not acceptable for me. I know what I do :) Every page must have user rights verification code on load and this code is in the super class.
abatishchev
Is there any reason that you can't turn it into a User Control?
Paul Manzotti
@Paul Manzotti: ok, thanks! it seems that's a real solution. because i didn't find any other ways
abatishchev
+1  A: 

The most common solution I've seen is to do a recursive descent down the control tree until you find the control with the desired ID, e.g. http://www.codinghorror.com/blog/archives/000307.html

flatline
@flatline: Looks strange but the link doesn't work - shows blank screen. Does it work for you now?
abatishchev
@abatischev - weird, same thing here, but I tested the link the before posting, and the google cache still has a record. Methinks it's a problem with codinghorror.com? At any rate: http://www.google.com/search?q=findcontrolrecursive has a number of relevant links
flatline