I have an aspx page that needs to be an embedded resource for a WCF project. An interfacing service serves the aspx/html/js files to the web browser via a Stream object. The problem I am having is getting the aspx page to display properly. I have tried setting the content type to "text/html", but when I do this, I get the following at the top of the page:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="Services.Interface.Views.WebForm" %>
Which is the first line of Index.aspx.
Since it's being served as an html file, the aspx "stuff" is ignored, I guess.
I've also tried setting the content type to "application/xhtml+xm" as some have suggested, but I get an error that says:
This page contains the following errors:
error on line 1 at column 2: StartTag: invalid element name
Below is a rendering of the page up to the first error.
I have tried taking out the ASP stuff from Index.aspx and changing it to an HTML file, and it works, but I need the ASP capabilities for accessing some settings in Web.config.
Is there a proper content type for aspx pages that I'm not aware of, or is something else going wrong? Thanks in advance
Related
I am very new to asp.net and my job.
I was assigned a project to make a simple online order web application using asp.net c#.
The specification has been strictly defined (copied below)
Regarding the common content of the site, I need to make head, top and left(a search function)
"The design in /design/: Head.aspx Top.aspx Left.aspx"
-- Does that mean I am not allowed to use (nested) master page?
--- If so, how can I make a template without using master page?
Another option is to use the Server.Execute() method to render the contents of three separate pages into your base page if you have to use pages instead of usercontrols and still must have three separate pages rendered into one.
In the old days we did this with iis html includes.
If the head/top/left content is just basic HTML, then you could just put the HTML in a separate file and use an include link/reference in the original .aspx page.
If the content is all like a search function, then I agree with Henk's comment, create an .ascx User Control (which is really no different than an .aspx page with an .ascx extension) and then just reference that control on your .aspx page like this:
//put this at the top of your .aspx page
<%# Register src="usercontrols/YourControl.ascx" tagname="nameOfControl" tagprefix="ucControlName" %>
//then reference your control where you want it in your .aspx page
<ucControlName:nameOfControl ID="nameOfControl" runat="server" Visible="True" />
Here's an MSDN article on creating user controls: MSDN User Control
Maybe shoot the person who wrote the spec an email and ask why they have an issue with using a master page - this is kind of exactly why you would use one?!
JM
I am generating HTML in the Page Load method in more than 1 page.
All those generated HTML are the same across all pages.
I found that it's a pain to have the same codes in every page because once I need to change something, I need to change them in all pages.
Example:
I have a div in each of those page:
<div id="Questions" runat="server"></div>
In the Page Load method of each page, I generate the same HTML.
Questions.InnerHTML = "<span>...etc...</span>";
So I decided to make a page that generates those contents, then load this page inside the div of the other pages, means, if I ever need to change, I only change this page.
I created a Handler, Questions.ashx. This handler generates that HTML and sends back a response.
Now to include it, I know I can use JQUERY's .load() function, but I would like to generate those HTML from server side.
What I've tried:
Questions.InnerHTML = LoadControl("~/Handlers/Questions.ashx").ToString();
But I received this error:
Type 'Questions' does not inherit from 'System.Web.UI.UserControl'.
"LoadControl" is for "User Controls", not HTTP Handlers..
You will probably be better off creating a User Control, which is an .ascx file. This can contain HTML, ASPX controls and code behind, and can be referenced by any ASPX page.
More Info here:
http://msdn.microsoft.com/en-us/library/y6wb1a0e(v=vs.100).aspx
So I'm trying to use C# to grab stored HTML in a database and then display it between a set HTML header and footer (I have a couple of these for different pages, so a masterpage would make life more complicated rather than less. I also come from a PHP background where this is sort of the way things work).
<!--#include virtual="header.html" -->
<% #Page Language="C#" Debug="true" %>
<% #Import Namespace="System.Data.Odbc" %>
<% #Import Namespace="System.Web.Configuration" %>
<script language="C#" runat="server">
void Page_Load(object sender,EventArgs e) {
//Gets the HTML Successfully
//Response.Write(fetched_html);
}
</script>
<!--#include virtual="footer.html" -->
Unfortunately with the current workflow the code generates content before the header or footer are put on the page, putting all the stored HTML at the top of the page before the header. I've tried a couple different Page_Events and none of them seem to work, so I need to know what to do.
Is there an equally simple way to reference and insert a simple html header file that would render before any of the Page_Events? I've tried Page_PreLoad and others to no success. Either the code is not fetched at all or I run into the same issue.
Use a MasterPage with a ContentPlaceholder. Then, on the content page, use a Literal and put the HTML in there.
See "The difference between literal and label".
Try this:
Assuming you have data holding some where in memory like datatable or dataset or collection.
To display a html text on the page we generally use <asp:Literal/>. So place a asp literal control on the page. It works in between <body> </body> tag.
Now render the text to the control using its Text property. e.g.
ltl.text = dt.rows[0]["htmlcol"].tostring();
To dynamically add text inside <head> </head>, u need HtmlMeta,Page etc class.
i have use java script successfully running in my project but when i have to use master page and content page for this purpose i create master page and copy all css and script in the head tag of the master page and rest of the controls in the body tag
then i did copy all css and script in the asp:content1 tag while all other thing in the another asp:content2 tag abviously removed all form tags
when i compile content page it gave error
"'serializer' does not exist in the current context " in the source file master
actually i use this code in which is actually giving error,
i use this namespace "using System.Web.Script.Serialization;"
then use this code in the code behind file,
protected double[] Pvar;
protected JavaScriptSerializer serializer = new
System.Web.Script.Serialization.JavaScriptSerializer();
Pvar[runs] = Convert.ToDouble(output.Tables[0].Rows[runs][2]);
then for accessing this this array in client side javascript i use this code,
var Arr=<%= serializer.Serialize(Pvar)%>;
it giving error in this line but remember code is correct because it is working on a simple ASPX page on the same time side by using master page and content page ..
Hopes for your suggestions,
From which page, are you accessing the serializer object? You cannot access protected variable from content code-behind in master page markup and vice versa. So make sure that's not being the case otherwise you need to create public properties/methods and access them by casting Master property in content page (or Page property in Master page) to correct code behind class.
I'm working on a web form which works in a following way.
Read email template from database
Display email template on a web form in HTML format
User adds additional information to the web form and clicks on submit button
Before I get to a method which will process that request, I get A potentially dangerous Request.Form
I have looked at few articles that advise using .Net 2.0 in one of the web.config sections - that didn't work. I have set requestValidation = "false" for that page and it didn't work either.
My gut feeling is that I'm doing something fundamentally wrong...
HTML template is stored as VarChar(4000) in a database.
I have tried encoding text in a method before I send an email, but that didn't work either because the web form never got to executing that method.
What other options do I have? I have tried storing plain text in database, but then I have issue of tabs and returns etc.
Thank you
The remedy is in two parts and you MUST action both:
To disable request validation on a page add the following directive to the existing "page" directive in the file (you will need to switch to the HTML view for this):
ValidateRequest="false"
for example if you already have:
<%# Page Language="vb" AutoEventWireup="false"
Codebehind="MyForm.aspx.vb"
Inherits="Proj.MyForm"%>
then this should become:
<%# Page Language="vb" AutoEventWireup="false"
Codebehind="MyForm.aspx.vb"
Inherits="Proj.MyForm"
ValidateRequest="false"%>
In later versions of Visual Studio the value of this property is available via the page properties, so simply set "ValidateRequest" to "False". Either method of setting this achieves the same result.
Alternately, you can globally turn request validation off (but in which case be sure to implement item two below). To globally turn request validation off add the following to your web.config file:
<pages validateRequest="false" />
From: http://www.cryer.co.uk/brian/mswinswdev/ms_vbnet_server_error_potentially_dangerous.htm
As a first security lesson, never trust user input,so if you setting request validation to false then always HTML encode the input.
In basic either use: OnClientClick on submit and replace, < with & lt; and > with & gt; (no space with & and gt/lt)
or on submit method, use Server.HTMLEncode(inputtext)..or however you process it.