简体   繁体   English

从数据库检索HTML数据以创建页面

[英]Retrieving html data from database to create a page

I have stored html file in database. 我已经将html文件存储在数据库中。 Now I would like to get data using cs file and link it to my view page. 现在,我想使用CS文件获取数据并将其链接到我的视图页面。 Below is my example of how I have save my able. 以下是我如何保存能力的示例。

My database table contains two columns ( page_header, page_footer ). 我的数据库表包含两列( page_header,page_footer )。

page_header page_header

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>

page_footer page_footer

<footer>
<table width="100%">
<tr>
<td>
Written by <a href="mailto:webmaster@example.com">Jon Doe</a>.<br>
Visit us at:<br>
Example.com<br>
Box 564, Disneyland<br>
USA
</td>
<td>
<a href="www.google.com">Visit our Site </a>
</td>
</footer>

I want to retrieve those data to my aspx page. 我想将这些数据检索到我的aspx页面。 Can anyone help in doing that. 任何人都可以帮忙。 Or if any demo is available which will be helpful understand how to do that. 或者,如果有可用的演示,将有助于您理解如何进行演示。

If this post is not related please don't degrade. 如果此职位无关,请不要降级。 Just let me know, I will delete it. 请告诉我,我将其删除。

This is how you get user-specific information to your user without having to hand-update every single page you ever serve: 这是您如何向用户获取特定于用户的信息,而不必手动更新您所服务的每个页面:

<!DOCTYPE html>
<html>
<head>
<title runat="server">Page Title</title>
</head>
<body runat="server">
    <div id="welcomeDiv" runat="server"></div>
    <div id="dataDiv" runat="server">
         <datagridview id = "customerData" runat="server">
    </div>
</body>
</html>

then, in your codebehind file: 然后,在您的代码隐藏文件中:

private void Form1_Load(object sender, EventArgs e)
    {
      //load name from db
      Form1.Title = "Welcome back, Mr. "+customer.LastName;
      welcomeDIV.InnerHtml = "<b>It's been "+customer.daysSinceLastVisit.ToString()+" days since you last visited! How are "+customer.wifeName+" and the boys?</b>";
      customerData = loadDataGrid(customer.ID);
    }

It seems that you are creating some kind of a multi-tenant system and need a small amount of customization for each tenant. 看来您正在创建某种多租户系统,并且需要为每个租户进行少量自定义。

There is no reason not to store HTML templates in a database—CMSes such as Wordpress do that a lot. 没有理由不将HTML模板存储在数据库中-诸如Wordpress之类的CMS可以做到很多。 However, to @ShannonHolsinger's point, you should ensure that your database schema is normalized to a reasonable extent. 但是,就@ShannonHolsinger而言,您应确保数据库架构在合理的范围内被规范化 Consider storing e-mail address, contact name, address and website URL as separate fields. 考虑将电子邮件地址,联系人姓名,地址和网站URL存储为单独的字段。

For a templating system, there are many types of choices. 对于模板系统,有多种选择。 You should explore some so you can choose one that is most familiar to you or your needs. 您应该进行一些探索,以便选择最适合自己或您的需求的一种。 In every case, though, be sure that data is property escaped to HTML or you could be allowing your page to be taken over. 但是,在每种情况下,请确保将数据的属性转义为HTML,否则您可以允许您的页面被接管。 If you just paste strings together, such as by using InnerHtml then someone could enter their name as </div><script>ChangeTheEntirePageToWhatEverILike()</script> or </div><script>InjectInSomeCodeToSendFormDataToMySite()</script> and it would be seen by the browser as your code rather than as text. 如果您只是将字符串粘贴在一起(例如使用InnerHtml则有人可以输入</div><script>ChangeTheEntirePageToWhatEverILike()</script></div><script>InjectInSomeCodeToSendFormDataToMySite()</script>将被浏览器视为您的代码而不是文本。

One templating technique could involve client-side data binding. 一种模板技术可能涉及客户端数据绑定。 Some popular libraries are Knockout and Angular 2. For client-side data binding, you could put variable references in trusted header and footer HTML and then pass the variable values to be bound as JavaScript data. 一些流行的库是Knockout和Angular2。对于客户端数据绑定,您可以将变量引用放入受信任的页眉和页脚HTML中,然后将变量值作为JavaScript数据进行绑定。 In other words, let the browser do any data merging that's needed rather than ASP.NET. 换句话说,让浏览器执行所需的任何数据合并,而不是ASP.NET。

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

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