Calling inline (ascx) methods from codebehind file using partial classes - c#

I'm using .net 2.0. I'd like to place one of my C# methods in an inline server script block inside my ascx file because I need to update it a lot while designing. This way I don't have to compile my large project everytime.
I would like to call the method from inside my code behind file. I use to do this by inserting a virtual stub method in the codebehind file and overriding it in the ascx file. Now, since I've started using partial classes, I was expecting to be able to just call the method directly. Is this not possible?
Thanks

Unfortunately, I don't think this is possible. At design time, the partial class (*.designer.cs) mainly contains just your controls. The class that actually contains code from the script block is also generated by ASP.NET but it inherits from your main class.
See http://msdn.microsoft.com/en-us/library/ms178138.aspx for more info.

I don't understand your motive... Isn't it just as easy to modify a page's codebehind as it is the .ascx file? Either way ASP.NET is going to either dynamically compile them when the file gets accessed or force your to rebuild, depending on the type of project.

Related

The relationship between code behind class and aspx generated class

I was reading a book says that "a Web Form’s code-behind class was the base class for the class generated by the runtime from the .aspx file itself, as the picture below shows:
I don't know how the generated class looks like, as I only deal with aspx.cs which is the code-behind class.
So let say I have a .aspx file that have some controls like textboxes, do the generated class generated by .aspx file contains Textbox class? and how this generated class inherit the code-behind class, I'm really confused, can anybody post an example code of generated class generated by .aspx file?
The generated classes are stored here:
%windir%\Microsoft.NET\Framework\version\Temporary ASP.NET Files\root
Replace the version in the above path with the version of your framework. There will be many files in there but you can delete them all to start afresh. Then restart the web application in question and you will see a brand new folder (with a numeric name) will be created in the root folder. Inside the folder you will see many files but if you sort them by type, one of the CS Files will be the one generated for the .aspx page. The name will not be a friendly name so you will have to keep opening them and search for nameofpage_aspx. For example, if the aspx page is named About, search for about_aspx. If you do not find it, search for it in another file and one of them will be for about_aspx.
You may also look for the first line in the file which starts with #pragma checksum and it will have the name of the .aspx page. Not all files will have this line.
In those CS File types are the generated code which will be used by asp.net and you can examine its contents in more details if you want. Please do not ask me to explain the details of what is in those files but I am sure if you search online, you will find info on that.

How do you return an embedded resource .aspx page via a httphandler?

Pop quiz hot shots...
I have a Visual Studio 2010 .NET 4 solution with 2 projects, The first project is a c# class library that contains a httphandler and a .aspx page. The .aspx page's build action has been set to "Embedded Resource".
The second project is an asp.net web application which references the first. The httphandler is wired up in the web.config.
I want the httphandler to serve the embedded .aspx page. How do I do this?
Thanks,
James
maybe this is relevant: http://www.west-wind.com/weblog/posts/2007/Jul/23/Loading-an-ASPNET-Page-Class-dynamically-in-an-HttpHandler
.aspx is just a specialized kind of HttpHandler in .NET. Don't forget that.
Thus, .aspx files (ASP.NET Web Pages) actually have implemented IHttpHandler and they have ProccessRequest method. There are two ways to do this:
Based on dynamic compilation nature of Web Forms and markup vs. code-behind, if you want the markup of the page to be compiled dynamically and be executed, you have to extract the page (through code) and save it on the disk. This extraction process can take place on Applciation_Start event.
If you don't like the extraction method, don't forget that you can remove markup entirely and do everything in code-behind (just like PHP or old ASP or ASP.NET MVC). Also remember that your page is actually a class from the point of OOP. Thus simply instantiate it in your HttpHandler and call it's ProcessRequest method, passing the current HttpContext into it.
The way I would do it is through a VirtualPathProvider, not a handler. You can set up and register a virtual path provider to serve pages from an embedded resource (or database, web service, or anything else you can think of).
http://support.microsoft.com/kb/910441

GUI Designer and Code view

I do not understand why code view displays the code of Form instead of Form.designer file, which is the actual file containing the GUI code. How could they say then that you can see GUI in designer or code view?
The point is that the code behind for the GUI itself should not be altered manually, because any changes inside the designer can then overwrite or remove all your manual changes.
The code file you actually get to see is the same class as the other code behind file, but the class is split into two by the use of the partial keyword. This keyword was actually made for this purpose, so that auto generated code, and manually written code can be put into two different files.
'Code view' means: the code of your class (or module), not necessarily the GUI code for that class.

Add Control to website via Code

I am writing some controls for an asp.net website and i want make this controls contain .ascx file and .dll file.
I dont want upload site for every control.
so is there a way that make this control add to site via code like adding module in DotNetNuke.
You may need to consider using WebParts:
http://dotnetslackers.com/articles/aspnet/UsingWebPartsInASPNet20.aspx
Or this link:
http://quickstarts.asp.net/QuickStartv20/aspnet/doc/webparts/intro.aspx
You need to create custom controls. You can do that by deriving your control class from "System.WebUI.Webcontols.Control". You need to override CreateChildControls method for the UI.

Converting a Page to a User Control

If we want to convert a Page to user control then we should also do the following:
“If you aren’t using the code-behind model, make sure you still include a class name in the Control directive by supplying the ClassName attribute. This way, the web page that consumes the control can be strongly typed, which allows it to access properties and methods you’ve added to the control..”
I’m not sure I understand the above quote:
Namely, if we don’t use code behind class, then ascx class will derive directly from UserControl class!
Thus if we don’t use code behind class, then there won’t be any methods or properties added to a control, so why would web page have problems accessing any of user control’s properties and methods? So why would we need to include a class name in Control directive?
thanx
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. 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.
In any case, strictly none of the attributes are necessary. You can always use the properties defined anywhere in .ascx or .ascx.cs file included in a page in the .aspx file (but not always .aspx.cs file).
Edit to address the update to the question:
A) From your source code, it seems you are using the Website model here. Note that I mentioned you cannot use the class directly. I agree this statement might have been a little misleading. What I wanted to note is that without a ClassName, ASP.NET will choose a name for your user control class but that name is not guaranteed. While you can use the generated name, it's not recommended at all. You should treat it pretty much like an anonymous type where you can use an instance but cannot mention a name. In your example, you are basically referencing an instance (which is constructed on the .aspx markup), not the class, which is OK.
B) What you are saying is correct. Whatever you declare in ascx.cs in a Web application will be visible to .aspx.cs and .aspx. What I was talking about is properties that you declare in .ascx in a <script runat="server"> tag. Those will not be visible to .aspx.cs since it's compiled beforehand.
C) ASP.NET will generate the class defined by the ClassName attribute in the ASP namespace. You should use ASP.Some_Name instead. There is one thing I forgot to mention: in this case, you should somehow reference the .ascx in the .aspx markup; either with a Reference directive (<%# Reference Control="MyControl.ascx" %>) directive or with a Register directive (<%# Register TagPrefix="abc" TagName="xyz" Src="MyControl.ascx" %>). This will ensure that the ASP.NET build engine puts the generated .ascx class in the same assembly as .aspx page.
if we don’t use code behind class,
then there won’t be any methods or
properties added to a control, so why
would web page have problems accessing
any of user control’s properties and
methods? So why would we need to
include a class name in Control
directive
You can embed any code behind code in your .ascx/.aspx file, in new asp.net mvc model, there won't be any code behind by default. I think code behind is optional, it's just a nice way to separate one class into two pieces. Partial class is used in code behind.
Even if you don't use code behind class, you can still inherited properties from base control. Then by declaring a class, you will be able to access those properties too.

Categories

Resources