How to get access to current page class from other class?

J

John Smith

Hi, I have this problem.
I have a page class in code behind called "WebForm1" and I have another
class called "Class1".

Now in Class1 I am creating ImageButton and I am adding event handler
onClick, then this ImageButton is added to page - WebForm1.

In WebForm1 I have my methods to deal with the data.
When I click ImageButton, ImageButton_onClick is called in Class1. Now, my
problem is that I want to call the method 'test' that is in WebForm1 class.
How can I do that?
 
K

Karl Seguin

John:
If I understand correctly, you should be able to access the page via the
Page property of your imageButton control

((WebForm1)imageButton.Page).Test();


Karl
 
J

John

Thanks for the reply.
No luck here, I get this error:
Cannot convert type 'System.Web.UI.Page' to WebForm1.

Damn.
 
K

Kevin Spencer

You have a "class" in which you're "creating ImageButton?" What SORT of
"class" is it?

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
What You Seek Is What You Get.
 
K

Karl Seguin

John:
I think Kevin's question is valid and the answer would help throw some
context into this question. This should likely be a server-control class,
but don't know if this is what you are doing...if it was you'd directly have
access to the Page property of the control class you are inheriting from...
Anyways, I still find it strange that the code I provided didn't work (it
would only work when the class is used in WebForm1), but that could
certainly be due to the lack of context I'm aware of...how is this
imagebutton added to the page?

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
 
K

Kevin Spencer

Thanks John. the reason I asked is that a business class should not contain
any UI elements in it, such as an ImageButton. If you want to create an
ImageButton in the page, you need to either use or design a Server Control
or User Control.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
What You Seek Is What You Get.
 
J

John

Class1 is called from WebForm1.
Table object is passed by calling method to Class1.
Class1 class1 = new Class1();
class1.CreateImageButtons(tblData);

Class1 creates ImageButtons and adds event handlers - ImageButton_OnClick,
and then adds those buttons to passed table.
imgDel.Click += new ImageClickEventHandler(imgDel_Click);
tblData.Controls.Add(imgDel);

Now, when I click one of the imagebuttons, method from Class1 is called -
ImageButton_OnClick.

So, now I am in Class1 and I would like to call a method from WebForm1 code
behind class.

I can get to Page object through 'sender' (imageButton.Page). But this Page
class doesn't contain my methods and converting to WebForm1 class throws an
error(error from post above).

private void imgDel_Click(object sender, ImageClickEventArgs e)

{

ImageButton button = (ImageButton)sender;

Page page = button.Page;

Table tblData = (Table)page.FindControl("tblData");

--??-- something.MethodFromWebForm1();

}

I know this is little mixed up, but I am working on this page for some time
now and I want to finish it as soon as possible :) The code quickly became
veeery bulky.
 
K

Karl Seguin

Kevin's correct. You really should be using a custom server control to do
this...especially one that extends the ImageButton class to make things
easier.

Google search "introduction to ASP.Net custom server controls"

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
 

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
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top