Creating JavaScript on ASPX page

  • Thread starter Thread starter Kent Ogletree
  • Start date Start date
K

Kent Ogletree

I need to get some JavaScript values onto an aspx page and I am not finding
anything using Google. (probably not stumbled onto the right keywords)

The code behind has the object that conatins the info I need to get onto the
page. In old asp I would usually do:

var someStringValue = "<% = MyStringVar %>";

However everything I try right now has done nothing more than break the
page.

Any pointers whould be greatly appreciated.

Kent
 
Here is an example using "RegisterStartupScript" there are also others such
as
this.Page.RegisterClientScriptBlock

this.Page.RegisterArrayDeclaration

here is the example
private void LoadWorkTypeLabel()

{

System.Text.StringBuilder sBuild = new System.Text.StringBuilder();

sBuild.Append("<script language=javascript>\n");

sBuild.Append("<!--\n");

sBuild.Append("LoadWorkType(true)\n");

sBuild.Append("//-->\n");

sBuild.Append("</script>\n");

this.Page.RegisterStartupScript("JR_LoadWorkTypeLabel",sBuild.ToString());

}
 
Hi Kent,

Try page.registerclientscriptblock({scriptname},{scriptstring})

{scriptname} is anything you want to call the script
{scriptstring} example would be "var someStringValue = '" & MyStringVar &
"';"

You insert this on the page.load event, and depending on whether you are
using viewstate, you may also want to read up on the
IsClientScriptBlockRegistered to ensure you are not needlessly reinserting
the script.

Cheers

Chris
 
Back
Top