ClientScript.RegisterClientScriptInclude -

D

Dariusz Tomoñ

Hello,

I'd like to include JS from C# code (ASP.NET 2.0). I'm trying not directly
from i.e. default.aspx.cs code but from a class belonging to DLL, which is
referred in my project.

The code is as follow:

Page.ClientScript.RegisterClientScriptInclude(Page.GetType(),"swfobject.js","swfobject.js");

string scriptBlock = "<script type=\"text/javascript\"
language=\"JavaScript\">" +

"var fo = new SWFObject(\"" + path + "\", \"\", \""+szerokosc+"\",
\""+wysokosc+"\", \"6\", \"#FFFFFF\");" +

"fo.write(\"" + layer + "\");" +

"</script>";

All the time I get error in the line -
Page.ClientScript.RegisterClientScriptInclude(Page.GetType(),"swfobject.js","swfobject.js");
telling that reference to the object is not set for object occurence (I
cannot provide the error message because it's in my language - I have got
polish version of Win2003Serv. At the occasion: Is it possible to force my
environment to throw english errors not polish?).


I tried to change Page.GetType() to this.GetType() but without any effect.

Thank you for any suggestion.

Darek T.
 
S

Stan SR

Hi,

Did you try this ?

ClientScript.RegisterClientScriptBlock(this.GetType(), "swobject",
scriptBlock);

Hth
Stan
 
G

Guest

Dariusz,

you will need to call it as follows:

page..ClientScript.RegisterClientScriptInclude(page.GetType(), ...

where page will be the reference to the page on which you are trying to
register your script.
 
D

Dariusz Tomon

ok, but 'this' is not my Page object, rather: -
EuroAdresBasic.EFlashControl2, where EuroAdresBasic is namespace and
EFlashControl2 is a class from within I'm trying to reffer to my Page
object.
I tried to someting like that:
Page page = new Page();

but in reality it is not my Page where I want to inject JS so I cannot see
my swf injected.



Best Regards

Darek
 
D

Dariusz Tomon

ok, but 'this' is not my Page object, rather: -
EuroAdresBasic.EFlashControl2, where EuroAdresBasic is namespace and
EFlashControl2 is a class from within I'm trying to reffer to my Page object
(Page is null)
I tried to someting like that:
Page page = new Page();

but in reality it is not my Page where I want to inject JS so I cannot see
my swf injected.

Best Regards

Darek
 
D

Dariusz Tomoñ

ok, but I don't know hot to reffer to my Page object (during executing this
line 'Page' is null. Other guys suggested using 'this' but 'this' is not my
Page object, rather: - EuroAdresBasic.EFlashControl2, where EuroAdresBasic
is namespace and EFlashControl2 is a class from within I'm trying to reffer
to my Page object (Page is null)
I tried to someting like that:
Page page = new Page();

but in reality it is not my Page where I want to inject JS so I cannot see
my swf injected.

The question is how to reffer to my Page from external project (the class is
in external project)

Best Regards

Darek

U¿ytkownik "Sergey Poberezovskiy"
 
G

Guest

Darek,

If you have a control that resides on the page you after, then you can use
it's Page property. If you say that your control's Page property is null, it
looks like you are trying to reference it too early in the control's lifetime
(before the control is added to the page controls hierarchy).

I usually register all my scripts in PreRender event - this way you do not
execute unnecessary code if, say in other control's events the current page
terminates (if Response is redirected, transferred or otherwise ended).
 
D

Dariusz Tomoñ

Dear Sergey,

I eventually got by ... but I had to pass on my Page object as parameter to
external class. Yes, before calling external class code I have value of Page
object and when I call the code of the dll my Page object seems to be
reseted to null (therefore I thought of passing it as parameter). Now it
works but I'm wondering if there is other method ...
I have no idea how can I invoke the code from external class using PreRender
event. I'm doing it from within code so I have no graphical representation
of external class and therefore I have no access to events in my VS2005.
Can you share a snippet of code with me?

My calling is like this:
EuroAdresBasic.EFlashControl2 fleszcontrol = new EFlashControl2(Page,
"panel", "http://www.euroadres.pl/images/flash/" + flesz, "360", "340");

And a snippet from EFlasControl2 is like below:

public class EFlashControl2 : System.Web.UI.WebControls.WebControl

{

protected System.Web.UI.HtmlControls.HtmlGenericControl FlashObject

= new System.Web.UI.HtmlControls.HtmlGenericControl("object");

protected System.Web.UI.HtmlControls.HtmlGenericControl FlashParam

= new System.Web.UI.HtmlControls.HtmlGenericControl("param");

protected System.Web.UI.HtmlControls.HtmlGenericControl FlashEmbed

= new System.Web.UI.HtmlControls.HtmlGenericControl("embed");

protected System.Web.UI.HtmlControls.HtmlGenericControl pr;

public EFlashControl2(Page PageObject, string layer, string path, string
szerokosc, string wysokosc)

{

//Page page = new Page();



Random RandomClass = new Random();

int RandomNumber = RandomClass.Next(0, 90000);

string SWFObjectName = RandomNumber.ToString();



PageObject.ClientScript.RegisterClientScriptInclude(PageObject.GetType(),
"swfobject.js", "swfobject.js");

string scriptBlock = "<script type=\"text/javascript\"
language=\"JavaScript\">" +

"var fo = new SWFObject(\"" + path + "\", \"" + SWFObjectName + "\", \"" +
szerokosc + "\", \"" + wysokosc + "\", \"6\", \"#FFFFFF\");" +

"fo.write(\"" + layer + "\");" +

"</script>";

PageObject.ClientScript.RegisterClientScriptBlock(PageObject.GetType(),
layer, scriptBlock);



Best Regards



Darek T.


U¿ytkownik "Sergey Poberezovskiy"
 
G

Guest

Darek,

Your EFlashControl2 inherits WebControl and therefore can override its
virtual methods (as you may not assume that AutoEventWireup is set to true on
the page).
something similar to the following:

protected override void OnPreRender(EventArgs e)
{
if (this.Page != null){
string scriptBlock = "...";
this.Page.ClientScript.RegisterClientScriptBlock(this.Page.GetType(), ...
}
base.OnPreRender(e);
}

the reason I included check on whether the Page property is not empty is
that control may not necessarily ends up rendered on the page, but sometimes
into a stringbuilder instead.
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top