Hieu Nguyen Trung
Hieu Nguyen Trung

Reputation: 1622

Convert a html string to server control in ASP.Net

i have a problem so hope you guys can help! I have a string in code behind like this

string html = "<asp:CheckBox ID=\"CheckBox1\" runat=\"server\" />";

So how to insert it into aspx page and when the page is rendering, it convert my string as i write it own in the webpage

Hope you guys can help

Thanks in advance!

Let me say first that's why I must use this way, because I'm doing my own template project I have a HTML file like index.html and inside there's some html code like this

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
      <title>
        {title_page}
      </title>
    <link href="css/temp_css.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="somejs.js"></script>
    <script type=”text/javascript”>
        //code js here
    </script>
   </head>
   <body>
    <div>**{main_menu}**</div>
    <div>**{footer}**</div>
   </body>
</html>

All you guys can see my own markup ({main_menu}, {footer}), i just want to replace my Web User Control to that markup when the page is rendering, that's all!

Is there any idea to let me out of this way?

Upvotes: 2

Views: 7950

Answers (7)

Adil
Adil

Reputation: 11

Use Literals:

<code>string html = "<asp:CheckBox ID=\"CheckBox1\" runat=\"server\" />";
Literal lt = new Literal();
lt.Text = html;
ServerDIV.Controls.Add(lt);
</code>

Upvotes: 1

M4N
M4N

Reputation: 96606

You can't use a string as you have shown. Instead you have to instantiate and add a CheckBox control, e.g. something like this:

ASPX:

<asp:Panel id="panel1" runat="server" ...>

code-behind:

    var cb = new CheckBox();
    cb.ID = "CheckBox1";
    cb.Text = "text ...";
    //.. set properties of checkbox
    panel1.Controls.Add(cb);

Update: after your edit it seems that you might want to have a look at master pages, i.e. turn your HTML template into a master page, and put the real content onto a page which uses that master page.

Upvotes: 0

citronas
citronas

Reputation: 19365

For my CMS I made something similar. See this questions: How to dynamically render asp.net controls from string?

Upvotes: 0

Oded
Oded

Reputation: 499352

The server side markup for controls is not just text - visual sutdio generates a field with the right type and sets the different attributes in the code behind.

This, of course, does not happen with a simple string, as in your code example.

If you want to add a control dynamically, you need to create it in your code behind and add it to the page in code (see the answer from GenericTypeTea).

You also need to keep in mind the page life cycle, as you will need to recreate the control on every postback (best done in the OnInit event handler).

Edit:

From your edit, I understand what you are trying to accomplish, however, this is a very difficult problem.

You need to dynamically parse and compile the webpage into controls, change all the textual content to controls and handle all postbacks etc.

Why can't you simply use master pages and controls?

Upvotes: 0

brumScouse
brumScouse

Reputation: 3226

You want to use

Template.ParseControl

http://msdn.microsoft.com/en-us/library/kz3ffe28.aspx

Upvotes: 0

Jagmag
Jagmag

Reputation: 10366

In general, i agree with most of the comments above and would recommend you look at your design and if you really want to try and do things this way.

Having said that, your question made me think of why not use the Render event?

When the Render event is overridden, the developer can write custom HTML to the browser The Render method takes an HtmlTextWriter object as a parameter and uses that to output HTML to be streamed to the browser. Changes can still be made at this point, but they are reflected to the client only.

Something on the lines of :-

protected override void Render(HtmlTextWriter output) 
{
    output.Write ("<h3> Hello </h3>");
}

If you put in your HTML string here, i presume it should work.

Note 1: Because this change is reflected on the client only, i seriously doubt how you could do anything useful with it even if you did end up with atleast the client displaying your controls

Note 2: That i have never tried to do something like this but your question just got me thinking!!

Upvotes: 0

djdd87
djdd87

Reputation: 68506

I've never seen this done before, so I'm not even sure it's possible. However it's certainly bad practise. Why can you not just add the control yourself without using a string?

protected void Page_Init(object sender, EventArgs e)
{
    CheckBox checkBox = new CheckBox();
    checkBox.ID = "CheckBox1";
    Page.Controls.Add(checkBox); // Replace Page with any other Control to add to
}

Upvotes: 2

Related Questions