简体   繁体   English

如何在数据库中使用标记生成ASP.NET控件

[英]How to generate asp.net control using markup in database

How do I generate the controls in a div based on the markup defined in a SQL Server database? 如何基于SQL Server数据库中定义的标记在div中生成控件? 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 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;
        }
    }
}

In the database I have stored string as 在数据库中,我将字符串存储为

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

Now the problem is the control is rendered on page but is not visible. 现在的问题是控件在页面上呈现,但不可见。 I can see it in view source. 我可以在查看源代码中看到它。

Can anyone help me? 谁能帮我?

You can use ParseControl which accepts a string and create controls on the fly. 您可以使用ParseControl接受一个字符串并动态创建控件。

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. 此外,您需要手动附加事件,例如按钮单击事件。

For example, 例如,

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

Here is another SO question using jquery ajax to load info from database 这是另一个使用jquery ajax从数据库加载信息的问题

I would recommend having some type of sanitizing intermediary; 我建议您使用某种类型的sanitizing otherwise you are opening yourself up to cross-site scripting (XSS) issues. 否则,您将面临跨站点脚本(XSS)问题。

You could use a placeholder on a page like so: 您可以在类似这样的页面上使用占位符:

<body>
    <form id="form1" runat="server">
    <div>
        <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
    </div>
    </form>
</body>

And in your code behind, you could have several checks (all of which contain SQL statements querying the data in the database). 在后面的代码中,您可能需要进行多项检查(所有检查都包含查询数据库中数据的SQL语句)。 The database could contain a field containing component names such as checkbox, button (various other controls). 数据库可以包含一个包含组件名称的字段,例如复选框,按钮(各种其他控件)。 This saves you storing markup in the database fields.. 这可以节省您将标记存储在数据库字段中。

Very rough representation of how the code would look like.. 非常粗略地表示代码的外观。

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
    // SQL Code here

    // Datatable code here 
    // End of DataTable code

    // Beginning of IF blocks   
        If (DataTable.Rows.Count > 0) {
        If (your check != "checkbox") {
        this.PlaceHolder1.Controls.Add(new Button(){
            Text = "Added"}
            }
          }
     // More if statements here
       );

    }
}

Alternatively if you insist on storing asp markup in the database, then you could just feed the asp markup into the placeholder using a loop (if there are more than one rows in your database) 另外,如果您坚持要在数据库中存储asp标记,则可以使用循环将asp标记馈入占位符(如果数据库中有多行)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM