Click here to Skip to main content
15,896,727 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
Hi,

I have 100's of HTML template(Basically HTML file from 3rd party with header, footer and body with many branding elements and a div called "container") and this is stored in a physical location in a Some server.

My requirement is to render this template by finding the div "container" inside the template and inject my form content into this div.

Sample template.

<html>
<head>
//logo and some content
</head>
<Body>
<div id ="MainContent">
</div>
<div id ="MyContent">
</div>
<div id ="Footer">
</Body>
<html>

And below is my form

 <form name="form1" method="post" action="1.aspx" id="form1"  autocomplete="off">
<div>
<input type="sumbit" value="submit">
</div>

This form should go inside the div MyContent and render.

I don't have control on template and it will not be part of my solution and it is dynamic it can change any time, but always have div called MyContent

Thanks,
Posted
Updated 17-Oct-13 23:33pm
v2

1 solution

Method 1

You can read html as string by using normal File.ReadAllText Method
C#
public static string GetHtml(string path)
        {
            string html = File.ReadAllText(path);
            return html;
        }


and then Create an ActionResult for returning the html as json string like below
C#
[HttpGet]
public ActionResult LoadHtml(int id)
{
var html=GetHtml("you html file path");
var result = new { Value = html };
return Json(result, JsonRequestBehavior.AllowGet);
}


Call the action by using jQuery from the client and insert the html in to a container you want
JavaScript
jQuery(document).ready(function(){
$("#buttonClick").click(function() {
var ServiceUrl ="/YourController/LoadHtml;
var content = '';
$.support.cors = true;
$.ajax({
type: 'GET',
url: ServiceUrl,
async: true,
cache: false,
crossDomain: true,
contentType: "application/json; charset=utf-8",
dataType: 'json',
error: function (xhr, err) {
},
success: function (result, status) {
$('#yourdivId').html(result.Value);
}
});
});
});


Method2
By using Render js also you can load template in to html
for more http://www.jsviews.com/#jsrender[^]
Hope this helps
 
Share this answer
 
v2
Comments
charan49 10-Sep-13 6:32am    
Hi Jameel,

Thanks for your reply.

I don't want to insert template content(HTML) to a div in my Page.(As it is done above)

I want to insert my form content to a template div and render that.

Thanks,
charan49 12-Sep-13 6:16am    
Anybody has any suggestion for this?
Jameel VM 12-Sep-13 6:22am    
did you try Jsrender?
charan49 18-Oct-13 5:25am    
Hi Jameel, Jsrender also does the same thing.. Someone will push the Template(HTML page) with a div id =container into my server.. and i need to take that template and put my FORM content into this div and render.. So i dont have any control over the template..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900