Retrieving html data from database to create a page - c#

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 Jon Doe.<br>
Visit us at:<br>
Example.com<br>
Box 564, Disneyland<br>
USA
</td>
<td>
Visit our Site
</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.

Related

ASP.Net C# Master Site

I have a question about the ****.Master Site a in ASP.NET with C# (WebForms).
I've created a Site.Masters File with a lot of differente Placeholders inside. So the question is - is it possible to fill the Placeholders from different .aspx Files?
.Master Page:
<asp:ContentPlaceHolder ID="MainHeader" runat="server">
</asp:ContentPlaceHolder>
<asp:ContentPlaceHolder ID="MainContent" runat="server">
</asp:ContentPlaceHolder>
<asp:ContentPlaceHolder ID="MainFooter" runat="server">
</asp:ContentPlaceHolder>
</div>
Now i would do something like this:
Adding some Content to the Main Header - the Content is represented in a file called mainHeader.aspx
Adding some Content to the Main Content - the Content is represented in a file calle mainContent.apsx
Is this possible or is there a solution for my plan? I would like to create a modular and flat structure of my .NET Project.
Thanks for your help - best regards
No, that's not how Master Pages work.
When the user/client requests an individual content Page, that Page is merged with the Master Page. The Master Page can not be called by the user/client.
I believe User Controls are what you are looking for.
You can do what you want by using jquery.load(), here is the documentation http://api.jquery.com/load/, however i think what you are doing is not the way to procede, in order to do what you want i suggest yo to do this:
Create User Controls, one for each master ContentPlaceHolder, then in your master, add each one of this and initiate them.

Creating a textbox with a new syntax

In my Asp.NET page I have one html editor.
When user write below part, and click the save button this text is saved in database and gets the id number like (Id=12) and I get it from user interface side of web site with a page with below code.
<html>
<head></head>
<body>
...
..
.
</body>
</html>
I can get the saved text like below sql statement
SELECT Text FROM StackOverFlow WHERE Id = 12
And then I can show the value in web page.
In this respect I want to use this editor to create a asp.net textbox.
That is to say I want to create a new syntax which supply to editor entering basic sentences to create asp.net textbox.
Let's assume that syntax is below:
{{inputbox}}
<html>
<head></head>
<body>
<li>
{{inputbox}}
</li>
</body>
</html>
How can I create an asp.net textbox with using a new syntax like {{inputbox}}?
Can you give any advice to illuminate me?
I'd try looking at how the Razor view engine works. Or any ASP.net view engine.
I use some replace operations in HTML.
<html>
<head></head>
<body>
<li>
{{inputbox}}
</li>
</body>
</html>
I found {{ ...... }} and replace and dynamically create what I want.
You can try to do it using JQuery on client side by replacing {{inputbox}} with Text box.
var htmlStr = $(this).html();
htmlStr.replace('{{inputbox}}', '<asp:TextBox ID="DynamicName" runat="server"></asp:TextBox>');
$(this).html(htmlStr);

C# having control over HTML elements

jQuery and JavaScript can manipulate DOM like anything. But if C# has to send something to the client, then in my knowledge, we can only user Response object to write something in the browser. Now, this would write text in the browser, but we do not have control over where would it write it. Is there anyway that we can control from C#, somehting like :
"This is the text and I want it to be innerHTML of some particular DIV"?
You have two ways to go about doing something like this.
Make your div runat="server" and then it's accessible from the back end to manipulate
Register a JavaScript call using ClientScriptManager.RegisterClientScriptBlock and manipulate the div that way.
But also as Chuck has mentioned in his comment - that's a rather odd thing to do in ASP.NET. For the most part you'd use Label, Literal etc. server controls to add and manipulate text on a page instead of modifying native DOM elements directly.
That's not really how the internet works. When your browser navigates to a webpage, it sends a request to a server. The server responds with something. If it's trying to show a whole webpage, it needs the HTML of the whole page back. C# is powering the webserver.
When you browse to another page, you're not modifying an existing page; you're getting a whole new page back. We use JavaScript to get around that by letting it put out calls to webservers and use the information it gets back to modify the HTML on page, which it does know about, because it's running client-side.
So: no.
You can set the inner html of a div from your code behind. Just ad a runat="server" attribute to the div
<div id="divUserInfo" runat="server"></div>
and in your code behind
string strHtml="<h3> User Name </h3><p>User description</p>";
divUserInfo.innerHtml=strHtml;
This is in VB.NET, but I am sure C# has an equivalent. Just inject some javascript to do what you want.
ClientScript.RegisterStartupScript(Me.GetType, "myScript",
String.Format("<script>document.getElementById('{0}').innerHTML = '{1}';</script>", elementID, html))
.NET provides frameworks to do this - check out WebForms or MVC
From this article on MSDN:
<%# Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
Message.InnerHtml = Server.HtmlEncode("Welcome! You accessed this page at: " + DateTime.Now);
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>HtmlContainerControl Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<span id="Message" runat="server"></span>
</div>
</form>
</body>
</html>

HTML not displaying correctly in outlook 2007 emails?

EDITED:
I have written some correct HTML and passed this as a string into an email,
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>/n<html>
<head>
</head>
<body>
<table>
<tr>
<th>X</th>
<th>Y</th>
</tr>
<tr>
<td>Overall</td>
<td>207,890</td>
</tr>
<tr>
<td>a</td>
<td>100,568</td>
</tr>
<tr>
<td>b</td>
<td>107,322</td>
</tr>
</table>
</body>
</html>
I re-wrote the HTML to be extremely simple, only using a table but its still not showing??
Generally email clients don't seem to like decently formatted HTML. Just from conversation I've had with HTML developers
Use inline styles even if that means repeating yourself. No style sheets even in head
No fancy floating of the divs
Put everything in tables for formatting
Generally pretend like it's 1999
Your problem is probably not only Outlook 2007 but most other email clients as well.
Make sure that your html is very simple and does not use many external resources, inline CSS is probably necessary. This article is a nice summary: http://css-tricks.com/using-css-in-html-emails-the-real-story/

How to dynamically add rows to a table in ASP.NET?

So today I started learning ASP.NET. Unfortunately I haven't found any good tutorials online, and I can't afford to buy books at the moment, so I've had to create a ASP.NET web application in Visual Studio 2010 and just play around with the default project setup.
So far here's what I have in my Default.aspx:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Project Management</title>
</head>
<body>
<div style="padding-bottom:10px;"> Project Management System</div>
<div> <table style="width:100%;">
<tr>
<td>Name</td>
<td>Task</td>
<td>Hours</td>
</tr>
</table></div>
</body>
</html>
I created a simple table with the header row already in there. Through a C# script, I want to be able to dynamically add rows to this HTML table. Is this the right way of thinking in ASP.NET? If so, how can I do this? I'm sure I'll need an "Add" button, which adds a new row to the table, with editable fields, and a "submit" button which adds some stuff to a database.
Basically just a rundown of how this is done would be ever so helpful.
Have you attempted the Asp:Table?
<asp:Table ID="myTable" runat="server" Width="100%">
<asp:TableRow>
<asp:TableCell>Name</asp:TableCell>
<asp:TableCell>Task</asp:TableCell>
<asp:TableCell>Hours</asp:TableCell>
</asp:TableRow>
</asp:Table>
You can then add rows as you need to in the script by creating them and adding them to myTable.Rows
TableRow row = new TableRow();
TableCell cell1 = new TableCell();
cell1.Text = "blah blah blah";
row.Cells.Add(cell1);
myTable.Rows.Add(row);
Given your question description though, I'd say you'd be better off using a GridView or Repeater as mentioned by #Kirk Woll.
EDIT - Also, if you want to learn without buying books here are a few sites you absolutely need to become familiar with:
Scott Guthrie's Blog
4 Guys from Rolla
MSDN
Code Project Asp.Net
in addition to what Kirk said I want to tell you that just "playing around" won't help you to learn asp.net, and there is a lot of free and very good tutorials .
take a look on the asp.net official site tutorials and on 4GuysFromRolla site
ASP.NET WebForms doesn't work this way. What you have above is just normal HTML, so ASP.NET isn't going to give you any facility to add/remove items. What you'll want to do is use a Repeater control, or possibly a GridView. These controls will be available in the code-behind. For example, the Repeater would expose an "Items" property upon which you can add new items (rows). In the code-front (the .aspx file) you'd provide an ItemTemplate that stubs out what the body rows would look like. There are plenty of tutorials on the web for repeaters, so I suggest you google that to obtain further information.
public partial class result : System.Web.UI.Page
{
static DataTable table1 = new DataTable("Shashank");
static DataSet set = new DataSet("office");
protected void Page_Load(object sender, EventArgs e)
{
lblEmployeeNumber.Text = HttpContext.Current.Request.Form["txtEmployeeNumber"];
lblFirstName.Text = Request.Form["txtFirstName"];
lblLastName.Text = Request.Form["txtLastName"];
lblTitle.Text = Request.Form["txtTitle"];
Int32 Rcount = Convert.ToInt32(table1.Rows.Count);
if (Rcount == 0)
{
table1.Columns.Add("ID");
table1.Columns.Add("FName");
table1.Columns.Add("LName");
table1.Columns.Add("Title");
table1.Rows.Add(lblEmployeeNumber.Text, lblFirstName.Text, lblLastName.Text, lblTitle.Text);
set.Tables.Add(table1);
}
else
{
if (lblEmployeeNumber.Text != "")
{
DataRow dr = table1.NewRow();
dr["ID"] = lblEmployeeNumber.Text;
dr["FName"] = lblFirstName.Text;
dr["LName"] = lblLastName.Text;
dr["Title"] = lblTitle.Text;
table1.Rows.Add(dr);
}
}
gvrEmp.DataSource = set;
gvrEmp.DataBind();
}
}
You need to get familiar with the idea of "Server side" vs. "Client side" code. It's been a long time since I had to start, but you may want to start with some of the video tutorials at http://www.asp.net.
Two things to note: if you're using VS2010 you actually have two different frameworks to chose from for ASP.NET: WebForms and ASP.NET MVC2. WebForms is the old legacy way, MVC2 is being positioned by MS as an alternative not a replacement for WebForms, but we'll see how the community handles it over the next couple of years. Anyway, be sure to pay attention to which one a given tutorial is talking about.
You can use the asp:Table in your web form and build it via code:
http://msdn.microsoft.com/en-us/library/7bewx260.aspx
Also, check out asp.net for tutorials and such.
Dynamically Created for a Row in a Table
See below the Link
http://msdn.microsoft.com/en-us/library/7bewx260(v=vs.100).aspx
Link for adding through JS
https://www.youtube.com/watch?v=idyyQ23joy0
Please see the below link as well. This would help you add the rows dynamically on the fly:
https://www.lynda.com/C-tutorials/Adding-data-HTML-tables-runtime/161815/366843-4.html
<html>
<head>
<title>Row Click</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
function test(){
alert('test');
}
$(document).ready(function(){
var row='<tr onclick="test()"><td >Value 4</td><td>Value 5</td><td>Value 6</td></tr>';
$("#myTable").append(row);
});
</script>
</head>
<table id="myTable" >
<th>Column 1</th><th>Column 2</th><th>Column 3</th>
<tr onclick="test()">
<td >Value 1</td>
<td>Value 2</td>
<td>Value 3</td>
</tr>
</table>
</html>
You need to use JavaScript in your HTML and make sure you are using forms so that. You may finally serialize the data using Ajax method to push the data from HTML into database

Categories

Resources