Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inlcude a .js file in code behind?

Status
Not open for further replies.

Ecreations

Programmer
Jul 6, 2002
220
CA
is there anyway to register a .js file from code behind i n VB.net?
I know theres a way to do it using the

RegisterClientScriptBlock


any hints would be appreciated




The solution is simple, the problem is complex.
 
To register a js file, I think you would probably need a placeholder, and then just add a LiteralControl to it:

<asp:placeHolder id=plcScript runat=server />

dim script as string = "<script language=javascript src=YourFile.js></script>"
dim lc as new LiteralControl(script)
plcScript.Controls.Clear()
plcScript.Controls.Add(lc)

-paul

penny.gif
penny.gif

The answer to getting answered -- faq855-2992
 
You've answered you own question here as RegisterClientScriptBlock is designed for the purpose. In reality all it actual does is render the string you pass as the script block to the HTML response immediately after the server FORM tag. You can use it to render any piece of HTML, a script block a link to an external script, a css file etc etc although it was intended for use with client scripts of course.
Code:
string script = "<script language=javascript src=YourFile.js></script>"
Page.RegsisterClientScriptBlock("scriptid", script);

Rob

Every gun that is made, every warship launched, every rocket fired, signifies in the final sense a theft from those who hunger and are not fed, those who are cold and are not clothed - Eisenhower 1953
 
I like Pauls way but a little different, I'd use
a System.Web.UI.WebControls.Literal. I believe I got the Literal idea from CrazyBoyBert.

in the aspx inbetween the HEAD tags
<asp:Literal id="myJavaScript" text="myJs" runat="server"></asp:Literal>

the in the code behind
protected System.Web.UI.WebControls.Literal myJavaScript;

then to add the js you can
myJavaScript.Text="<script language=\"javascript\" src=\"YourFile.js\"></script>"

as CrazyboyBert stated RegisterClientScriptBlock puts it after the FORM tag and you will not be able to use <body onLoad="jsFunction();"> and in your code behind you will not be able to add attributes of functions that live in your js file.
Button1.Attributes.Add("onClick", "hello();");
Marty
 
Thanks guys
Thanks to you i just found out my problem
the javascript was calling the window.onload to populate a user control with data pulled from the database.

Its time for a better approach

Thank you very much

Sammy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top