asp:Panel runat="server" not available in C# .cs code page - c#

My ascx page:
<%# Control Language="c#" Inherits="FSB.Layouts.Short_Home_Loan_Application" CodeBehind="Short Home Loan Application.ascx.cs" %>
<div class="form">
<form action="" method="post" rel="shortForm">
<div class="section">
<asp:panel id="shortForm_wrapper" runat="server">
this is the first panel
</asp:panel>
<asp:Panel runat="server" ID="thankYouWrapper" >this is the second panel</asp:Panel>
</div>
</form>
</div>
<sc:sublayout runat="server" path="~/layouts/FSB/Document Checklist Lightbox.ascx" id="checklist" />
My CS Page:
using System;
using Sitecore.Data.Items;
using Sitecore.Links;
using Sitecore.Data.Fields;
using Sitecore.Web.UI.WebControls;
using System.Net;
using FSB.Helpers;
using System.Configuration;
using System.Text;
using System.Web;
using System.Web.SessionState;
namespace FSB.Layouts {
public partial class Short_Home_Loan_Application : System.Web.UI.UserControl {
protected void Page_Load(object sender, EventArgs e) {
thankYouWrapper // not accessible here
}
}
}
NOTE: This is a CMS project and uses Sitecore. I don't actually know what it is may be this helps. I am totally new to C# and have been thrown into bunch of C# websites.
Thanks in advance

I assume you have a Web Application Project instead of a WebSite Project. As such, in the Solution Explorer, navigate to your *.ascx file, expand it to see the C# code-behind and you will also see a *.ascx.designer.cs file. Delete that "designer" file. Next, right-click on your *.ascx file and select Convert to Web Application which will re-generate the designer file. Now try to access the control via C#.

use "CodeFile" instead of "CodeBehind"
<%# Control Language="C#" Inherits="FSB.Layouts.Short_Home_Loan_Application" CodeFile="Short Home Loan Application.ascx.cs" %>

You can adjust theses elements :
1 CodeFile
CodeFile="Short_Home_Loan_Application.ascx.cs" %> //AddCodeFile, and set the same name
2 Runat
Add runat="server" to your forms
3 And replace your Panel with Div Container

In your ascx page header, modify your Inherits directive to include the Namespace prefix in your code behind.
Revision
I have taken your original markup as posted and placed it in a test web application, and it compiles cleanly with the proper references available in the codebehind using VS2010.
Is there any possibility that this control was originally authored under a different name, in a different namespace, or copied from a different project and pasted into a shell source file in this project? The problem you are experiencing seems directly tied to the absence of the intermediate files ASP.NET generates in the background to establish protected declarations for markup-based entities that can be referenced in the codebehind of the control's source. If, for some reason, VS2010 cannot detect the need for these intermediate files to be regenerated, the markup and source will become out of sync and fail in a manner similar to what you are experiencing. I obviously cannot guarantee that is the cause of your particular problem, but I would at least consider it a strong suspect.
I would respectfully suggest that you create an entirely new control within VS2010, via the "Add New Item->" option in Solution Explorer, and add just the markup from this control into the ascx file (not the directive line), and then see if the code-behind for that new control can reference the server-side elements.
Good luck.

Related

Parser error copying a single aspx page with code-behind

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.

How can I make controls added to existing mark-up be automatically hooked up to the code behind?

I have an existing page.aspx with a bunch of controls:
<%# Page Trace="false" Language="c#" Codebehind="page.aspx.cs"
AutoEventWireup="true" ValidateRequest="False" %>
<asp:CheckBox id="test1" runat="server" />
<asp:CheckBox id="test2" runat="server" />
<asp:TextBox id="test3" runat="server" />
And a code behind page.aspx.cs that declares a protected member with the same type and name as each control in the markup:
protected CheckBox test1, test2;
protected TextBox test3;
When the page is created, these three members are automatically initialized to refer to the appropriate controls.
I then add a new control:
// In page.aspx
<asp:TextBox id="test4" runat="server" />
// In page.aspx.cs
protected TextBox test4;
The test4 member is not initialized. It remains null.
The only 'solution' I can find is to convert the page to a web application, which doesn't work in this case. It creates a designer file (which originally didn't exist for this) and removes all of the protected members from the code behind, breaking compilation.
How can I make it so that my new controls are hooked up automatically?
If it's a web application your aspx pages should have a designer file by default. But, if your project is a website then .aspx pages will not have a designer file and each page is compiled on the fly by VS compiler. there are few files being created by VS in following temp folder (path depends on your .Net Framework version)
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files
Having said that you can create aspx files without a designer file by removing the Inherits attribute of the #Page directive. Then you have to initialize your controls by your self, which is not automated. It's worth trying a clean build.

CKeditor on Textarea implementation

I have implemented a CKeditor from the below link:-
CKEditor But the issue is that, As soon as I register the editor on my page, it gets reflected. I want the same editor just only for my asp.net textbox. What should I do and make change so that It can only be visible to my textbox only. Please help.
See my textbox
<asp:TextBox ID="txtPostdesc" CssClass="form-control" runat="server" ValidationGroup="AddNew" TextMode="MultiLine"></asp:TextBox>
As per the article CKEditor in ASP.Net it described the way of implementing CKEditor with dll.
You would require following things
1. Two dll : CkEditor.dll and CKEditor.NET.dll.
2. CKEditor folder containing all js, css and images.
Register the CKEditor control at the top of your .aspx page such as
<%# Register Assembly="CKEditor.NET" Namespace="CKEditor.NET" TagPrefix="CKEditor" %>
Now you will be able to write the CKEditor server control markup such as below
<CKEditor:CKEditorControl ID="txtPostdesc" BasePath="/ckeditor/" runat="server">
</CKEditor:CKEditorControl>
In above I just change the ID as per your textarea ID. Now you can set and get its content via .Text Property in your code behind file i.e.
string str = txtPostdesc.Text;
Hope above explanation works for you.

How to load Subgurim GMap object in razor(MVC3/4)

I'm using ASP.NET MVC 4 and I'm trying to load a Google Map on my page.
I'm using Subgurim lib and already have the map object, but I don't know how to load it into the page.
Here's what I got so far:
#model Subgurim.Controles.GMap
#{
ViewBag.Title = "Pesquisa Carona";
}
<h2>PesquisaCarona</h2>
<h4>Diga o endereço</h4>
<input type="text" name="txEndereco" />
<input type="submit" />
<cc1:GMap ID="googleMap" runat="server" /> <!-- This doesnt work!!!--!>
I know that normally I would add the map accessing it directly like:
For the serverside:
GMap1.addGMarker(oMarker);
And for the page:
<cc1:GMap ID="GMap1" runat="server" Height="300px" Width="300px" />
But with MVC I can't do that.
Cheers!
I was able to the the basic example to work using .NET 4.5, MVC 5 (razor), GMaps 4.1.0.6, and Visual Studio 2013 by rendering an ascx user control as a partial in the cshtml. Here is what I did to get it to work:
Add the Subgurim.Maps package from NuGet.
Add an ascx control: In my case it was Gmap.ascx placed in the same folder as the cshtml I wanted to use. Your code should end up looking like the following:
In the ascx:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="Gmap.ascx.cs" Inherits="YourApp.Views.Gmap" %>
<%# Register Assembly="GMaps" Namespace="Subgurim.Controles" TagPrefix="gmaps" %>
<form id="form1" runat="server">
<gmaps:GMap
ID="GMap1"
runat="server"
Width="500px"
Height="250px"
EnableViewState="False" />
</form>
In the ascx.cs:
namespace YourAPP.Views
{
using System;
using System.Drawing;
using System.Web.Mvc;
public partial class Gmap : ViewUserControl
{
protected void Page_Load(object sender, EventArgs e)
{
GMap1.enableDragging = false;
GMap1.Language = "en";
GMap1.BackColor = Color.White;
GMap1.Key = "YOUR GOOGLE KEY";
GMap1.CommercialKey="YOUR COMMERCIAL KEY";
}
}
}
In the cshtml:
#Html.Partial("Gmap")
** A couple of things to note:
The form tag in the ascx is required it won't work without it.
The control needs to inherit from ViewUserControl instead of UserControl to work with razor.
Make sure you have your keys for the control (Google, Commercial). See the control site for details on those.
If you didn't put the control in the same folder as your cshtml you will need to adjust the path given to the Partial helper.
Thank you to:
The subgurim forums for the last bit about needing the form tags and the basic example.
How to display WebForms .ascx as partial view in MVC 3 for how to load an ascx into a razor view.
If I left anything out please go easy this is my first one.
I hope this helps - Cheers,
Anker
I've got the same problem.
On my project (made with MVC3) I've added a partial view in aspx engine (the rest of my website is with razor). I followed the subgurim tutorial site. Then I added the google api key but I get a gray rectangle even if everything seems ok!
Then I found this post on google: http://it.googlemaps.subgurim.net/Foro/general-discussions-perm5956-5963.aspx
:(
I'm one of the developers of the project and we are working on a new version suitable for MVC (Razor or other view engine of choice). I'll update this response when it's ready for production.

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