"Content Not Supposed to Be Outside 'Script' or 'asp:Content' Regions" - asp.net

I'm starting to learn ASP.NET and it's the little things that are really annoying with Visual Studio Web Dev Express. I keep getting the error "Content Not Supposed To Be Outside 'Script' or 'asp:Content' Regions" - so I put content inside an asp:content container and it doesn't disappear. I also get the same sort of error with declaring the DocType. Any suggestions to fix this. Also on an off-note: is there any better IDE's to code ASP.NET?
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>Hello VS2010</title>
</head>
<body>
<asp:content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<form id="form1" runat="server">
<div>
<asp:Literal ID="Literal1" runat="server">
Hello World
</asp:Literal>
</div>
</form>
</asp:content>
</body>
</html>
Master Page
<%# Master Language="C#" AutoEventWireup="true" CodeFile="Site.master.cs" Inherits="SiteMaster" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head runat="server">
<title></title>
<link href="~/Styles/Site.css" rel="stylesheet" type="text/css" />
<asp:ContentPlaceHolder ID="HeadContent" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form runat="server">
<div class="page">
<div class="header">
<div class="title">
<h1>
My ASP.NET Application
</h1>
</div>
<div class="loginDisplay">
<asp:LoginView ID="HeadLoginView" runat="server" EnableViewState="false">
<AnonymousTemplate>
[ Log In ]
</AnonymousTemplate>
<LoggedInTemplate>
Welcome <span class="bold"><asp:LoginName ID="HeadLoginName" runat="server" /></span>!
[ <asp:LoginStatus ID="HeadLoginStatus" runat="server" LogoutAction="Redirect" LogoutText="Log Out" LogoutPageUrl="~/"/> ]
</LoggedInTemplate>
</asp:LoginView>
</div>
<div class="clear hideSkiplink">
<asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" EnableViewState="false" IncludeStyleBlock="false" Orientation="Horizontal">
<Items>
<asp:MenuItem NavigateUrl="~/Default.aspx" Text="Home"/>
<asp:MenuItem NavigateUrl="~/About.aspx" Text="About"/>
</Items>
</asp:Menu>
</div>
</div>
<div class="main">
<asp:ContentPlaceHolder ID="MainContent" runat="server"/>
</div>
<div class="clear">
</div>
</div>
<div class="footer">
</div>
</form>
</body>
</html>

Master pages are a kind of template applied to all of your pages. Here it seems you use one without knowing it
MasterPageFile="~/Site.master"
In the masterPage you have all the content you want to be common to all your pages and a asp:content region where you can add content for each page.
So here you can try to remove the masterPage reference or just Adding and removing things in the asp:content region.
Here's the msdn link about MasterPages. I think it really necessary to understand masterPages to learn asp.net.

New Reply:
Well, okay.
So first things first only one form tag per page, that includes pages within master pages.
You don't need the form tag or the html, body or head tag on your Web form, only your master page.
So Page.aspx code should be like: http://codepaste.net/87n474
Note you need to include all content areas in your aspx page that are available in your master page.
Original Reply:
Couple of things.
Check your masterpage layout and see if there is any content misplace in that. (paste it here as well)
Also related to your masterpage, I don't see much point in having a masterpage if you're putting all your HTML markup inside the asp.aspx page.
Your doctype should be:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
Note the small differences in capitalisation.
Finally, the only IDE any better than web dev express is the full version of Visual Studio, there isn't any better version for creating and debugging asp.net sites than the latest version of visual studio.

Related

Image wont show on visual studio design time

I have a virtual directory on my iis -portal, which contains images. I put an image tag in my asp.net form.
<img src="http://localhost/portal/images/button_right.gif" alt="Alternate Text" />
This is the code:
<%Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication15.WebForm1" %>
<!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>
<form id="form1" runat="server">
<div>
<img src="http://localhost/portal/images/button_right.gif" alt="Alternate Text" />
</div>
</form>
</body>
</html>
The image shows when I run the project, on the browser.
The image doesnt show in design time. - I see X, instead of an image.
I set all read write execute permissions alowed o the virtual directory.
You should never specify image src as an absolute path. Use ~ to address your application path. It requires the runat="server" attribute since it is substituted by IIS at server.
<img runat="server" src="~/images/button_right.gif" alt="Alternate Text" />

problems when adding server side control

I have a fully HTML project, with complete .CSS file.
The thing is I want to add some asp.net controls like TextBox's and Buttons and what not, but when I add server side controls to it and run the pages it gave me errors I cannot follow because it appears on the tab of the browser.
The error start with control 'txt' of type TextBox..., and then it gives me Navigation Cancelled, and the browser direct me to
http://youtubedownloader.mybrowserbar.com/cgi/errors.cgi?q=http://localhost:55942/Dar%2520Al-Thaqafah/Default2.aspx&type=dns&ISN=A9B31DEE919345BE838EAF628EAFA22F&ccv=149&cnid=937811&cco=US&ct=1&sc=500
Here is the code of default2.aspx page
[%# Page Language="VB" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Default2" %]
[div class="header"]
[div class="logo"></div]
[div id="menu"]
[ul]
[li class="selected"][a href="Default.aspx"]Home[/a][/li]
[li][a href="Default2.aspx"]Available Books[/a][/li]
[li][a href="Default3.aspx"]Register[/a][/li]
[li][a href="Default4.aspx"]Checkout[/a][/li]
[li][asp:TextBox ID="txt" runat="server"][/asp:TextBox][/li]
[li][asp:Button ID="btn" Text="hhhhh" runat="server" /][/li]
[/ul]
[/div]
[/div]
[/div]
I am using Browser IE version 9.
Visual Studio 2005.
Thank you
Try with the code given below:
<%# Page Language="VB" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Default2" %>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div class="logo"></div>
<div id="menu">
<ul>
<li class="selected">Home</li>
<li>Available Books</li>
<li>Register</li>
<li>Checkout</li>
<li><asp:TextBox ID="txt" runat="server"></asp:TextBox></li>
<li><asp:Button ID="btn" Text="hhhhh" runat="server" /></li>
</ul>
</div>
</form>
</body>
</html>
Well with the limited information you provided, I guess the problem is that you are adding ASP.NET controls to an HTML page and not an ASP.NET page and that's why you see the errors. The ASP.NET controls are executed by the server and converted into Browser-readable HTML controls. So, you can only add ASP.NET controls to only ASP.NET page.

Separate asp.net pages into parts

Hi can I break the aspx into parts? E.g., one page for head, one page for body, and one for footer, and then combine them? Like that I can hide my JavaScript. Please advise. I can do so in python.
use ASP.NET master pages:
ASP.NET Master Pages
these are designed exactly for what you need/asked
for example you define content place holders in the master page like this:
<%# Master Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML
1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server" >
<title>Master page title</title>
</head>
<body>
<form id="form1" runat="server">
<table>
<tr>
<td><asp:contentplaceholder id="Main" runat="server" /></td>
<td><asp:contentplaceholder id="Footer" runat="server" /></td>
</tr>
</table>
</form>
</body>
</html>
and you populate from another page based on that master page the Main and Footer contents like this:
<% # Page Language="C#" MasterPageFile="~/Master.master" Title="Content Page 1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="Main" Runat="Server">
Main content.
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="Footer" Runat="Server" >
Footer content.
</asp:content>
You can do this with MasterPages and ContentPages
Have you tried to use a master page?
http://msdn.microsoft.com/en-us/library/wtxbf3hh.aspx
Also, if you are just trying to keep your javascript separate, you could put all of the javascript into a .js page and include that into you master.

Events only firing on specific machines but not on others

I have been trying to solve this problem for several weeks now and it is getting really frustrating. Basically i have a simple project which includes one master page and one content page. the following code is what is found in the content page
<%# Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:Button ID="Button1" runat="server" Text="ButtonContent"
onclick="Button1_Click" />
</asp:Content>
The button is bound to a simple event. A break point is set to detect whether the event fired as expected. The event does not fire when a form is placed above the content place holder.
<%# Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
<!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>
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
<form></form>
</div>
<div>
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
<div>
</div>
</form>
</body>
</html>
however the event is fired when the form is removed or place below the content place holder. On the other hand, on some machines i have found that it does not matter if a form is placed before the content place holder. I have tried this project on 4 machines (three windows 7 and one xp) it has worked regardless of form placement on two machines and has been subject to form placement on the other two.
Could some one please point me to some setting which could solve this problem or offer any advice to solve my problem. I need a form before the content place holder so removing is out of the question. It would also make sense that some sort of setting is preventing the project from working correctly on another machine
per HTML spec, you can not have form inside form. If I remember correctly some browsers break when you have this kind of structure. I think IE works fine.
If you need another form, put it outside of the <form id="form1" runat="server">
- above it, specifically. That is valid.

Asp.net Masterpage not working

<%# Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
<!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>
<link rel="stylesheet" type="text/css" href="Home.css" />
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
<div id="banner" style="font-family: Calibri, Serif; color: #FFFFFF">
blah blah
</div>
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
Home.aspx
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Home.aspx.cs" Inherits="Home"
MasterPageFile="~/MasterPage.master" Title="Welcome to StuartStudios!"%>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
</asp:Content>
Does Home.aspx not print whatever I define in the ContentPlaceHolder1 ? At the minute it prints nothing out. :S
When you specify markup in a ContentPlaceHolder inside of a MasterPage, that markup will only be rendered if the pages that use your master pages don't use the ContentPlaceHolder. So in Home.aspx, if you get rid of the Contact1 element, you should see your "banner" div.
So if you want your banner div to appear on ALL pages, you should move it to outside the ContentPlaceHolder control.
On your individual pages that use your master page, the Content tag should contain the stuff that's unique to the specific page.
No... ContentPlaceholder1 should be left blank in the masterpage.
I am no expert in ASP.Net, but as far as I understand the way <asp:ContentPlaceHolder /> works, when you add a <asp:Content> tag in your page file, you override the content of the ContentPlaceHolder in the master page. The contents of which will only be displayed if you do not have a corresponding Content tag.
Two things I see:
Your div in your master page has color set to white. So unless I'm mistaken, you're printing white on white.
Your Content item in Home.aspx IS empty, so why would it print anything else?

Resources