w3school asp.net syntax appears to not be asp.net? - asp.net

I was looking at this page and the #{ look neat. I modified the example and made a new asp.net project. My code is below. My results were not expected. This showed up on my browser
#foreach(var row in new int[1,4,3]) { #row }
Now i'm assuming that the code on the page is asp.net and that i am doing something wrong.
<%# Page Language="C#" 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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
#foreach(var row in new int[1,4,3])
{
<tr>
<td>#row</td>
</tr>
}
</div>
</form>
</body>
</html>

This is Razor syntax, very easy and flexible used in asp.net mvc.
Visit the link

Use MVC3, search about it with Razor View Engine.

In MSVS2012, MVC is included with ultimate and express edition.
else you can take help from here
asp.net MVC4

Related

ASP.NET adding a form to Master Page

So I've never really coded a website using ASP.NET. I'm trying to add a search form to a masterpage for my site. The problem is the whole body is wrapped in a form tag, which makes the functionality of my new form non-existant. I have a habbit of making bad situations worse by messing around with things I don't yet understand. So I thought I'd ask for your advice, my thoughts were:
Remove the runat="server" form tag completely.
Close it before my form and replace it at the end of my form.
Code within the button (which I later noticed you can't open.
Give up on ASP.NET and go back to PHP lol.
Hope you can help.
Thanks.
Master page will be like this
<%# 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>
<asp:ContentPlaceHolder id="bodycontent" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
you can write the search page like this
<%# Page Title="" Language="C#" MasterPageFile="~/MasterPage.master"
AutoEventWireup="true" CodeFile="Search.aspx.cs" Inherits="Search" %>
<asp:Content ID="Content1" ContentPlaceHolderID="bodycontent" Runat="Server">
//dnt include form tag here having runat = server
</asp:Content>
don't add content to the master page (you can, but don't for this example), add it to the content-page (.aspx). having the form on the master page means you don't have to manually add a form to content-pages; it's there automatically. (everything on the master page will be on the content-page.)
as masood is showing, search.aspx (the content-page) is using a master page, MasterPageFile="~/MasterPage.master". content goes in the content-tag.
I realised as I am used to hard coding in open source languages, that I was using HTML specific syntax, which was conflicting with the tag surounding the whole masterpage. In using the tools on the side I found my solution.
Thank you.
Well open Visual Studio Click File -> New -> Website -> Empty Web Site.
Create a MasterPage.master and add it to you project.
Example:
<%# Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
<html>
//.......
</html>
Create a SearchPage.aspx and bind it with your master page. Example:
< Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="MasterPage.aspx.cs" Inherits="MasterPage" Title="Untitled Page" %>

Redirecting back from where user came from in ASP.NET Page Not code behind

I have a ASP.NET page what I got at the moment is
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="RedirectMeNow.aspx.cs" Inherits="abc.eyd._12.TEMPLATE.LAYOUTS.RedirectMeNow" %>
<!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>
</div>
</form>
</body>
</html>
how can I redirect a user back from where he came from here, not in code behind like here
Redirecting back from where user came from in ASP.NET User Control
OR I just want a ASP.NET page that redirects user on call
To avoid using code-behind, you have to use a client-side Javascript function:
window.history.back();
You can either bind it to an event or execute it during page load.
EDIT:
If u want to make this page a redirect-only page, you can add this code to your tag:
<head>
....
<script type="text/javascript>
window.history.back();
</script>
</head>
It will be executed as soon as page is getting loaded.
That's it!

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.

Want C# ASPX page to produce plain text rather than HTML

I have a C# project in Visual Studio that produces an aspx page. The code I am editing is in default.asp.cs. When I build the project a file default.aspx is produced. This file looks something like this:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="CSRValidationConnector._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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
When I make my web request most of this page comes back.
But I want the page to return only plain text and not any of this HTML. How do I configure things so that nothing is returned except what I add via Response.Write calls in default.aspx.cs?
Just remove all the HTML except:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="CSRValidationConnector._Default" %>
and in your codebehind Page_Load do a Response.Write("String")
Response.ContentType is what you are looking for :)
Response.ContentType = "text/plain";

Error with masterpage

i'm creating a brand new masterpage with VS2010 Beta 2 and I get this warning (that causes me errors in the content pages):
Validation (XHTML 1.0 Transitional):
Content is not supported outside
'script' or 'asp:content' regions.
The masterpage's code :
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="Bob.master.cs" Inherits="TShirtFactory.Web.All.Core.lib.masterpage.Bob" %>
<!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>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
As you can see, it's the default masterpage generated code. I get the warning when I hover the tag at top. Does anybody have an idea of what's going on ?
Thank you
In my case the error has gone after removing masterPageFile attribute from the Page-section in the web.config file:
<configuration>
<system.web>
<pages styleSheetTheme="mystyle" masterPageFile="~/myMaster.master" />
</system.web>
</configuration>
Or, if you need this attribute in the web.config file, just add empty MasterPageFile to you master page:
<%# Master Language="VB" AutoEventWireup="false" CodeFile="mySecondMaster.master.vb" Inherits="mySecondMaster" MasterPageFile="" %>
It's simple... Visual Studio is bonkers. Actually, the truth is that it can't possibly validate some markup simply because much of it is dynamic. For instance, in my project I have a constant warning about the lack of a <title> tag because it's added dynamically. Bottom line: the XHTML validator does not really know much about ASP.NET code.

Resources