2

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;
        }
    }
}

データベースに文字列を次のように保存しました

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

ここでの問題は、コントロールがページにレンダリングされますが、表示されないことです。ビューソースで見ることができます。

誰でも私を助けることができますか?

4

3 に答える 3

1

文字列を受け取り、オンザフライでコントロールを作成するParseControlを使用できます。

欠点は、サーバー コードが文字列内にある場合、実行されないことです。また、ボタン クリック イベントなどのイベントを手動でアタッチする必要があります。

例えば、

<script runat="server">
    // This server code will not be executed 
</script>
于 2013-07-09T19:00:43.440 に答える
-1

次のように、ページでプレースホルダーを使用できます。

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

コード ビハインドでは、いくつかのチェックを行うことができます (すべてのチェックには、データベース内のデータをクエリする SQL ステートメントが含まれます)。データベースには、チェックボックス、ボタン (その他のさまざまなコントロール) などのコンポーネント名を含むフィールドを含めることができます。これにより、データベース フィールドにマークアップを保存する手間が省けます。

コードがどのように見えるかの非常に大まかな表現..

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

    }
}

または、データベースに asp マークアップを保存することを主張する場合は、ループを使用して asp マークアップをプレースホルダーにフィードすることができます (データベースに複数の行がある場合)。

于 2013-07-09T18:51:38.753 に答える