简体   繁体   中英

Retrieving html data from database to create a page

I have stored html file in database. Now I would like to get data using cs file and link it to my view page. Below is my example of how I have save my able.

My database table contains two columns ( page_header, page_footer ).

page_header

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

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. 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. However, to @ShannonHolsinger's point, you should ensure that your database schema is normalized to a reasonable extent. Consider storing e-mail address, contact name, address and website URL as separate fields.

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. 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.

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. In other words, let the browser do any data merging that's needed rather than ASP.NET.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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