hello
I hope someone can help me with this, because I can't figure this out on my own no matter how much I try to:
A)
I think I should clarify how the compilation model works. First of all, you can write everything (including C# code) in a single .ascx file and inherit from whatever class you want to (that ultimately inherits from UserControl) by specifying Inherits attribute (or you might want to omit it) and without a ClassName attribute. If you do this, you can still access the properties from the .aspx file that uses this control. The thing is, you can't use the class in the codebehind file of .aspx page (if it has one) directly.
But why then was I still able to access UserControl’s properties from the aspx.cs file even without specifying ClassName attribute:
ascx file:
<%@ Control Language="C#" AutoEventWireup="true"
CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl"%>
ascx.cs file:
public partial class WebUserControl : System.Web.UI.UserControl
{
public string Some_Property
{
get { return "this is UserControl "; }
}
aspx.cs file:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = WebUserControl1.Some_Property; //works fine
}
}
B)
By specifying the ClassName, if you are using the Website project model of Visual Studio, you can access it from the .aspx.cs file too. Note that this would not work in Web application project model as every .cs file will be compiled ahead of the time (prior to the .ascx file). In that case, even ClassName attribute wouldn't help much.
But doesn’t Web application project compile all code-behind files into a single dll? Thus if ascx uses code behind, won’t this ascx.cs also be compiled into same assembly as aspx.cs file? Thus shouldn’t we in that case be able to access the class from aspx.cs also ( provided we specified ClassName attribute )?
But I was able to access UserControl’s properties from aspx.cs, even if I worked in Web application project ( and even though I didn’t specify ClassName attribute )?!
C) Besides, when I did specify ClassName=”Some_Name” attribute inside control directive, I wasn’t able to refer to Some_Name inside aspx.cs:
<%@ Control Language="C#" AutoEventWireup="true"
CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" ClassName=”Some_Name”%>
Aspx.cs file:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Some_Name some_name; // compile time error
}
}
Could you tell me what I'm doing wrong?
thanx