开发者

Dynamically create web page in asp.net using c#

开发者 https://www.devze.com 2023-02-24 01:24 出处:网络
I have 开发者_高级运维ASP.NET web page (mypage.aspx) which has a TextBox (multiline) and a Button.

I have 开发者_高级运维ASP.NET web page (mypage.aspx) which has a TextBox (multiline) and a Button.

Problem: I want to add html content into textbox and then click the button, it should generate the exact web page according to my html.

Example:

<html>
<head><title></title></head>
<body>
<h1>Hello</h1>
</body>
</html>

The generated web page should contain Hello.

Any idea..?

Thank in advance.


Take a look in this answer I gave some while ago, in your case have body be txtHTML.Text to take the HTML given by the user.

To make it work you'll have to follow those steps:

  1. Add this under system.web in the web.config file:
    <httpRuntime requestValidationMode="2.0" />
  2. Add this to the Page directive in the .aspx file:
    validateRequest="false"

Otherwise you won't be able to send raw HTML contents.


Use a html editor like tincymce instead of a textbox. Save the content of the html editor in a database and call it with another page. Or post the content on button press and display it on postback (and hide the textbox + button)


This sounds like quite a simple HelloWorld type app? Though you'll need to protect against injections if this is a public app (maybe HTMLEncoding and decoding). But Basically (I'm assuming webforms for the textbox, but it can easily be changed for MVC):

This would go in your page:

In your codebehind (just in the Page_Load method): string content = HtmlContent.Text;

You can then do what you need to with the content variable. If you are just outputting to the same page add:

to your page

and change your codebehind from: string content = HtmlContent.Text;

to Output.Text = HtmlContent.Text;

0

精彩评论

暂无评论...
验证码 换一张
取 消