How do I use my HTML page inside my ASP.NET page? - c#

I have a .html.
I also have a .aspx that have two panels. I want to display my .html page between the panels, how can I do that?
I would like my .aspx page to end up with following structure. If I just drag the .html file in, it only creates a hyperlink. Should I put the .html code into a asp web forms instead?
...
<asp.Panel ...>
</asp.Panel>
.html
<asp.Panel ...>
</asp.Panel>
...

If you want it in the same page, you could just paste the contents of the html body between your panels.
If your html is going to change in future, and you want it to remain a different page, use an iframe and set it to display your html page.
If you want to combine both on the fly, you can use a asp.net Literal control, read the contents of your html page and set the contents of the html to the Text property of Literal

You can use iframe to display html page.

Use like this:
<iframe width="100%" height="100%" src="a.html" />

Convert your html page as user controls. Then include that user controls in aspx page.
Details:
1. Create a user control. ( .ascs extention file ).
2. Place html code inside user control.
3. Create asp page.
4. Include user control in asp page via following code.
<%# Register src="control.ascx" tagname="controlName" tagprefix="pre" %>
<pre:controlName ID="controlId" runat="server" />

Related

Load HTML content on Aspx page Dynamically

I'm trying to generate a page with HTML sent via parameter, but I have no idea how to do it.
For exemple: I'm creating the whole HTML code on the page 'page1.aspx' and I want to show this content on another page, 'page2.aspx', preferably in another tab.
Is there a way to do this?
I'd like not to create a new aspx file everytime I do this, rather, use the same page2.aspx file and just replace the content dynamically.

Making a asp.net web site template without using master page?

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

Including a Handler Response inside a Web Form

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

how do I show a string as html content in web form

I am trying to retrieve a html document to show in my web page, now the content comes from database as a string. Now I know there is a way to do this in win forms using Browser.DocumentText. But how do I do this in web form?
I tried the setting the innerHTML property for a div inside of the "OnRowCommand", the "OnRowCommand" happens to be inside an update panel. When I move the Div outside the panel, say to just below the body, it renders well.
Well there are many ways to do this, you can use a label, literal Controls.
Or maybe defining a public string within your page then use it directly in your html as:
<%= strSomeString %>
Add a literal control in aspx file and in codebehind set
Literal1.Text=data_from_DB;
Try this one:
html code:
<div id="webcontent" runat="server">
</div>
Code behind:
webcontent.InnerHtml = FromDBstring;
Write up for Mvc app Html.Raw('html tages') or MvcHtmlString.Create('html tages') and for web form HttpUtility.HtmlEncode('html tages')

Creating web controls inside a content placeholder programmatically

I've got a master page and a content page. How can I add controls to the master page's content placeholders programmatically?
Try something like following:
this.Master.Controls.FindControl("ContentPlaceHolderID").Controls.Add(yourControl);
this.Page.Master.FindControl
As said above would work fine.
Alternatively, make whatever placeholder in your master page a ContentPlaceHolder, and then your child pages can put stuff there directly without having to go up through the Master page.

Categories