C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !






Get Current page’s Html with c#


Posted Date: 01 Aug 2008    Resource Type: Code Snippets    Category: ASP.NET WebForms

Posted By: Nitin Kumar Sharma       Member Level: Silver
Rating:     Points: 10



Today my friend was working on a project where He needed to get the current page’s HTML. He searched all over the Internet and could not find anything. Then I thought about the Page’s built-in methods…Render! Here’s how you get it:


strWrt = new StringWriter();

htmText = new HtmlTextWriter(strWrt);

base.Render(htmText);

StringBuilder html = strWrt.GetStringBuilder();


And be sure to drop this code in the “OnPreRenderComplete” method (you’ll need to create an override in your code-behind).

Well that was easy enough!

Even you can render a portion of the page say you have a panel on which you display the profile of the Employee and you want to render this panel and also wants to save this page on disk so that you can send this url to pdf creater website to genearate pdf on fly. To do this you have change the code a little bit.

Here is the code sample :


try

{

System.Text.StringBuilder strBuilder = new System.Text.StringBuilder();

System.IO.StringWriter strWrt = new System.IO.StringWriter(strBuilder);

HtmlTextWriter htmText = new HtmlTextWriter(strWrt);

//pnlMain is the Panel ID

pnlMain.RenderControl(htmText);

System.IO.StreamWriter strHtmlFile = System.IO.File.CreateText(Server.MapPath(“~/ValuationReport/”)+“Property-valuation-” + Session.SessionID+“.htm”);

strHtmlFile.WriteLine(strBuilder.ToString());

strHtmlFile.Close();

Response.Redirect(“Payment1.aspx”);

}

catch (Exception exp) { Response.Write(exp.Message.ToString()); }



For more details, visit http://kaniks.blog.co.in




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Read web page's html code using c#  .  Read web page's html code from c#  .  Read web page's html code  .  Obtain web page's html code  .  Get web page's html code  .  Get CUrrent page's html using C#  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Save and retrive the value in ViewState object before the PostBack
Previous Resource: InterSoft WebGrid : Dynamic Unbound Hierarchical Grid
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET WebForms


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

doors in nj

Contact Us    Privacy Policy    Terms Of Use