【问题标题】:Execute html from database in ASP.NET page从 ASP.NET 页面中的数据库执行 html
【发布时间】:2011-11-17 23:52:04
【问题描述】:

对加载保存在数据库中的 html 页面有什么建议吗?

html:

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>xxx</title>

</head>
<body>
    <form id="Form1" runat="server" method="post">
        <ext:ResourceManager ID="ResourceManager1" runat="server" />            
    </form>
</body>

代码隐藏:

 protected override void OnInit(EventArgs e)
    {
        this.IniciarFormulario();

        using (ServicoECMClient proxy = new ServicoECMClient())
        {
            tipoDocumento = proxy.ObterTipoDocumento(int.Parse(tipoDocumentoID));
        }

        if (tipoDocumento == null)
        {
            throw new ApplicationException();
        }

        this.Page.Header.InnerHtml = tipoDocumento.estilo; //css

        this.Page.Form.InnerHtml = tipoDocumento.form; // form 

        base.OnInit(e);
    }

我无法检索表单值。

看:

 foreach (System.Web.UI.Control controle in this.Form1.Controls)
            {
                if (controle.GetType().Name == "HtmlInputText" || controle.GetType().Name == "HtmlInputSelect"
                    || controle.GetType().Name == "HtmlInputRadio" || controle.GetType().Name == "HtmlInputTextCheckbox")
                {
                    if (!string.IsNullOrEmpty(this.Request[controle.ClientID]))
                    {
                        documento_indice documentoIndice = new documento_indice();
                        documentoIndice.id_indice = int.Parse(controle.ClientID.Split('_')[1]);
                        documentoIndice.valor = this.Request[controle.ClientID];
                        documentoIndice.timestamp = DateTime.Now;
                        documentos_indices.Add(documentoIndice);
                    }
                }
            }

控件为空。 => this.Form1.Controls

有什么建议吗?

还有其他更好的方法吗?

谢谢。

【问题讨论】:

  • 这是一个html页面。开头没有任何 Aspx 标记表明这是从后面的代码派生的,也不表明该页面甚至被编译;因此,它永远不会出现在后面的代码中。 HTML 是否完整?
  • 如果您只想从数据库中渲染出原始 html,只需使用处理程序 (ashx) 文件和 Response.Write() 输出 HTML。
  • 查看 VirtualPathProvider msdn.microsoft.com/en-us/library/…
  • 大家好,@kurtnelle 这是我的 aspx 页面。我在这里完成(this.Page.Header.InnerHtml = tipoDocumento.estilo; //css this.Page.Form.InnerHtml = tipoDocumento.form; // form )不要把所有的实现都放在这里。但我可以访问代码隐藏。

标签: c# asp.net html controls dynamic-html


【解决方案1】:

简短的回答是,是的,您可以使用此功能。我们以与此类似的方式提供所有客户特定的定制。

答案很长,需要对您的应用程序和 HTML 进行一些重组。

我们发现实现这一点的最简单方法是通过 UserControls。基本做法是:

1) 创建作为 UserControl 存储在 DB 中的页面内容,即

<%@ Control Language="vb" AutoEventWireup="false" %>
<input id="txtTest" type="text" runat="server" />

2) 当您从数据库中提取它时,将其存储在磁盘上带有 ascx 扩展名的文件中(现在说 content.ascx)。

3) 修改您的主页以添加一个在将加载 ascx 的服务器上运行的 div:

<div id="divContent" runat="server">
</div>

4)在页面初始化中,将控件加载到div中并初始化:

Dim oControl As Control

' Load a user control
oControl = Me.LoadControl("content.ascx")
If oControl IsNot Nothing Then
    ' Ensure viewstate is enabled
    oControl.EnableViewState = True
    ' Set properties as required
    oControl.ID = "ContentControl"
    ' Clear the existing control(s) from the content container
    Me.divContent.Controls.Clear()
    ' And add the new one
    Me.divContent.Controls.Add(oControl)
End If

5) 访问 div 控件中包含的控件集合。

请注意,在页面回发中,由于标准页面生命周期,在触发页面加载事件之前,控件不会将内容加载到其中。

我已验证不需要代码隐藏,并且完全按照上述方式正常工作。

【讨论】:

  • 会这样做。稍后我会发布结果。谢谢!!
【解决方案2】:

除非您手动将控件添加到控件集合中(这是一项棘手的工作),否则您将需要以老式方式读取表单值:

Request.Form["txtSomething"]

这意味着如果您知道控件的名称,则可以获取控件中包含的字符串值,但除此之外就没什么了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-29
    • 1970-01-01
    • 2020-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-22
    • 2012-11-17
    相关资源
    最近更新 更多