Can a user control call a method in the 'parent' aspx page?

  • Thread starter Thread starter engwar1
  • Start date Start date
E

engwar1

I have a page that my users will go to to upload files to my website.
As I want to reuse the file upload code on multiple pages I put the
file upload textbox/buttons on a user control which I plan to include
in multiple aspx pages where they'll be able to upload images for other
purposes.

On the first page that I'm working on where users will be doing the
upload (which I'll call the 'parent' page) I also have a DataGrid where
I display the images that the user has already uploaded. The problem
I'm running into is that the code that hits the database and binds the
dataset to the DataGrid only runs when the page loads for the first
time. And I can't determine how to call my DataBind method from the
method in my user control that runs when a person actually uploads a
new image.

So the image does get uploaded but the DataGrid doesn't refresh
displaying the most recent image that's just been added.

I can re-write my code to not use a user control. I'd have similar code
repeated multiple times in 3 or 4 .aspx pages (which I'd like to
avoid.)

Any ideas? Can you call a method in an aspx.cs page from the
code-behind of a user control included in that page?
 
You should easily be able to get the parent page from the Parent
property on your control. You can then cast this instance of the control to
the type of the parent page and then access whatever you need to from there.

Hope this helps.
 
Nicolas, I'm just adding a small piece of code :-)

public class YourControlClass : UserControl
{
override protected OnLoad(EventArgs e)
{
// Casts the Page property to the desired type and access the method
((YourPageClass) this.Page).YourMethod();
}
}
 
Back
Top