C# 如何使用数据库中的标记生成asp.net控件

C# 如何使用数据库中的标记生成asp.net控件,c#,asp.net,sql,ado.net,C#,Asp.net,Sql,Ado.net,如何基于SQL Server数据库中定义的标记在div中生成控件?可能吗?如果是的话,怎么办?谁能给我资源 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> &

如何基于SQL Server数据库中定义的标记在div中生成控件?可能吗?如果是的话,怎么办?谁能给我资源

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Literal ID="lit" runat="server" ClientIDMode="Static" Mode="Transform"></asp:Literal>
    </div>
    </form>
</body>
</html>

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication2
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection _newConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

            SqlCommand storedProcedure = new SqlCommand("sp_getMarkup", _newConnection);
            storedProcedure.CommandType = CommandType.StoredProcedure;

            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter(storedProcedure);

            _newConnection.Open();
            da.Fill(ds);
            _newConnection.Close();

            DataTable dt = ds.Tables["Table"];
            string s = (from str in dt.AsEnumerable()
                       where str.Field<int>("Id").Equals(1)
                       select str.Field<string>("elemMarkup")).SingleOrDefault().ToString();

            this.lit.Text = s;
        }
    }
}

使用制度;
使用System.Collections.Generic;
使用系统配置;
使用系统数据;
使用System.Data.SqlClient;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
命名空间WebApplication2
{
公共部分类WebForm1:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
SqlConnection _newConnection=新的SqlConnection(ConfigurationManager.ConnectionString[“ConnectionString”].ConnectionString);
SqlCommand storedProcedure=newSQLCommand(“sp_getMarkup”,_newConnection);
storedProcedure.CommandType=CommandType.storedProcedure;
数据集ds=新数据集();
SqlDataAdapter da=新的SqlDataAdapter(StoredProcess);
_newConnection.Open();
da.填充(ds);
_newConnection.Close();
DataTable dt=ds.Tables[“Table”];
字符串s=(来自dt.AsEnumerable()中的str)
其中str.Field(“Id”)等于(1)
选择str.Field(“elemMarkup”).SingleOrDefault().ToString();
this.lit.Text=s;
}
}
}
在数据库中,我将字符串存储为

<asp:CheckBox ID="chk" runat="server" ClientIDMode="Static" />

现在的问题是控件在页面上呈现,但不可见。我可以在view source中看到它


有人能帮我吗?

这里还有一个问题


我建议使用某种类型的
消毒
中介;否则,您将面临跨站点脚本(XSS)问题。

您可以在这样的页面上使用占位符:

<body>
    <form id="form1" runat="server">
    <div>
        <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
    </div>
    </form>
</body>
或者,如果坚持将asp标记存储在数据库中,则可以使用循环(如果数据库中有多行)将asp标记输入占位符中。

您可以使用循环接受字符串并动态创建控件

缺点是如果服务器代码在字符串中,则不会执行。此外,您需要手动附加事件,例如按钮单击事件

比如说,

<script runat="server">
    // This server code will not be executed 
</script>

//将不执行此服务器代码

只要你努力,几乎一切都有可能。你试过了吗?如果是,请向我们展示代码。看:@MichaelBray我不知道该怎么做。如果你能给我指个方向,对我会有帮助的。谢谢你的鼓励,这个标记是包含ASP.NET控件,还是只包含HTML?ASP.NET控件。。。为什么?有什么区别吗?有。HTML可以直接在页面上抛出,而ASP.NET控件需要通过编程方式添加到控件树中。我想生成asp.net控件,如标签或复选框,其标记在数据库中定义。
<script runat="server">
    // This server code will not be executed 
</script>