2

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?

1
  • I'm sorry, I don't quite under why you don't just add this control to the controls collection in the code-behind? Commented Oct 1, 2010 at 9:54

6 Answers 6

2

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
}
Sign up to request clarification or add additional context in comments.

2 Comments

Ths for help! can you plz check the question again, i just edited and change some words
@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.
1

Use Literals:

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

Comments

0

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?

Comments

0

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

1 Comment

Ths for help! can you plz check the question again, i just edited and change some words
0

You want to use

Template.ParseControl

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

1 Comment

Ths for help! can you plz check the question again, i just edited and change some words
0

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.

1 Comment

Ths for help! can you plz check the question again, i just edited and change some words

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.