render .htm page into .aspx page - asp.net

in my .aspx page i want to render a .htm page which has some data
in .aspx page i have:
<% Html.RenderPartial("/Views/Templates/HTML_Temp.htm"); %>
but this gives runtime error:
Server Error in '/' Application.
There is no build provider registered for the extension '.htm'. You can register one in the section in machine.config or web.config. Make sure is has a BuildProviderAppliesToAttribute attribute which includes the value 'Web' or 'All'.
how to resolve this problem...please help

try this instead:
<%= File.ReadAllText(Server.MapPath("/Views/Templates/HTML_Temp.htm")) %>

<%= System.IO.File.ReadAllText(Server.MapPath("~/Views/Templates/HTML_Temp.htm")) %>

Use #include
The simplest way to put some HTML content in your containing page is by using a server include with #include:
<!-- #include file="../Templates/HTML_Temp.htm" -->
// for relative paths
or
<!-- #include virtual="/Views/Templates/HTML_Temp.htm" -->
// for virtual paths
A better way for Asp.net up to 2.0
A better way would of course be to rename your HTML files to ASCX and create a common CS file that doesn't have any particular functionality. Use this same CS file (class relation) with all you newly created ASCX files.
A better way for Asp.net 2.0 and above
Create a master page (or multitude of them; if you're in 3.0 or higher they can be nested as well) that includes common content and develop other pages on top of your master(s).

Related

namespace of DNN's Language class

I am trying to configure internationalization for my DNN project. I have added two languages, and now I want to access DNN's Language control in order to make it visible on project's pages.
I have found a snippet
<dnn:LANGUAGE runat="server" ID="dnnLANGUAGE" ShowLinks="True" ...
and I tried to use it. Compiler does not recognize dnn: tagPrefix so I have to register it, that is, I have to pass class's namespace inside Register tag in my ascx page. But I can not find the namespace anywhere. I have also browsed www.dnnsoftware.com/dnn-api , but could not find it.
You need to add the following line at the top of your ascx page.
<%# Register TagPrefix="dnn" TagName="LANGUAGE" Src="~/Admin/Skins/Language.ascx" %>

How to pass an object from .cs to .aspx

I am a asp .net beginner. I want to use some objects created at Site.Master.cs in Site.Master. Is there an easy way to do it?
I know how to do it in MVC(by using view(the object)). But how can i do it in normal ASP .net web application?
I don't understand what exactly you want to do.
If you want to insert some string into tag's title you can insert the following thing in SiteMaster.master file:
<img src="<%= Page.ResolveUrl("~/") %>images/logo.png">
instead of:
<img src="images/logo.png">
In the first case there will be calculated the path from the root of your application. In the second case there will be relative link. This is because server will CALCULATE the value of Page.ResolveUrl("~") function and will WRITE it in src tag.
You can do the same thing with any other methods, classes if you defined them properly. But I wouldn't recommend you to implement complicated logic in .aspx files (or .master files). Because you can end up with many difficulties with testing and styling such application.
There are other server tags:
<% %> - an embedded code block is server code that executes during the page's render phase. The code in the block can execute programming statements and call functions in the current page class. Description and examples
<%= %> - most useful for displaying single pieces of information. Description and examples
<%# %> - data binding expression syntax. Description and examples
<%$ %> - ASP.NET Expression. Description and examples
<%# %> - Directive Syntax. Description and examples
<%-- --%> - Server-Side Comments. Description and examples
<%: %> like <%= %> - But HtmlEncodes the output (new with Asp.Net 4). Description and examples
Another way: you can use JSON to send some data to the client and then process it with javascript. Take a look at this project.
If the #Page directive in your .aspx file has Inherits="XYZ" where XYZ is the class declared in your .cs file, you can simply add a protected field to your class and assign a value to it. You'll be able to access it in the .aspx file just by using its name.
You can also use HttpContext.Items property to keep objects during a single request:
HttpContext.Current.Items["SavedItem"] = "hello world";
And use it in page:
<%= ((string)Context.Items["SavedItem"]) %>
Any public or protected property or method in Site.Master.cs will be accessible from Site.Master.
but how to invoke c# code in aspx ?
There are several ways, including the <%= %> construction, and databinding syntax.
It would help if you explained what you're trying to achieve.

How to force update .ascx file content in Website project

I have Website project, which contains some .ascx and .aspx files. I have added new element <asp:TextBox ID="tb1" runat="server" ... /> in .ascx file and I have wrote some code in proper .ascx.cs file using this element: tb1.Text = "SomeText";. When I compile this project I recieve following error: The name 'tb1' does not exist in the current context.
How can I force to refresh markup of .ascx page? I use Website project and I cannot to change its type to Webapplication.
UPD: I have Website project, which has NOT .ascx.designers.cs files. And I cannot change type of my project to web application.
Unless there's something else happening here, it sounds like the designer.cs file might be out of sync. Try cutting and pasting the control back into the markup, or go into the designer file for the user control and add the TextBox manually:
protected global::System.Web.UI.WebControls.TextBox tb1;
It seems like your design file is not connecting with your code behind file.
Can you confirm if you are defining it as follows,
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="yourcontrol.ascx.cs" Inherits="CompleteNameSpace.ClassName" %>

Import HTML page in .NET

The following code imports an HTML page (which simply holds one table) into my .ASP page. This works great, but I now am converting into .NET and am having obstacles.
<% Response.Write(getFilesContent("table.htm")) %>
This code does not work in .NET, and I read that this method is not recommended or widely used? Are there any thoughts, advice, or solutions about this?
I simply want to import this HTML page to read in a content box within my .NET page. In essence, the .NET page is hosting the HTML table.
Any help would be greatly appreciated.
Thank you in advance for your time and help.
To get you started, take a look at the System.Net.WebClient class http://msdn.microsoft.com/en-us/library/system.net.webclient%28v=vs.80%29.aspx
In particular, the "DownloadXXX" methods.
You could put the table in an asp.net user control
A asp.net custom user control can act like an include that encapsulates asp.net
markup.
here is a tutorial:
http://ondotnet.com/pub/a/dotnet/excerpt/progaspdotnet_14/index1.html
After you create the control add a reference
<%#Register tagprefix="uc" Tagname="html" src="custom_html.ascx" %>
then just a the control markup (in this case <uc:html runat="server"/>)
It would be cool the create a control the reads an html file by adding a src property
Try this:
<% Response.Write(New StreamReader(Server.MapPath("~/table.htm")).ReadToEnd()) %>

How can I resolve relative paths in an ASP ListView template?

I use a ListView to display (funnily enough) a list of data, including a hyperlink. Here is my item template (ascx file):
<a href='<%# DataBinder.Eval(CType(Container, ListViewDataItem).DataItem, "ID","/Pages/Image.aspx?id={0}").ToString()%>'
title='View <%# DataBinder.Eval(CType(Container, ListViewDataItem).DataItem, "Title")%>'>
<%# DataBinder.Eval(CType(Container, ListViewDataItem).DataItem, "CardNo")%> -
<%# DataBinder.Eval(CType(Container, ListViewDataItem).DataItem, "Title")%></a>
I'm having problems with the link resolving correctly as this same template is re-used multiple times across the project and fails for pages in a subfolder. I guess I'm looking for a Server.MapPath equivalent so that I could use ~ to get a path relative to root but how do I insert that into my HTML?
Update: The problem is caused by the fact that my project runs in a subfolder of LocalHost root on my machine. In Production the project is in the root itself and this problem doesn't occur.
You're looking for ResolveClientUrl or ResolveUrl.

Resources