469,936 Members | 2,413 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,936 developers. It's quick & easy.

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

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?
Nov 19 '05 #1
9 1655
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

--
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!)
"John Smith" <me@me.me> wrote in message
news:e4**************@TK2MSFTNGP14.phx.gbl...
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?

Nov 19 '05 #2

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

Damn.
"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl...
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

--
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!)
"John Smith" <me@me.me> wrote in message
news:e4**************@TK2MSFTNGP14.phx.gbl...
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?


Nov 19 '05 #3
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.

"John Smith" <me@me.me> wrote in message
news:e4**************@TK2MSFTNGP14.phx.gbl...
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?

Nov 19 '05 #4
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!)
"John" <me@me.me> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...

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

Damn.
"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl...
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

--
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!)
"John Smith" <me@me.me> wrote in message
news:e4**************@TK2MSFTNGP14.phx.gbl...
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?



Nov 19 '05 #5
it's just a public class Class1. It supposed to be a business logic class.
"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:uR**************@TK2MSFTNGP12.phx.gbl...
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.

"John Smith" <me@me.me> wrote in message
news:e4**************@TK2MSFTNGP14.phx.gbl...
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?


Nov 19 '05 #6
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.

"John" <me@me.me> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
it's just a public class Class1. It supposed to be a business logic class.
"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:uR**************@TK2MSFTNGP12.phx.gbl...
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.

"John Smith" <me@me.me> wrote in message
news:e4**************@TK2MSFTNGP14.phx.gbl...
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?



Nov 19 '05 #7


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.
"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:uy**************@TK2MSFTNGP10.phx.gbl...
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!)
"John" <me@me.me> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...

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

Damn.
"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl...
> 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
>
> --
> 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!)
>
>
> "John Smith" <me@me.me> wrote in message
> news:e4**************@TK2MSFTNGP14.phx.gbl...
>> 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?
>>
>>
>
>



Nov 19 '05 #8
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!)
"John" <me@me.me> wrote in message
news:uQ**************@tk2msftngp13.phx.gbl...


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.
"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:uy**************@TK2MSFTNGP10.phx.gbl...
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!)
"John" <me@me.me> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...

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

Damn.
"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl...
> 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
>
> --
> 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!)
>
>
> "John Smith" <me@me.me> wrote in message
> news:e4**************@TK2MSFTNGP14.phx.gbl...
>> 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?
>>
>>
>
>



Nov 19 '05 #9

Thank you both. I will have to re-write it.
I know what I'll be doing this weekend :-(
"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:O$****************@TK2MSFTNGP14.phx.gbl...
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!)
"John" <me@me.me> wrote in message
news:uQ**************@tk2msftngp13.phx.gbl...


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.
"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:uy**************@TK2MSFTNGP10.phx.gbl...
> 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!)
> "John" <me@me.me> wrote in message
> news:%2****************@TK2MSFTNGP12.phx.gbl...
>>
>> Thanks for the reply.
>> No luck here, I get this error:
>> Cannot convert type 'System.Web.UI.Page' to WebForm1.
>>
>> Damn.
>>
>>
>> "Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
>> net>
>> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl...
>> > 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
>> >
>> > --
>> > 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!)
>> >
>> >
>> > "John Smith" <me@me.me> wrote in message
>> > news:e4**************@TK2MSFTNGP14.phx.gbl...
>> >> 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?
>> >>
>> >>
>> >
>> >
>>
>>
>
>



Nov 19 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Peter Frost | last post: by
29 posts views Thread by Patrick | last post: by
3 posts views Thread by Wee Bubba | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.