Howto:HTML template which needs to be filled with data from a data

G

Guest

Hello. I recently built an ASP.NET "financial" application. I got to the
point where I have a HTML template (a contract) which needs to be filled with
various data from the database. I put a "generate contract" button on my ASPX
page, but I totally lack ideas of how to put the data in the correct places
in the html file. I was recommended (actually "do like this" from my boss) to
enclose the variable name like this {variable_name} in the html, and he said
that there are some functions to assign the value to the variable but how???
So the question is: how can I place the data that I gather from the database
inside the html file using only asp.net, without any report generation tool
(click the button and there you go: window with the contract filled)? A
little more elaborated example would be highly appreciated as this is my
first ASP.NET app.
 
G

Guest

Hi Vlady,

Can you insert label controls where the data needs to go? That way you can
pick up the data and insert it into the labels, including calculations.
There's a quick example below. Let us know if it helps?

Ken
Microsoft MVP [ASP.NET]
Toronto

<%@ Page Language="VB" %>
<script runat="server">

' Insert page code here
'
Sub Page_Load
' initialize here
if not ispostback then
label1.text= "no"
label2.text= "0"
end if
End sub

Sub Button1_Click(sender As Object, e As EventArgs)
if textbox1.text <> "" and isnumeric(textbox1.text) then
' Get your values from the database
label1.text= textbox1.text
Label2.text=(3.25 * cdec(textbox1.text)).tostring("C")
else
label1.text= "no"
label2.text= "0"
end if
End Sub

</script>
<html>
<head>
</head>
<body>
<form runat="server">
<p>
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
</p>
<p>
<asp:Button id="Button1" onclick="Button1_Click" runat="server"
Text="Order"></asp:Button>
</p>
<p>
You have ordered <asp:Label id="Label1"
runat="server"></asp:Label> Widgets.
The value of the widgets is <asp:Label id="Label2"
runat="server"></asp:Label>.
</p>
</form>
</body>
</html>
 
K

Karl Seguin

To make sure i understand the question, let me rephrase it. You are
wondering how to put values from some data structure (say a dataset) into a
web page. I take it the point to your question is that you don't want to
manually have to do:

txtEmail.Text = datatable.rows[0]["Email"];

for some 100 fields?

In asp.net 2.0 there'll be a DetailsView to help automate this otherwise
highly manual process
(http://msdn.microsoft.com/asp.net/whidbey/default.aspx?pull=/library/en-us/
dnvs05/html/grddetview.asp#grddetview_topic5)

Currently, you can look for a similar controls for 1.0...Your only other
alternative is to do something like:

<table>
<tr>
<td>First Name</td>
<td><asp:textbox runat="server" id="firstName" /></td>
</tr>
<tr>
<td>LastName</td>
<td><asp:textbox runat="server" id="lastName" /></td>
</tr>
</table>

then in codebehind:

foeach (DataColumn column in datatable.Columns){
Control c = page.FindControl(c.ColumnName)
if (c != null){
if (typeof(c) == TextBox){
((TextBox)c).Text = datatable.rows[0][column];
}else if (typeof(c) == RadioButtonList){
....
}
}
}

anyways, just some rough ideas...

Karl
 
G

Guest

In my contract details page I have a GENERATE CONTRACT button. What does it
do? It retrieves the coresponding row from contracts table by ID, and the
products child rows (nevermind this last one). After that I presume a
Server.Transfer("blablablafile.htm") to the completed page with the missing
fields that I got from the table. But the problem is that in that html (not
aspx) there has to be no scripting code as I got it from my boss. It could
have been easily resolved by use of Session in an aspx page but again "the
boss" said that in html should be inserts like {variable_name}. Let me know
if it is necessary to further explain myself, as I am a little annoyed by
this little thingie.

Karl Seguin said:
To make sure i understand the question, let me rephrase it. You are
wondering how to put values from some data structure (say a dataset) into a
web page. I take it the point to your question is that you don't want to
manually have to do:

txtEmail.Text = datatable.rows[0]["Email"];

for some 100 fields?

In asp.net 2.0 there'll be a DetailsView to help automate this otherwise
highly manual process
(http://msdn.microsoft.com/asp.net/whidbey/default.aspx?pull=/library/en-us/
dnvs05/html/grddetview.asp#grddetview_topic5)

Currently, you can look for a similar controls for 1.0...Your only other
alternative is to do something like:

<table>
<tr>
<td>First Name</td>
<td><asp:textbox runat="server" id="firstName" /></td>
</tr>
<tr>
<td>LastName</td>
<td><asp:textbox runat="server" id="lastName" /></td>
</tr>
</table>

then in codebehind:

foeach (DataColumn column in datatable.Columns){
Control c = page.FindControl(c.ColumnName)
if (c != null){
if (typeof(c) == TextBox){
((TextBox)c).Text = datatable.rows[0][column];
}else if (typeof(c) == RadioButtonList){
....
}
}
}

anyways, just some rough ideas...

Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/


Vlady said:
Hello. I recently built an ASP.NET "financial" application. I got to the
point where I have a HTML template (a contract) which needs to be filled with
various data from the database. I put a "generate contract" button on my ASPX
page, but I totally lack ideas of how to put the data in the correct places
in the html file. I was recommended (actually "do like this" from my boss) to
enclose the variable name like this {variable_name} in the html, and he said
that there are some functions to assign the value to the variable but how???
So the question is: how can I place the data that I gather from the database
inside the html file using only asp.net, without any report generation tool
(click the button and there you go: window with the contract filled)? A
little more elaborated example would be highly appreciated as this is my
first ASP.NET app.
 
K

Karl Seguin

If you are stuck with templates in HTML, then you're only/best bet is to
read the HTML file into a string, like:

StringBuilder sb = null;
StreamReader sr = null;
try {
//CONSIDER CACHING THIS!!!
sr = new StreamReader(Server.MapPath("mytemplate.html"));
sb = new StringBuilder(sr.ReadToEnd());
}finally {
if (sr != null){
sr.Close();
}
}

foreach (DataColumn column in dt.Columns) {
sb.Replace(column.ColumnName, Convert.ToString(dt.Rows[0][column]));
}



then you can do someLIteral.Text = sb.ToString();

so your generate contract page will actually be an aspx file with a literal
on it. The code behind will read the HTML file and do as your boss suggests
(search/replace).

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


Vlady said:
In my contract details page I have a GENERATE CONTRACT button. What does it
do? It retrieves the coresponding row from contracts table by ID, and the
products child rows (nevermind this last one). After that I presume a
Server.Transfer("blablablafile.htm") to the completed page with the missing
fields that I got from the table. But the problem is that in that html (not
aspx) there has to be no scripting code as I got it from my boss. It could
have been easily resolved by use of Session in an aspx page but again "the
boss" said that in html should be inserts like {variable_name}. Let me know
if it is necessary to further explain myself, as I am a little annoyed by
this little thingie.

Karl Seguin said:
To make sure i understand the question, let me rephrase it. You are
wondering how to put values from some data structure (say a dataset) into a
web page. I take it the point to your question is that you don't want to
manually have to do:

txtEmail.Text = datatable.rows[0]["Email"];

for some 100 fields?

In asp.net 2.0 there'll be a DetailsView to help automate this otherwise
highly manual process
(http://msdn.microsoft.com/asp.net/whidbey/default.aspx?pull=/library/en-us/
dnvs05/html/grddetview.asp#grddetview_topic5)

Currently, you can look for a similar controls for 1.0...Your only other
alternative is to do something like:

<table>
<tr>
<td>First Name</td>
<td><asp:textbox runat="server" id="firstName" /></td>
</tr>
<tr>
<td>LastName</td>
<td><asp:textbox runat="server" id="lastName" /></td>
</tr>
</table>

then in codebehind:

foeach (DataColumn column in datatable.Columns){
Control c = page.FindControl(c.ColumnName)
if (c != null){
if (typeof(c) == TextBox){
((TextBox)c).Text = datatable.rows[0][column];
}else if (typeof(c) == RadioButtonList){
....
}
}
}

anyways, just some rough ideas...

Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/


Vlady said:
Hello. I recently built an ASP.NET "financial" application. I got to the
point where I have a HTML template (a contract) which needs to be
filled
with
various data from the database. I put a "generate contract" button on
my
ASPX
page, but I totally lack ideas of how to put the data in the correct places
in the html file. I was recommended (actually "do like this" from my
boss)
to
enclose the variable name like this {variable_name} in the html, and
he
said
that there are some functions to assign the value to the variable but how???
So the question is: how can I place the data that I gather from the database
inside the html file using only asp.net, without any report generation tool
(click the button and there you go: window with the contract filled)? A
little more elaborated example would be highly appreciated as this is my
first ASP.NET app.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top