ASP.NET adding a form to Master Page - asp.net

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" %>

Related

Error on Adding Master Page.

I have a using ASP.net C# firstly I have created a webform FrmFirst.aspx. Then I have created a master page. Now I have to add my first page to master page then I am facing the same error :
I am using this code to add my first page to Master page
<%# Page Language="C#" AutoEventWireup="true" MasterPageFile="~/MasterPage.master" CodeFile="FrmCustomerMaster.aspx.cs" Inherits="FrmCustomerMaster" %>
Tell me my Problem !! Thanks ....
You need to change your body to content sections. The master page works with contentplaceholders: your content pages need to provide the content for these placeholders
see LINK
if your master page contain this code
<asp:contentplaceholder id="Main" runat="server" />
your page that Inherits from master should have
<asp:Content ID="Content1" ContentPlaceHolderID="Main" Runat="Server">
</asp:Content>

Why is an added web form different from the default forms?

An ASP.NET Webforms project has some default pages ("About" and "Contact" to be specific), which have this type of content:
<%# Page Title="About" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeFile="About.aspx.cs" Inherits="About" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<h2><%: Title %>.</h2>
<h3>Your application description page.</h3>
<p>Use this area to provide additional information.</p>
</asp:Content>
Yet when you select Add > Web Form, it creates this:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="DuckbilledPlatypus.aspx.cs" Inherits="DuckbilledPlatypus" %>
<!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>
Why the difference/inconsistency? Note: This is in Visual Studio 2013; I'm not positive other versions are the same.
You didn't create the Web Form using the Master Page. When you right click the project, click Add Item, then under Web it will have Web Form and then other classes as well. Using the Web Form using Master Page should give you the one that you want. The one that it sounds like you used is the blank web form and has no site.master linked up to it where the Web Form using Master Page links up the site.master for you.

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 MasterPageFile causing error

I'm trying to use a master page in my website. I created a page and then created the master. I then added the tag MasterPageFile="~/master". I'm guessing that I need to do something to my main page, because I've now started getting the error:
The page contains markup that is not valid when attached to a master page
My main page is as follows:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" MasterPageFile="~/my.master" %>
<!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">
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
I tried deleting the tag thinking that this might be the issue. Can anyone point me in the right direction?
You need to change your body to content sections. The master page works with contentplaceholders: your content pages need to provide the content for these placeholders. See the msdn page on this topic.
Quoted from that link above, your master page could contain the following:
<td><asp:contentplaceholder id="Main" runat="server" /></td>
Which the content page would fill by supplying the following
<% # Page Language="C#" MasterPageFile="~/Master.master" Title="Content Page 1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="Main" Runat="Server">
Main content.
</asp:Content>
Note that I included the main declaration at the top of the content page.
the pages inhering master page should have a <asp:Content as their root. this means no html tag no doctype etc.

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";

Resources