将HTML字符串转换为ASP.Net中的服务器控件 [英] Convert a html string to server control in ASP.Net

查看:60
本文介绍了将HTML字符串转换为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\" />";

那么如何将其插入到aspx页面中,以及如何在页面呈现时将其转换为我在网页中自己编写的字符串

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

希望你们能帮忙

提前谢谢!

首先让我说这就是为什么必须使用这种方式的原因,因为我正在做自己的模板项目我有一个HTML文件,例如index.html,里面有一些这样的html代码

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>

所有人都可以看到我自己的标记({main_menu},{footer}),我只想在页面呈现时将Web用户控件替换为该标记,仅此而已!

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?

推荐答案

控件的服务器端标记不仅是文本-视觉sutdio会生成具有正确类型的字段并在后面的代码中设置不同的属性.

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.

如果要动态添加控件,则需要在后面的代码中创建它,并将其添加到代码中的页面中(请参阅GenericTypeTea的答案).

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).

您还需要记住页面生命周期,因为您将需要在每个回发中重新创建控件(最好在OnInit事件处理程序中完成).

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).

从您的编辑中,我了解您要完成的工作,但这是一个非常难题.

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?

这篇关于将HTML字符串转换为ASP.Net中的服务器控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆