views:

72

answers:

6

Hi all, 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"&gt;
<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?

A: 

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.

M4N
Ths for help! can you plz check the question again, i just edited and change some words
Chrno Love
+2  A: 

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
}
GenericTypeTea
Ths for help! can you plz check the question again, i just edited and change some words
Chrno Love
@Chrno Love - It's impossible to write out a string and expect the server to know it exists. You could do if it you weren't using `runat="server"` by making use of the `Render` event.
GenericTypeTea
A: 

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?

Oded
Ths for help! can you plz check the question again, i just edited and change some words
Chrno Love
Ths 4 your re-answer, appreciate so much! I did have an idea to use master pages, but i thought use that way, the designers is difficult to make template so i want to make it simply by making some markup so the designers can easy do their way! Anyway ths so much ^^!
Chrno Love
A: 

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!!

InSane
Ths for help! can you plz check the question again, i just edited and change some words
Chrno Love
A: 

You want to use

Template.ParseControl

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

brumScouse
Ths for help! can you plz check the question again, i just edited and change some words
Chrno Love
A: 

For my CMS I made something similar. See this questions: http://stackoverflow.com/questions/2351225/how-to-dynamically-render-asp-net-controls-from-string

citronas