using a User Control

G

Guest

Hello,

I created a WebPage.aspx that has some functions in it, and I created a User
Control that has a File Field Control and a Botton control on the .ascx. I
added myUserControl.ascx to the WebPage.aspx, but it can not access the
WebPage.aspx functions.

If I add a button to the WebPage.aspx, I can access those functions no
problem. Is not adding a User Control the samething? If not, what must I do
in myUserControl.ascx file so I can access the functions that are on the
WebPage.aspx where I placed myUserControl.ascx?

Thanks,
 
G

Guest

when you think about it, your button can not access any of the page members.
You're writing page code that accesses the button.

Since your page code should not contain any logic at all, this might be a
sign that you need to break out some functionality into some seperate classes
 
M

Mark Fitzpatrick

The user control should not be accessing functions that exist in the main
web page. User controls are encapsulated logic. Basically, you build them to
re-use in lots of different places. Because of this, you don't want them
trying to access functionality on a page. They can inform the page of
certain events through event bubbling, and you can access properties and
methods of the user control from the page, but you don't want to do it the
other way around. It's just like the button control. You place the button
control and you can access the properties and methods of the button. The
button does not access the page code itself and perform any tasks.
 
G

Guest

I thought about that, but the developer who coded this originally did not do
that and uses ViewState, Session, Request all though out the Webpage making
exceptionally hard to do, considing I only have a short time to make this
change. If I had more time, no problem.

If I did this myself I would have used classes and put very little code in
my .aspx page, which is the right way to do things like you said.
 
B

Baski

All you have to do is in your user control code use
(assume your class name for your aspx page is Webform1.cs
Webform1 form = this.Page as Webform1;

if(form ! == null)
{
// call the page function here
form1.funcation();
}
 

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