Dynamically populating a webform

B

Ben Ong

Hi,

i'm pretty new to the .NET framework and am looking for some help. Is
there any way to dynamically create a number of hypertext links
dependant upon what is extracted from a database. This is a lil
snippet of my code.


<script runat=server>
for (int i =0 ; i < measureArray.Count; i++)
{ </script>
<td>
<asp:Label ID=measureArray.Name text=measureArray.Value />
</td>
<script runat=server>
}
</script>

measureArray is an array of objects of type Measure(string name, in
value) whose values are extracted from a relational database.
i know this code wont work and i know it may look stupid. I just wrote
it to try to give you guys a better idea of what im trying to do.
Thanks for the help.

-ben
 
G

Guest

Hi Ben
This is exactly what we mean by writing ASPNET Rendered
Controls. You can write your own control in ASPNET. Ok
here goes the logic, but before I start, I assume you are
a bit familiar with HTML and C#.

All you need to do is to first create a string which
stores HTML data equivalent to the 100 hyperlinks that
you wanna create, and then render this string on a HTML
Label. Find attached the sample code(not tested).

public class HyperlinkArray :
System.Web.UI.WebControls.Label
{
public HyperlinkArray()
{
//
// TODO: Add constructor logic
here
//
}

public System.Data.DataTable LabelData;

protected override void OnPreRender
(EventArgs e)
{
HttpWriter htp = new HttpWriter();
htp.Write(FormHtmlString());
base.OnPreRender (e);
}

private string FormHtmlString()
{
string s = "";
for(int i = 0;
i<LabelData.Columns.Count; i++)
{
s += "<a href=" +
LabelData.Rows["Hyperlink"].ToString();
s += "
CSSclass=\"HyperLinkClass\" >";
s += ">" + LabelData.Rows
["HyperlinkText"].ToString();
s += "</a>";
}

return s;
}

}

Hope this helps...
Ramjee

-----Original Message-----
Hi,

i'm pretty new to the .NET framework and am looking for some help. Is
there any way to dynamically create a number of hypertext links
dependant upon what is extracted from a database. This is a lil
snippet of my code.


<script runat=server>
for (int i =0 ; i < measureArray.Count; i++)
{
<td>
<asp:Label ID=measureArray.Name text=measureArray .Value />
</td>
<script runat=server>
}
</script>

measureArray is an array of objects of type Measure (string name, in
value) whose values are extracted from a relational database.
i know this code wont work and i know it may look stupid. I just wrote
it to try to give you guys a better idea of what im trying to do.
Thanks for the help.

-ben

.
 
B

Benjamin Ong

Hi Ramjee,

Thanks for the reply to my message. While I was waiting for posts I
came up with another method of dynamically generating web controls in
ASP.NET. I did some research and found out that by using the
placeholder web control I could generate any number of Web controls
dynamically. I haven't had a chance to try your solution yet but I was
wondering if you had any experience using the place holder web control,
and if so which solution should be used and why.


Thanks,

Ben
 

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,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top