Getting to the ClientScript object from another C# Class

G

Guest

I want to create a C# class where I'll have a bunch of static functions which
will use the ClientScript object to dump out some JavaScript dynamically.

But I can't figure out how to get a hold of the "ClientScript" object in
those other classes. I guess it's that I have to get the current Page object
and work down to the Page's ClientScript object. But I don't know how to do
that either. I tried HttpContext.Current... but that doesn't get me to the
Page or the ClientScript object.

So how to I get a hold of the Page object from another class?

Alex
 
S

Steven Cheng[MSFT]

Hi Alex,

As for the ClientScript property, it is specific to the Page class, and as
for the HttpContext, it could be established for different httphandlers,
not only Page, but also other general httphandlers, that's why it doesn't
expose a Page property. However, we still could use the
HttpContext.Handler property to access the current request's processing
handler instance, if the request is a page request, the handler is just the
page instance. e.g:

Page page = HttpContext.Current.Handler as Page;

if (page != null)
{
page.ClientScript.RegisterStartupScript(page.GetType(),
"msgbox", "alert('hello.');", true);
}

In addition, if the utility class is mostly used directly in page code, I
suggest you consider Steve's suggestion on passing the page instance as
input parameter of the utility functions.

Hope this helps.

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

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