从数据库动态加载asp.net内容

Dynamically load asp.net Content from database
2020-10-18
  •  译文(汉语)
  •  原文(英语)

我的解决方案由一个页面"Content.aspx"组成,我使用它根据查询字符串填充不同类型的html数据.

我需要知道,如果可能的话,如何在页面中加载aspx内容.我试过添加如下代码:

<asp:Label ID="lblName" runat="server"></asp:Label>

但是它显示为纯文本.这是我用来从数据库检索数据的代码.

string category = Request.QueryString["category"].ToString();
using (MyEntity dbc = new MyEntity())
{
    ContentTables cms = (from c in dbc.ContentTable
                         where c.Name == category && c.Status == true
                         select c).First();
    divHeader.InnerHtml = cms.Header;
    divContent.InnerHtml = cms.Content;
    if (cms.Header == "dataPage")
    {
       /*Code Requirement Here*/
    }
}

任何帮助将不胜感激.谢谢

速聊1:
您想显示DB包含页面的源代码吗?您需要对其进行html编码并包装在HTML <pre>或<code>标记中.
解决过程1

使用StringBuilderLiteral

StringBuilder sb = new StringBuilder();
sb.append("Your Contain From Database");
Literal1.Text = sb.ToString(); //Bind Contain to Litral

StringBuilder AppendFormat方法对于基于模式生成文本很有用.

My solution consists out of a single page, "Content.aspx" which I use to populate with different types of html data depending on the querystring.

I need to know, if possible, how to load aspx content in my page. I've tried adding code like :

<asp:Label ID="lblName" runat="server"></asp:Label>

But it gets displayed as plain text. Here is my code that I use to retrieve the data from the database.

string category = Request.QueryString["category"].ToString();
using (MyEntity dbc = new MyEntity())
{
    ContentTables cms = (from c in dbc.ContentTable
                         where c.Name == category && c.Status == true
                         select c).First();
    divHeader.InnerHtml = cms.Header;
    divContent.InnerHtml = cms.Content;
    if (cms.Header == "dataPage")
    {
       /*Code Requirement Here*/
    }
}

Any help will be greatly appreciated. Thanks

Talk1:
You want to show DB conatined page source? You need to html encode it and wrap in HTML <pre> or <code> tags.
Solutions1

Use StringBuilder and Literal

StringBuilder sb = new StringBuilder();
sb.append("Your Contain From Database");
Literal1.Text = sb.ToString(); //Bind Contain to Litral

The StringBuilder AppendFormat method is useful for generating text based on a pattern.

转载于:https://stackoverflow.com/questions/30773486/dynamically-load-asp-net-content-from-database

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入