Parser error copying a single aspx page with code-behind - c#

When I just copy (without deploying) an aspx page with code-behind to a web site I get a Parser Error Message when I try to use the page.
Let us assume this is just for testing purposes that I want to develop a single page in Visual Studio then copy (or FTP) it to a web site.
The complete error message is:
Parser Error Message: Could not load type 'SimpleSite.WebForm2'.
The WebForm2.aspx file is:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs"
Inherits="SimpleSite.WebForm2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
</div>
</form>
</body>
The WebForm2.aspx.cs file is:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace SimpleSite
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
TextBox2.Text = TextBox1.Text;
}
}
}
I am not sure if the namespace is the problem, it appears to be. I have used Expression Web to create an ASP.Net file with code-behind that I can just FTP
but that does not have a namespace (yes, I know; it is in the global namespace).
If I want to make one or two aspx files with code-behind just for educational purposes to be copied without deploying, can I do that using a Visual Studio Web Project or Web Site and maintain the page such that it still works as a page in the Visual Studio Web Project or Web Site?

Yes, the namespace is a problem.
Visual Studio creates pages for a Web Site without a namespace specified, in other words it creates pages with the code in the global namespace. Therefore simple pages (that do not use additional files) created in a VS Web Site can be copied individually by copying just the aspx file and corresponding aspx.cs file.
Another difference between a Web Project and a Web Site is that VS adds a aspx.designer.cs file for each page. It is not created for Web Sites.

Related

Error: ASP.NET Ajax client-side framework failed to load after some updates on host

I have a web site in windows host = https://blobloblo/WebForm5.aspx
When run that link on FireFox developer & take a look at Console logs,
I will see this error :
Error: ASP.NET Ajax client-side framework failed to
Because of this error many parts of this web site do n't work correctly.
Yesterdays every thing was ok.
But today they closed the host for some hours & did some updates such as plesk's update.
After running the host again this error appeared.
What should i tell them to fix this problem?
WebForm5 ASPX :
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm5.aspx.cs" Inherits="Virtual_Visa_Cards.WebForm5" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" Text="Button" Height="26px" OnClick="Button1_Click" /> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
WebForm5 C# :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Virtual_Visa_Cards
{
public partial class WebForm5 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "This is Ajax Call = CallBack";
}
}
}
Related Topic :
ASP.NET Ajax client-side framework failed to load. when put the ScriptManager on a blank page
I test all answers in the topic upper WITH NO RESULT.
In plesk i went to Web Application Firewall & put Web application firewall mode to off.
I worked one day to solve this issue.

Request.Form is empty (asp.net c#)

I've been doing some really clever stuff (I think) in ASP.Net c#, so much so that the simple stuff is more difficult (if that makes sense)
I have this snippet of code in my page
<form id="form1" runat="server">
<asp:HiddenField runat="server" ID="hdnConfirm" value="Hello World" />
<asp:LinkButton runat="server" PostBackUrl="/confirm.aspx" Text="Confirm">
</asp:LinkButton>
</form>
I have this snippet of code in confirm.aspx.
if !(IsPostback)
{
lblConfirm.Text = Request.Form["hdnConfirm"]
}
I was expecting this to be nice and simple but when i click the button and go to page "confirm.aspx" the Request.Form has no values. What have I missed ?
[TESTING]
I ran a test on a brand new web forms project in VS2013. Dot.Net 4.5.1 This does not work. PreviouPage is always null. Whether surrounded by (!IsPostBack) or not. Doesn't matter if the submitting control is a Button, LinkButton or Hyperlink. Request.Form["hdn"] is also null. I have restarted my computer just in case and still no joy. I am missing something really simple I am sure of it but I can't see what
This is the first page nothing in the code behind
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:LinkButton runat="server" PostBackUrl="~/WebForm2.aspx">click</asp:LinkButton>
<asp:HiddenField runat="server" ID="hdn" Value="3" />
</div>
</form>
</body>
</html>
This is the second page
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication2.WebForm2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
Code behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication2
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string s = ((HiddenField)this.PreviousPage.FindControl("hdn")).Value;
}
}
}
On confirm.aspx use PreviousPage.FindControl instead :
HiddenField hdnFieldName = this.PreviousPage.FindControl("hdnConfirm") as HiddenField;
string hiddenValue = string.Empty;
if (hdnFieldName != null)
{
hiddenValue = hdnFieldName.Value;
}
Here is good example to get you started.
What's going on Here:
By default with VS 2013 and Asp.Net v4.5.1 , there is a feature FriendlyUrls which is enabled.
The FriendlyUrls feature does kind of Asp.Net routing , thus changing URLs from localhost/webform1.aspx to localhost/webfrom1
PostbackUrl property will not work in combination with Asp.Net Routing. When ASP.NET routing is in use the PreviousPage URL is the final routed URL. The runtime will check for a file represented by the Routed URL now, which will be webform1 and NOT webfrom1.aspx. Since there is NO such file as webform1, so it will always set the PreviousPage to null.
Possible Solutions:
So, now you know the issue at hand.
1.) Either don't use the Routing system of Asp.Net Friendly Urls , in this case, therefore try adding the <%# PreviousPageType VirtualPath="~/WebForm1.aspx"%> to webform2.aspx page and check.
2.) OR if you keep the FriendlyURLs and hence the Routing system, make changes in your code to read the Form values using some other alternatives.

ajaxToolkit:ToolkitScriptManager not found

I am trying to get the AjaxControlToolkit (ACT) Auto complete extender working.
It was working briefly but has now stopped working and I can't figure out why. I haven't changed changed anything in my project that I can see would affect this.
I have even created a brand new VS2013 project and tried to implement this but still no joy.
I see several references to "ajaxToolkit:ToolkitScriptManager" but I cannot find a reference for this anywhere. I have tried installing the toolkit from a download as well as via NUGET and it simply will not work.
I have tried every example on here, at least 20 of them or more and still not joy. If you have it working please post your code because mine doesn't work and its pointless posting it be cause i have tried absolutely everything suggested here
Any help would be much appreciated as I really dont want to engineer something unelegant to get around this.
Thanks loads
[UPDATE]
ToolkitScriptManager is removed from ACT 15.1 see here So that answers this question but the autocomplete still is not working
[UPDATE]
Here is an example I put togetgher, which still does not work with or without the [System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()] directive
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="TestWebSite.WebForm1" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager runat="server"></asp:ScriptManager>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<cc1:AutoCompleteExtender ServiceMethod="GetCompletionList"
MinimumPrefixLength="1"
CompletionInterval="100"
ID="AutoCompleteExtender1"
runat="server"
TargetControlID="TextBox1"></cc1:AutoCompleteExtender>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace TestWebSite
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public static string[] GetCompletionList(string prefixText, int count, string contextKey)
{
string[] s = {"a","b","c","d"};
return s;
}
}
}
You need to register the AJAX Control Toolkit Library by putting the following line just below the #PageDirective
this is the ajaxtoolkit
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
then this is the example to call ajaxtoolkit
<cc1:toolkitYouWantToUse/>
thasts it
Ok I have it working. I created a brand new test projects in VS2013. I used the developer features of Google Chrome to debug and saw I was constantly getting a 500 error. In my experience I've usually caused this by getting my web addresses wrong so that led me to checking that the web service was working. (See further down)
After fixing that problem and knowing that the web service was OK I engaged in some trial and error, and the steps below solved the problem. I now have the AutoCompleteExtender working in all my projects using this same method.
(This may not be the way to do it but I know it works for me)
Remove the nuget installed version of the AjaxControlToolkit (ACT)
Manually add the reference to the ACT dll that I had downloaded, get the latest version from here.
Add a .asmx file to your project with the WebMethod defined as below. You can actually call the method any name you like the profile (e.g. parameters and return type) must remain the same. There may be other variations but I don't know what they are. I just know that this configuration worked for me.
Re-add the AutoCompleteControl. the VS IDE will add the #Register directive for you
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager runat="server"></asp:ScriptManager>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<cc1:AutoCompleteExtender ServiceMethod="GetCompletionList"
ServicePath="WebService1.asmx"
MinimumPrefixLength="0"
CompletionInterval="100"
ID="AutoCompleteExtender1"
runat="server"
TargetControlID="TextBox1"></cc1:AutoCompleteExtender>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace TestWebSite
{
/// <summary>
/// Summary description for WebService1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public string[] GetCompletionList(string prefixText, int count)//, string contextKey)
{
string[] s = { "a", "b", "c", "d" };
return s;
}
}
}
//List<string> works just as well as string[]
Run the project, manually navigate to the .asmx page and make sure that the function returns the results you expect. VS knows your running in debug so will let you add parameters and call the web method.

Pre-set formatting on asp.net pages.

I am newish to Asp.net. I have inherited a web application project. When I create a new page it already has some formatting on the top even though I don't use any master page when creating the page. This I can see when I build the page. I am using ASP.Net 4.0, C# language.
For the life of me I cant figure out as in the underlying code for the file there is no reference to any css or any elements. The code for page.aspx is as follows:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
</body>
</html>
The code behind is as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
Thanks in advance for any help I can get.
Oh Actually when you create a page in Visual studio. He create some by default code file and a project structure. Just why you show different type of file on solutions.
See on your aspx page header its define cs code file as CodeFile="Test.aspx.cs" and also Inherits test.
Also .csproj file that contains assembly references
.projRefs.sln file that contains the current project and all referenced projects
I figured it out. There was a theme applied which was adding reference to a stylesheet when compiling the page. I added disable theming tag which removed this reference for the particular page.

Parser Error: '_Default' is not allowed here because it does not extend class 'System.Web.UI.Page' & MasterType declaration

I recently converted a website project to a web application project in Visual Studio 2008. I finally got it to compile, and the first page (the login screen) displayed as normal, but then when it redirected to the Default.aspx page, I received an error:
Parser Error Message: 'SOME.NAMESPACE.MyApplicationName.WebApplication._Default' is not allowed here because it does not extend class 'System.Web.UI.Page'.
All of my pages inherit from a class called "BasePage" which extends System.Web.UI.Page. Obviously the problem isn't with that class because the login.aspx page displays without error, and it also inherits from that basepage.
All the pages on the site, including the login page, are children of a masterpage.
After some testing, I have determined what it is that causes the error (although I don't know WHY it does it).
On all the pages where I have the following tag, the error does NOT occur.
<%# MasterType VirtualPath="~/MasterPages/MainMaster.master" %>
On all the pages that do not contain that line, the error DOES occur. This is throughout the entire application. I have the tag only on pages where there has been a need to reference controls on the MasterPage.
So, I thought I would just add that line to all my pages and be done with it. But when I add that line, I get a compile error:
'object' does not contain a definition for 'Master'
This error is coming from the designer.cs file associated with the ASPX page that I have added the "MasterType" declaration to.
I've forced a rebuild of the designer file, but that doesn't change anything. I compared the content of the Master reference in the designer files between the login.aspx (working) and the default.aspx (not working) but they are exactly the same.
Since I'd really like to get it to work without having to add the "MasterType" declaration to everypage, and since that "fix" isn't working anyway, does anyone know why not having the "MasterType" declaration on an aspx file causes the parser error? Is there a fix for this?
Example Code:
Here is the code for login.aspx and login.aspx.cs which is working without error:
Login.aspx
<%# Page Title="" Language="C#" MasterPageFile="~/MasterPages/MainMaster.master" AutoEventWireup="true" Inherits="SOME.NAMESPACE.MyApplicationName.WebApplication.Login" Codebehind="Login.aspx.cs" %>
<%# MasterType VirtualPath="~/MasterPages/MainMaster.master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder" Runat="Server">
<table>
<tr>
<td>
<asp:UpdatePanel ID="upLogin" runat="server">
<ContentTemplate>
<asp:Panel ID="Panel1" runat="server" DefaultButton="Login1$LoginButton">
<asp:Login ID="Login1" runat="server" LoginButtonStyle-CssClass="button"
TextBoxStyle-CssClass="textBoxRequired"
TitleTextStyle-CssClass="loginTitle" >
</asp:Login>
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="upPasswordRecovery" runat="server">
<ContentTemplate>
<asp:PasswordRecovery ID="PasswordRecovery1" runat="server"
SubmitButtonStyle-CssClass="button" TitleTextStyle-CssClass="loginTitle"
SuccessText="Your new password has been sent to you."
UserNameInstructionText="Enter your User name to reset your password." />
</ContentTemplate>
</asp:UpdatePanel>
</td>
</tr>
</table>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="SideBarPlaceHolder" Runat="Server">
<h2>Login</h2>
<asp:Button ID="btnCreateAccount" runat="server" Text="Create Account" OnClick="btnCreateAccount_Click" CausesValidation="false" />
</asp:Content>
Login.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SOME.NAMESPACE.MyApplicationName.WebApplication;
using SOME.NAMESPACE.MyApplicationName.Bll;
namespace SOME.NAMESPACE.MyApplicationName.WebApplication
{
public partial class Login : BasePage
{
protected void Page_Load(object sender, EventArgs e)
{
Login1.Focus();
}
protected void btnCreateAccount_Click(object sender, EventArgs e)
{
Page.Response.Redirect("~/CreateUser/default.aspx");
}
}
}
Here is the code for default.aspx and default.aspx.cs which is throwing the parser error when viewed in a web browser:
Default.aspx
<%# Page Title="" Language="C#" MasterPageFile="~/MasterPages/MainMaster.master" AutoEventWireup="True" Inherits="SOME.NAMESPACE.MyApplicationName.WebApplication._Default" Codebehind="Default.aspx.cs" %>
<%# MasterType VirtualPath="~/MasterPages/MainMaster.master" %>
<asp:Content ID="MainContent" ContentPlaceHolderID="ContentPlaceHolder" Runat="Server">
<div class="post">
<h2 class="title">Announcements</h2>
<p class="meta">Posted by Amanda Myer on December 15, 2009 at 10:55 AM</p>
<div class="entry">
<p>The MyApplicationName CMDB will be down for maintenance from 5:30 PM until 6:30 PM on Wednesday, December 15, 2009.</p>
</div>
<p class="meta">Posted by Amanda Myer on December 01, 2009 at 1:23 PM</p>
<div class="entry">
<p>The MyApplicationName CMDB is officially live and ready for use!</p>
</div>
</div>
</asp:Content>
<asp:Content ID="SideBarContent" ContentPlaceHolderID="SideBarPlaceHolder" Runat="Server">
<img src="images/MyApplicationName.jpg" alt="MyApplicationName Gremlin" width="250"/>
</asp:Content>
Default.aspx.cs
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using SOME.NAMESPACE.MyApplicationName.Bll;
using SOME.NAMESPACE.MyApplicationName.WebApplication;
public partial class _Default : BasePage
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
Thanks!
I figured it out. The problem was that there were still some pages in the project that hadn't been converted to use "namespaces" as needed in a web application project. I guess I thought that it wouldn't compile if there were still any of those pages around, but if the page didn't reference anything from outside itself it didn't appear to squawk. So when it was saying that it didn't inherit from "System.Web.UI.Page" that was because it couldn't actually find the class "BasePage" at run time because the page itself was not in the WebApplication namespace.
I went through all my pages one by one and made sure that they were properly added to the WebApplication namespace and now it not only compiles without issue, it also displays normally. yay!
what a trial converting from website to web application project can be!
I had this issue, as I had copied a (fairly generic) webpage from one of my ASP.Net applications into a new application.
I changed the relevant namespace commands, to reflect the new location of the file... but... I had forgotten to change the Inherits parameter in the aspx page itself.
<%# Page MasterPageFile="" StylesheetTheme="" Language="C#"
AutoEventWireup="true" CodeBehind="MikesReports.aspx.cs"
Inherits="MikesCompany.MikesProject.MikesReports" %>
Once I had changed the Inherits parameter, the error went away.
Look for this:
ANY page in your project that has a missing, or different Namespace...
If you have ANY page in your project with <NO Namespace> , OR a
DIFFERENT Namespace than Default.aspx, you will get this
"Cannot load Default.aspx", or this: "Default.aspx does not belong here".
ALSO: If you have a Redirect to a page in your Solution/Project and the
page which is to be Redirected To has a bad namespace -- you may not get
a compiler error, until you try and run. If the Redirect is removed
or commented-out, the error goes away...
BTW -- What the hell do these error messages mean? Is this MS.Access, with
the "misdirection" -- ??
DGK
I had copied and renamed the page (aspx/cs). The page name was "mainpage" so the class name at the top of the cs file as follows:
After renaming the class to match this error was resolved.
For me I had all of the namespaces on the pages and none of the solutions above fixed it. My problem was in:
<%# Page Language="C#"
AutoEventWireup="true"
CodeBehind="xxx.aspx.cs"
Inherits="xxx.xxx.xxx"
MasterPageFile="~masterurl/default.master" %>
Then in my aspx.cs file the namespace did not match the Inherits tag. So it needed
namespace xxx.xxx.xxx
In the .cs to match the Inherits.
I had a similar error but not from a Conversion...
System.Web.HttpException: 'Namespace.Website.MasterUserPages' is not allowed here because it does not extend class 'System.Web.UI.MasterPage'
I was also extending the MasterPage class.
The error was due to a simple compilation error in my Master Page itself:
System.Web.HttpCompileException: c:\directory\path\Website\MasterUserPages.Master(30): error CS1061: 'ASP.masteruserpages_master' does not contain a definition for 'btnHelp_Click' and no extension method 'btnHelp_Click' accepting a first argument of type 'ASP.masteruserpages_master' could be found (are you missing a using directive or an assembly reference?)
I was not able to see the error until I moved the MasterPage to the root website folder. Once that was taken care of I was able to put my MasterPage back in the folder I wanted.
My issue was simple: the Master page and Master.Designer.cs class had the correct Namespace, but the Master.cs class had the wrong namespace.
You can always refractor the namespace and it will update all the pages at the same time. Highlight the namespace, right click and select refractor from the drop down menu.
I delete that web page that i want to link with master page from web application,add new web page in project then set the master page(Initially I had copied web page from web site into Web application fro coping that aspx page (I was converting website to web application as project))
Remember.. inherits is case sensitive for C# (not so for vb.net)
Found that out the hard way.
For me the fix was different. The corresponding aspx page has been unloaded from the project, I added that back and the error is gone.

Categories