views:

78

answers:

4

I've created new Web Site project. Some helper classes are in App_Code folder. Pages are in the root. User controls are in the folder UserControls in the root. All page class are defined in MyProjectName.Pages namespace. All user control class are defined in MyProjectName.UserControls namespace.

I can get access from my pages to controls, but I cann't get access from my controls to page classes! I use Page property and try to cast it to MyPage class (include directive using MyProjectName.Pages at the begining). Here compiler says, the is no type or namespace Pages in MyProjectName namespace. Also, I don't see these namespaces (MyProjectName.Pages and MyProjectName.UserControls) in Object Viewer. Should I replace somewhere my controls and pages to get ability to use page classes from controls?

+1  A: 

Your best bet is to switch to Web Project from the Web Site.

WebSite "project" is a Microsoft's foster child introduced in ASP.NET 2.0 with the intention for being able to compile the pages dynamically AFTER the site is deployed. Therefore the compiler must be prepared to the situation when the pages are modified (including the introduction and/or removal of the namespaces) after it was built. Microsoft solution to this problem was to make the classes in the app_Code somewhat separate from the pages - they are compiled in separate passes.

Initially this was the only model for web apps in ASP.NET 2.0 but it introduced so many problems (yours is just one of them) that MS re-introduced the model from ASP.NET 1.1 now known as web projects. This model does not have any of these limitations, but deploying apsx dynamically is more difficult.

The bottom line is that unless you really really need to be able to replace aspx on the fly you are better off with web project. And if you do you have to have a closer look at the design of your web site

mfeingold
Thank you for the detailed explanation! Could you tell me, what should I do to switch to Web Project? And should I change some IIS settings or settings in web.config?
Sevina
this is a link I found on MSDN: http://msdn.microsoft.com/en-us/library/aa983476%28VS.80%29.aspx
mfeingold
A: 

The reason for your problem has to do with the way that web site projects are built. Since they are designed to built on the fly by IIS, they don't package up every file in the project into a single DLL; the supported linkages are limited. Common code should be in App_Code, not in your page classes.

Also, why are you trying to access your page classes from your controls? That's unusual, and shouldn't normally be required.

RickNZ
I've migated! Thank you very much!.. But shouldn't control use some properties, with are common for several controls and pages - such as UserID - which retrieve from Cookie userID - this define on the pge and uses in some controls, so they should to cast page class
Sevina
Still it is not a god idea to define such properties on the page itself. This approach is fragile and easily breaks. There are better ways - i.e. you can derive your own page class from the Web.Page and define such properties there. Your pages can derive from the your page class.
mfeingold
@Sevina: you can pass data from your page to your controls using properties on the controls. That's much better than trying to have the control reach back into the page somehow. Another possibility is to set parameters in the per-request cache at HttpContext.Items[], or to use functions with project-level scope, rather than putting those functions in your pages.
RickNZ
A: 

Thak you for advice! I've define property UserID on the page:

private int userID = 0;
        public int UserID
        {
            get
            {
                if (userID == 0 && Request.Cookies["UserID"] != null)
                    int.TryParse(Request.Cookies["UserID"].Value, out userID);

                return userID;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                DataBind();
        }

And set it to the appropriate control property:

<uc:MyControl ID="uc1" runat="server" UserID='<%# UserID %>' />

But at breakpoint in debug mode

public partial class MyControl : UserControl
    {
        private int userID = 0;
        public int UserID
        {
            get { return userID; }
            set { userID = value; }
        }
public override void DataBind()
        {
            // Why UserID = 0 ?
        }

Am i wrong?

Sevina
A: 

I can get access from my pages to controls, but I cann't get access from my controls to page classes

So are you trying to access pages control from some class. Check out link: http://niitdeveloper.blogspot.com/2010/10/access-page-control-from-class-file-in.html

Vikram Singh Saini