By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,967 Members | 1,554 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,967 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.
Nov 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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>
"Vlady" wrote:
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.

Nov 18 '05 #2

P: n/a
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/wh...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" <Vl***@discussions.microsoft.com> wrote in message
news:94**********************************@microsof t.com...
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.

Nov 18 '05 #3

P: n/a
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" wrote:
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/wh...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" <Vl***@discussions.microsoft.com> wrote in message
news:94**********************************@microsof t.com...
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.


Nov 18 '05 #4

P: n/a
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" <Vl***@discussions.microsoft.com> wrote in message
news:33**********************************@microsof t.com...
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" wrote:
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/wh...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" <Vl***@discussions.microsoft.com> wrote in message
news:94**********************************@microsof t.com...
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.


Nov 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.