One of our contractors gave us an ASP.NET 2.0 web site and I'm having a little trouble integrating it into our process. He gave us the project as a zipped directory, no solution file or anything. I can open it in Visual Studio using the Open -> Website option, and the website runs. However, I wanted to set it up as a web application project so that I could add project references to our shared libraries instead of them being linked into the 'bin' directory, and just because I'd prefer to have a regular project really.
I'm running into a weird issue. Let me extract a little code:
HeaderControl.ascx
<%# Control Language="C#" AutoEventWireup="true" CodeFile="HeaderControl.ascx.cs" Inherits="Controls_WebUserControl" %>
HeaderControl.ascx.cs
public partial class Controls_WebUserControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
Ok, this is a little strange but it seems to be legal at least. The base class has a partial definition in the code-behind (code file?). But then many other controls are defined in the same way, eg
SomeControl.ascx
<%# Control Language="C#" AutoEventWireup="true" CodeFile="SomeControl.ascx.cs" Inherits="Controls_WebUserControl" %>
SomeControl.ascx.cs
public partial class Controls_WebUserControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
Expectedly, when I try to build my new web application project that I've transferred the files into I get a bunch of errors about the same method being defined in several places. I don't understand why it's not set up with a code behind defining a partial definition of the control class and overriding methods of the base class, but that's sort of beside the point.
What really gets me is this: How does the original work? It runs and it works, so the compiler must be doing something that I don't understand. What happens to the Controls_WebUserControl class?
WHen you change from a web site to a web app, you need to right-click on the project and choose "Convert to Web Application". This should update all the pages.
This is an old link, but I think it's still relevant:
http://webproject.scottgu.com/CSharp/migration2/migration2.aspx
I would say that while the provided site works, it is incorrect.
It works because it is a Web Site project (as opposed to a Web Application Project). Web Site projects do not have a namespace for the web pages; instead it compiles each page separately. In other words, you could say that the class name for the pages is ignored.
It fails when you bring it into a Web Application project because that tries to build a namespace and that fails because of the common class names across the web pages and controls.
I'd right click, select Convert to Web Application, and then fix the class names on the pages & controls. That should get you to where you want to be.
Actually you need to replace CodeFile="SomeControl.ascx.cs" with CodeBehind="SomeControl.ascx.cs"
CodeBehind vs CodeFile
Related
I am making a class library in asp.net , is there any possibility to add web forms in class library ( content view files with their code-behind ) so i can run , only with a simple method in the CALLER PROJECT ,a whole web app (thanks to this class library) .
I searched and i found i can't but If not , is there any near solution which can solve this problem ?
Thanks !
is there any possibility to make a class library as a web app "template" that we can use every time we call it in another project
A "class library" project, in Visual Studio terms, is strictly namespaces and classes - no web forms or controls allowed. And you're right, these projects are easily shared among sites by leaving them in a common source control repository or similar.
In an "ASP.NET Web Forms Web Application" project, you can reduce duplication by moving as much "code behind" code as possible to class libraries, which can then be shared among your team(s). You could even theoretically implement a lot of page/control functionality as "base classes" stored in class libraries and then inherited in aspx.cs and master.cs "code behind" files.
You could also move as much "code front" as possible into Custom User Controls which could be shared across projects by linking source control to a common repository.
But I don't think there's a good way to "share" .master and .aspx files. You could set up a basic website, keep it in a common place, and let people copy/paste it for new projects. But I don't know of a useful way to share that UI code among multiple web app projects.
I'm working on my school project in which I need to build up one original web application. But I got an error when I tried to connect two web forms in my project. I have no idea what's wrong about it, and I have looked at many video instruction sources showing how to do it, and still I have the same error.
Here is what I have done.
First, I created a web from and named it MainForm
and also I made another one and named TestForm1.
Then, I put a button on TestForm1 aspx file, double-clicked the button to call the cs file, and there I coded
protected void Button1_Click(object sender, EventArgs e)
{
MainForm newWindow = new MainForm();
newWindow.Show();
}
Here, an error comes up and I see a red line under "Show"
The error says
MainFor1 does not contain a definition for "Show," and no extension
method accepting a first argument of type "MainForm" could be found.
What is wrong about my code? I just simply made two forms and am trying to connect them.
Please tell me how I can handle this problem.
Sorry for my bad English, since I'm not a native speaker.
And Thank you in advance.
According to my Understating you want to navigate to another page, you can add a link button to take you to the other page like so:
<asp:Button ID="Button3" runat="server" Text="Button" PostBackUrl="anotherpage.aspx" />
To show a new Page (not a Form!) means that you want the Web Browser to open a new URL. This can be done in many ways, and the way closest to your orignal code is this:
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("MainForm.aspx");
}
A Redirect literally tells the browser: please go to another URL. The browser then makes a new http request with the new URL, and the server then shows that page.
You need to use Server.Transfer(), method Show() can't be used in WebForms. Probably You are wrong WinFroms with WebForms.
Server.Transfer("TestForm1.aspx", true);
Here you find introduction to ASP.NET and Web Forms
Summary: This article explains how Web Forms are fundamental to Microsoft ASP.NET, shows how to build a Web form, and discusses the controls essential for building a Web Form. (16 printed pages)
Objectives
Learn about Web Forms
Learn the Web controls that are built into Web Forms
Build a Web Form
Actually those 2 links talks about the .net Reflector. But my problem is that when we choose the PUBLISH WEBSITE option for a WebSite in Visual Studio, and then in the next window that comes up, uncheck all the 3 options which are
Allow this precompiled site to be updatable --> Not Checked
Use fixed naming and singe page assemblies -->Not Checked.
Enable strong naming on pre-compiled assemblies --> Not Checked.
In the location where the files are placed , we can see .aspx file which has the content This is a marker file generated by the precompilation tool, and should not be deleted!.
Now in the current scenario that I can facing, some person has published the website , using the PUBLISH WEB SITE option and then deleted the existing dotnet code. Now if I reverse engineer using the .Net Reflector, I get back .aspx.cs files, but I cannot get back the .aspx files.. Actually the .aspx file contents are represented as a class which inherits from the Page Class of dotnet. Below is the sample snippet what I get from the reflector
public class HomePage : Page, IRequiresSessionState
{
// Fields
protected HtmlForm Form1;
protected Literal InvalidPortalHeader;
protected Literal InvalidPortalText;
protected Table Table1;
.................. Something like this.
If code is generated in this format, then I will have to add new controls to the webpage in this format. Is there a way that I can get back the .aspx file also (not only the .aspx.cs file), so that I can add new controls like Label, Textbox at design time using my Visual Studio.
Is there any idea to get that .aspx file with original code?
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
I tried the following:
I have a shared library (.dll) that contains these files:
Views
Search
PowerSearch.aspx
PowerSearch.aspx.cs
PowerSearch.aspx.designer.cs
The PowerSearch.aspx file contains my html code.
The PowerSearch.aspx.cs file contains this:
using System.Web.Mvc;
using CommonProject.Web.Shared.Controllers;
namespace CommonProject.Web.Shared.Views.Search
{
public partial class PowerSearch : ViewPage<SearchViewData>{}
}
And the designer I catually don't even care about cause it's not used anyways.
Nothing fancy, just a strongly typed view.
I basically pulled an existing, working view out of my asp.net mvc project, put it in a seperate library and changed the namespace to contain the word "Shared" as in "this will be shared amongst several mvc projects".
Then in my original asp.net mvc project I created the same structure, only now the aspx page contains nothing but the asp #Page rule.
The matching cs file contains:
using System.Web.Mvc;
using CommonProject.Web.Shared.Controllers;
namespace CommonProject.Web.DRE.Views.Search
{
public partial class PowerSearch : CommonProject.Web.Shared.Views.Search.PowerSearch { }
}
There are no compile errors and no run time exceptions either. There is only a huge blank page...
Anybody got an idea?
Make sure the assembly (dll) is in your bin folder that you are referencing. Also try adding the assembly namespace to your web.config otherwise your View will not be able to find the inherited page.
<pages>
<namespaces>
<add namespace="CommonProject.Web.Shared"/>
How much of a struggle was it to try to add an ASPX page to a class library?! It's not in the default list when you select 'Add New Item...'.
I know it's OK to store Controllers and Models in a separate library DLL/assembly, but I'm pretty sure it's not as trivial to store or share the Views. The problem is that the default view engine is looking in a specific folder on disk for the view (~/Views/Controller/ViewName.as[pc]x or ~/Views/Shared/ViewName.as[pc]x). With a library DLL, the compiler doesn't really have any idea of what it can do with your ASPX file. It's not code, so unless you have a 'Build Action' set, it's just going to ignore it. There are various 'Built Actions' but I think your only options are 'Copy' and 'Embed As Resource'. Copy isn't going to copy to the folder that you need it to (the Views folder in your ASP.NET MVC web project), although you could possibly write a build script or 'Custom Tool' that did that for you (with a bit of work).
Hammett (of Castle Monorail) fame (and now an MS Employee) came out with a sample that allows you to store Views inside library assemblies using a custom VirtualPathProvider class that is able to dig into the DLL and pull out the View (embedded as a resource). The sample application is just a concept right now, so you might hit some roadblocks, but it appears to work and looks like an exciting direction. You can find it on his blog here: MEF and ASP.NET MVC sample. Download the code and do some exploring.
This blog post ASP.NET MVC Plugins is not by the same author as the one above, but it gives another examination of the topic and points to another post here on StackOverflow where a similar question was asked: Using VirtualPathProvider to load ASP.NET MVC views from DLLs.
I've seen a post from Phil Haaaaaaaaaaaaaaaaaack on storing views in the database. It's using Ruby scripts instead of Web Forms, so I'm not 100% sure if you could adapt his sample to fit your needs or not. Check it out here: Scripting ASP.NET MVC Views Stored In The Database.
What does the source look like for the .aspx file? Make sure you are inheriting your view in the page directive like this:
<%# Page Title="" Inherits="CommonProject.Web.Shared.Views.Search.PowerSearch" %&>
Also, if you don't want to specify the whole namespace in your page directive, you can add this to the <namespaces /> section in your web.config:
<add namespace="CommonProject.Web.Shared.Views.Search"/>