im fairly new to the .Net world and i'm tryin to make a simple page that dynamicly fills a table/form from a database based on user log info. i've been doin some reading and the datalist/repeater seem like more than i need...or maybe they are exactly what i need but i'm not using them correctly. i have the login working already (thanks to some nice code samples from this site) that idenifies the user and stores first and last name in session variable to be used with later SQL strings. if anyone could suggest something or point me in the right direction, that'd be great. thanks!

What does the rendered table need to do?

If the answer is "it needs to contain "stuff" that the user will click/change, and then more server code will run", then you need to "Render dynamic controls", and should use a server side control to do that. Repeater. DataList. DataGrid.

If the answer is "sit there and look pretty", then you simply output a good old fashioned HTML Table, by using the LiteralControl.

The idea is: drag a PlaceHolder where you want the table to be.

Build a query, run the query, return a SqlDataReader.

Start your table:

PlaceHolder1.Controls.Add(new LiteralControl("<table>"));

Read through your datareader, outputting tags and content as you go:

while(myDataReader.Read())
{
  PlaceHolder1.Controls.Add(new LiteralControl("<tr><td>" + myDataReader["myField"].ToString() + "</td></tr>"));
}

And so on.

tgreer, thank you so much. that was excatly what i was looking for. just had to make very minor change for VB.Net :cheesy:

You're welcome. Ain't forums grand?

Hi

I need to render a table with dynamic rows based on data obtained through data reader. Each row has a image link button, a link button
and a label to which data comes from the database. In the earlier message you said we can use either a datalist, repeator or datagrid.
Can you please let me know how to render the table dynamically.

Thanks
Sandy

What does the rendered table need to do?

If the answer is "it needs to contain "stuff" that the user will click/change, and then more server code will run", then you need to "Render dynamic controls", and should use a server side control to do that. Repeater. DataList. DataGrid.

If the answer is "sit there and look pretty", then you simply output a good old fashioned HTML Table, by using the LiteralControl.

The idea is: drag a PlaceHolder where you want the table to be.

Build a query, run the query, return a SqlDataReader.

Start your table:

PlaceHolder1.Controls.Add(new LiteralControl("<table>"));

Read through your datareader, outputting tags and content as you go:

while(myDataReader.Read())
{
  PlaceHolder1.Controls.Add(new LiteralControl("<tr><td>" + myDataReader["myField"].ToString() + "</td></tr>"));
}

And so on.

The central point is, with ASP.NET, you need to forget HTML. Stop thinking of rendering a "table". Instead, use an ASP.NET Server Control. It will render the table.

There are lots of "Repeater control" examples on the web, if you search for them.

The central point is, with ASP.NET, you need to forget HTML. Stop thinking of rendering a "table". Instead, use an ASP.NET Server Control. It will render the table.

There are lots of "Repeater control" examples on the web, if you search for them.

Thanks for the info. I have tried with Repeater control and it works. Only thing is, I have an asp:ImageButton Control which OnClick should open a url obtained from database. I am not sure about the exact format to do this. Following is what I am doing:

<asp:Repeater ID="repSpecials" Runat="server">
<ItemTemplate>

<td
<asp:ImageButton ID="ibSpecial" Runat="server" ImageUrl='<%# DataBinder.Eval(Container.DataItem, "ImageUrl") %>'
OnClick="javascript:OpenSpecialsLink('<%# DataBinder.Eval(Container.DataItem, "SpecialsLink") %>'); >

</asp:ImageButton>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>

Thanks for the info. I have tried with Repeater control and it works. Only thing is, I have an asp:ImageButton Control which OnClick should open a url obtained from database. I am not sure about the exact format to do this. Following is what I am doing:

<asp:Repeater ID="repSpecials" Runat="server">
<ItemTemplate>

<td
<asp:ImageButton ID="ibSpecial" Runat="server" ImageUrl='<%# DataBinder.Eval(Container.DataItem, "ImageUrl") %>'
OnClick="javascript:OpenSpecialsLink('<%# DataBinder.Eval(Container.DataItem, "SpecialsLink") %>'); >

</asp:ImageButton>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>

I get the following error with this code:

Parser Error
Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.

Parser Error Message: The server tag is not well formed.

Source Error:


Line 97: </td>
Line 98: <td bgcolor="#ffccff">
Line 99: <asp:ImageButton ID="ibSpecial" Runat="server" ImageUrl='<%# DataBinder.Eval(Container.DataItem, "ImageUrl") %>' OnClick="javascript:OpenSpecialsLink('<%# DataBinder.Eval(Container.DataItem, "SpecialsLink") %>'); >
Line 100: </asp:ImageButton>
Line 101:

Since it's a server control, it's expecting the "onclick" attribute to contain a server-side method as its value. That's one problem.

It's been awhile, so this might not be 100% correct, but I think what you need to do is code the server-side ItemDataBound event. Within that method, you check the argument type, and if the control is an image button, then use the control's ".Attributes.Add()" method to code your onclick statement.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.