trouble with inserting javascript

modularbeing

Regular
Joined
Jan 8, 2004
Messages
63
I created a usercontrol that inserts some javascript on the page which basically is an image rotator. I need to add the onload event to the body tag but I could not figure out how to do that through a usercontrol. I tried asigning the body tag an id and assigning it to a genericHTMLControl in code but that didnt work. any ideas on how to get this working?
 
in Html:
Code:
<body runat="server" id="TagBody">

in Code:
Code:
protected HtmlGenericControl TagBody;

private void Page_Load(object sender, System.EventArgs e)
{
	string _Script = @"<script>";
	_Script += @"function showMessage()";
	_Script += @"{";
	_Script += @"	alert('Hallo World!');";
	_Script += @"}";
	_Script += @"</script>";
	
	if(!this.IsStartupScriptRegistered("Message"))
	{				
		this.RegisterStartupScript("Message", _Script);
	}

	TagBody.Attributes.Add("onload","showMessage()");
}

When you have a big java function (a lot of text) use instead StringBuilder object.
:)

Greets
Adam
 
bungpeng said:
put that code outside the function may work

how do you mean outside the function? I think the reason I cant modify the body tag is because im trying to do that within the usercontrol and doesnt a usercontrol render before the parent page?
 
modularbeing said:
how do you mean outside the function? I think the reason I cant modify the body tag is because im trying to do that within the usercontrol and doesnt a usercontrol render before the parent page?

You want the onLoad event is because there are code you need to run before user trigger the event, right?

Browser run your javascript code in HTML from top to botton, so you can simply put:

Code:
<script language=javascript>
   // Code here will be run first
   var a = 1 + 2;

   function jsFunction() {
      // your other function code
   }
</script>

This is what I mean
 
Im inserting the javascript with the usercontrol. What I need to find out is how to modify the <body> tag in code to add the onLoad event to run the javascript code.
 
Back
Top