如何在数据库中使用的标记生成asp.net控制

问题描述:

我如何生成基于数据库(SQL Server)的定义的标记在一个div的控制?可能吗?如果是,那么如何?谁能给我的资源?

How do I generate the controls in a div based on the markup defined in database(SQL Server)? Is it possible? If yes, then how? can anyone give me resources?

<%@ 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 storeProc = new SqlCommand("sp_getMarkup", _newConnection);
            storeProc.CommandType = CommandType.StoredProcedure;

            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter(storeProc);            
            _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;
        }
    }
}

在数据库中我已存储的字符串作为&LT; ASP:复选框ID =CHKRUNAT =服务器的ClientIDMode =静态/&GT;

现在的问题是控制被呈现的页面上,但不可见。我可以看到它在查看源代码。

Now the problem is the control is rendered on page but is not visible. I can see it in view source.

谁能帮我?

您可以使用的 ParseControl 它接受一个字符串,动态创建控件。

You can use ParseControl which accepts a string and create controls on the fly.

的缺点是,如果服务器code是字符串中,也不会被执行。此外,你需要附加的事件如手动按钮单击事件。

The drawback is if server code is in the string, it will not be executed. In addition, you need to attach event manually such as button click events.

例如,

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