Rendering ASP controls programmatically

G

Guest

I just started using ASP.NET yesterday (literally). I've been using ASP for some time, though

This code is in mypage.aspx, and it works just as I intended, showing each table in a separately rendered grid

for (int i = 0; i < 5; i++
{ // iterate through dataset table
grd0.DataSource= dsMain.Tables.DefaultView; //tbl.DefaultView
grd0.DataBind()
%><asp:datagrid id="grd0" runat="server" CellPadding="3" EnableViewState="False"><AlternatingItemStyle BackColor="PaleGreen"></AlternatingItemStyle><HeaderStyle Font-Size="Smaller" Font-Bold="True" ForeColor="White
BackColor="DarkGreen"></HeaderStyle></asp:datagrid><br><
} // end iteratio

This works fine, but I would like to do this in the code-behind page. How can I programatically render the control? Is it simply "grd0.RenderControl;"? Do I have to set all the properties explicitly, or can I copy an unbound prototype DataGrid set up in the Design window and repeatedly rebind the copy? Please give example code

The statement "foreach (DataTable tbl in dsMain.Tables)..." works fine in mypage.aspx.cs, but not in mypage.aspx. At runtime, I got an error saying it could not find the type DataTable. Why not
 
C

clintonG

I learned ASP first myself too. I think you should buy one of the MCAD
certification training guides and start doing the chapter lessons. Using
QUE's "Developing and Implementing Web Applications with Visual C#
..NET and Visual Studio .NET" is recommended. It's working for me.

It would take too long to descibe how to programatically render a
control as there are several approaches, i.e implement a Render( )
method or create a control designer class for example.

You get the DataTable( ) type error because the type is inherited
from System.Data namespace heirarchy which the DataTable can
not inherit from as your source lacks a reference to the namespace
via the using keyword.

You'll pick this up real fast with the QUE training guide... and another
thing... I have not found it practical to try to learn and develop ASP.NET
applications without using Visual Studio.NET and the MSDN Library.


--
<%= Clinton Gallagher
A/E/C Consulting, Web Design, e-Commerce Software Development
Wauwatosa, Milwaukee County, Wisconsin USA
NET (e-mail address removed)
URL http://www.metromilwaukee.com/clintongallagher/




Zanoni said:
I just started using ASP.NET yesterday (literally). I've been using ASP for some time, though.

This code is in mypage.aspx, and it works just as I intended, showing each
table in a separately rendered grid:
for (int i = 0; i < 5; i++)
{ // iterate through dataset tables
grd0.DataSource= dsMain.Tables.DefaultView; //tbl.DefaultView;
grd0.DataBind();
%><asp:datagrid id="grd0" runat="server" CellPadding="3"

EnableViewState="False"><AlternatingItemStyle
BackColor="DarkGreen"> said:
} // end iteration

This works fine, but I would like to do this in the code-behind page. How
can I programatically render the control? Is it simply
"grd0.RenderControl;"? Do I have to set all the properties explicitly, or
can I copy an unbound prototype DataGrid set up in the Design window and
repeatedly rebind the copy? Please give example code.
The statement "foreach (DataTable tbl in dsMain.Tables)..." works fine in
mypage.aspx.cs, but not in mypage.aspx. At runtime, I got an error saying
it could not find the type DataTable. Why not?
 
Z

Zanoni

I learned ASP first myself too. I think you should buy one of the MCAD
certification training guides and start doing the chapter lessons. Using
QUE's "Developing and Implementing Web Applications with Visual C#
.NET and Visual Studio .NET" is recommended. It's working for me.

Thanks for the reply, Clinton, and for the references. I got this far
using only the MSDN library and the Quickstart website.
It would take too long to descibe how to programatically render a
control as there are several approaches, i.e implement a Render( )
method or create a control designer class for example.

It seems strange to me there wouldn't be a simple Render() method
built in. I was hoping RenderControl() would work, but it requires a
HtmlWriter object, which I could not find a way to provide using the
Response object.
You get the DataTable( ) type error because the type is inherited
from System.Data namespace heirarchy which the DataTable can
not inherit from as your source lacks a reference to the namespace
via the using keyword.

So the "using" statements in mypage.aspx.cs do not affect mypage.aspx
itself? I am using System.Data in the .cs file.
You'll pick this up real fast with the QUE training guide... and another
thing... I have not found it practical to try to learn and develop ASP.NET
applications without using Visual Studio.NET and the MSDN Library.

I am using both vstudio.NET and the MSDN lib.

Thanks!
 

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

Top