Access custom page methods from a user control c#

U

ultplyr

From within my user control I need to access a method of the page that
hosts the user control. I can do this by casting the page to the page
that I need, but I don't know this at runtime.

example:
from within my control :
myDatagrid.DataSource = ((vieworders.aspx)Page).PageMethod();

is there a way to do this without casting.
ie:
dgrdToPage.DataSource = Page.PageMethod();

thanks.
 
U

ultplyr

Figured out a way to access a page's methods from a user control
placed on that page. Hope this helps someone, it baffled me for
awhile.

From within the ASP.NET application namspace create an interface an
place it in the code behind section of the .aspx page.
Then implement that method in the code behind page class.

namespace MyWebsite
{
public interface IData
{
DataView GetDataView();
}

public class My_ASPX_Page : System.Web.UI.Page
{

public DataView GetDataView()
{
DataView dvwMyDataView = new DataView();
return dvwMyDataView;
}
}
}


In the code behind of the control class (.ascx) write the following
code.

public class Pager : System.Web.UI.UserControl
{
IData pgData;

private void Page_Load(object sender, System.EventArgs e)
{

(IData) pgData = (IData) this.Page;
MyDataGrid.DataSource = pgData.GetDataView();
}
}
 

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

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,190
Latest member
ClayE7480

Latest Threads

Top