Simple OO Question for .NET

B

byrd48

Hi,
If I want to dynamically build an asp:table in a class file and return
it to the caller as an object ie:

public Table TableGenerator(){

public Table myTable = new Table();
....
....
return myTable;
}

My question is this, how do I call this method and display the
resulting table in the presentation layer. I'm sure there is more than
one way to accomplish this.

Thanks,
Jon
 
G

Guest

To call the method you would have to create in instance of the particular
class such as:

MyClassType MyInstance = new MyClass();

Then call the method and return the value to a Table object:

Table MyTable = MyInstance.TableGenerator;

To display the table you just need to ad it to the controls collection of
another control using the add or AddAt method, eg to display it on the page
at the end of the page:

Page.Controls.Add(MyTable);

You just need to make sure the above code goes in the relevent event handler
to determin when the table get's displayed.

If you are returning a variable/object from a method in a class it is usual
to have a scope local to that method rather than exposing it as a public
variable.
 
L

Laurent Bugnion

Hi,
Hi,
If I want to dynamically build an asp:table in a class file and return
it to the caller as an object ie:

public Table TableGenerator(){

public Table myTable = new Table();
...
...
return myTable;
}

My question is this, how do I call this method and display the
resulting table in the presentation layer. I'm sure there is more than
one way to accomplish this.

Thanks,
Jon

The method name should reflect an action, so I'd rather choose
GenerateTable() or something.

The resulting table is a Control, so it has the inherited method
RenderControl, which you can call in the Page_Load method of your page,
or in the Render method of another control. The place you execute the
RenderControl method will determine where in the page the Table is rendered.

HTH,
Laurent
 
B

byrd48

HI and thanks, that worked.
I have another question now. I built the table dynamically in the class
file and I added quite a few imagebuttons as in:

ImageButton Slot1 = new ImageButton();
Slot1.ID = "1";

Now when I call this class, I will pass a parameter that matches the
imagebutton.ID. I want to write a function to find which imagebutton
the parameter matches to, then dynamically assign the imageurl
property. In english terms, I want it to do something like:

find imagebutton with ID 1 and assign imageurl = "someimage.jpg".

My question is how do I either loop through the control collection, or
search for the specific control?
Thanks much,

Jon
 
B

byrd48

I'd like to clarify on the previous post.
If I define a new image button inside the class as mentioned
previously, what is the container I would reference to loop through the
controls?
If I added it to another control on the page, I could reference it by:

foreach (control c in table1.controls)

Where I'm having trouble now is finding the parent container to loop
through.
Thanks,
Jon
 
L

Laurent Bugnion

Hi,
I'd like to clarify on the previous post.
If I define a new image button inside the class as mentioned
previously, what is the container I would reference to loop through the
controls?
If I added it to another control on the page, I could reference it by:

foreach (control c in table1.controls)

Where I'm having trouble now is finding the parent container to loop
through.
Thanks,
Jon

The children controls of a page or of a control can be accessed with the
Controls collection. Actually, the Page class is derived from the
Control class, so pretty much everything you can do with a control, you
can also do it with the Page. The HtmlControl and WebControl classes are
also derived from the Control class.

To check if a Control has children or not, you can use the HasControls
property.

Though I don't mind answering on the newsgroup :) you will be faster if
you check the MSDN documentation, which either comes with Visual Studio,
or is available online at
http://msdn1.microsoft.com/en-us/default.aspx

For example:
http://msdn2.microsoft.com/en-us/library/system.web.ui.control_members.aspx

HTH,
Laurent
 
B

byrd48

Thank you for your assistance. I look through the help files and do
web searches extensively before posting to see if I can learn on my
own.
I got the above item to work by creating an array of imagebuttons which
I could easily access programmatically. However, in a related issue,
When I dynamically add these imagebuttons to the table, I would like to
add a click event to them to wire back to the code page, similar to the
OnCommand event I used to use in 1.x and VB.NET. I can add commandname
and commandargs to the control at run time, but the trick now is how to
access them when I load this table into the calling page.
I guess this is the same question as before, I'm not getting a handle
on containers.

Thanks again.

Jon
 
L

Laurent Bugnion

Hi,
Thank you for your assistance. I look through the help files and do
web searches extensively before posting to see if I can learn on my
own.

No problems, I was not sure if you were aware of the online documentation.
I got the above item to work by creating an array of imagebuttons which
I could easily access programmatically. However, in a related issue,
When I dynamically add these imagebuttons to the table, I would like to
add a click event to them to wire back to the code page, similar to the
OnCommand event I used to use in 1.x and VB.NET. I can add commandname
and commandargs to the control at run time, but the trick now is how to
access them when I load this table into the calling page.
I guess this is the same question as before, I'm not getting a handle
on containers.

Thanks again.

Jon

I fear that I cannot help more in this issue. I don't fully understand
the question, so it migh be over my head ;-)

I hope someone else can help.

Greetings
Laurent
 

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

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top