Object Design

  • Thread starter Thread starter JJ
  • Start date Start date
J

JJ

Is it possible if I create an object that I created to have a method
that would register a pagescriptblock for a aspx page? So I would use
Page.RegisterClientScriptBlock in the method being called and the
object would know that it is refering to the calling aspx page.


Thanks,

JJ
 
not 100% clear on what ur asking. Are you asking if you can access the Page
instance from a class method? Yes, using HttpContext.Current.Handler

Page page = HttpContext.CurrentHandler as Page;
if (page == null)
{
throw new ApplicationException("This method can only be called from within
an ASP.NET request");
}
page.Register...


karl
 
Hi,
Is it possible if I create an object that I created to have a method
that would register a pagescriptblock for a aspx page? So I would use
Page.RegisterClientScriptBlock in the method being called and the
object would know that it is refering to the calling aspx page.


Thanks,

JJ

I am not sure if it's what you mean, but...

If your object is a control, it has a this.Page property which refers to
the ASPX page the control is included on. So you can do

this.Page.Register...

If your object is not a control, and has no reference to the page,
nothing forbids you to do:

public void MyMethod( Page thePage )
{
thePage.Register...
}

Is that what you meant?
Laurent
 
If I create a class library that has a document class that I created
and I call the class in an aspx page it is now an object of the
document class. Now when I call a method that is part of the object,
just as you guys have above I wanted to know if its possible to call
Page.RegisterClientScriptBlock and it would know to register it for
that given page. I see I would have to pass in the a reference to the
page in my method correct? Am I using the right terminology here in my
explanation?

Thanks,
JJ
 
You're still not being very clear...but I do believe either Laurent's or my
solution is what you are looking for.

Karl
 
Hi,
If I create a class library that has a document class that I created
and I call the class in an aspx page it is now an object of the
document class. Now when I call a method that is part of the object,
just as you guys have above I wanted to know if its possible to call
Page.RegisterClientScriptBlock and it would know to register it for
that given page. I see I would have to pass in the a reference to the
page in my method correct? Am I using the right terminology here in my
explanation?

Thanks,
JJ

The thing is: You need a reference to the Page containing the object you
created. There are many ways to get a reference to the Page your control
is into (see Karl's and my proposals). Your design must plan a way to
get the Page's reference, one way or the other.

Let's say you're in the Page_Load event handler, and you do:

MyObject myObject = new MyObject();

With this code, your "MyObject" instance doesn't have any reference to
the Page class. This is a unidirectional composition in UML. If you want
to pass a reference from the Page to the MyObject, you need something like:

MyObject myObject = new MyObject( this );

This is then bidirectional, means that you can reach the MyObject from
the Page, and you can also reach the Page from the MyObject.

Is that clearer?

Laurent
 

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

Back
Top