js script block outside form tags...

  • Thread starter Thread starter Alex Wagner
  • Start date Start date
A

Alex Wagner

I'm trying to programmatically write a javascript script block *outside* the
<form> tag. Both RegisterClientScriptBlock and RegisterStartupScript will
render them inside... how can I do that?

Thanks
 
Create an HtmlGenericControl, by inserting an HTML element (such as a div)
with runat=server where you want the script to appear, and code an
HtmlGenericControl in the CodeBehind to manipulate the Html element. Then
you can set the InnerHtml of the HTML element to whatever you like.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
Kevin, thanks for your reply. it didn't work as the javascript block didn't
allow to be inside a div or span or anything else for that matter... so,
what I did was:
programmatically created an HtmlGenericControl and set IT's InnerHtml to my
string variable. than I simply added the control to the page's control
collection.

see, similar to yours but even simpler. Thanks, though, as your approach
allowed me to arrive at this solution!
 
Excellent, Alex!

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
I have been having a similar problem. I have written some client-side
script to set page focus and window scoll on postback. The script
needs to be written at runtime as the focus depends on the control
that caused the postback.

It seems the script will only work if it is placed within the <body>
tags but outside of the <form> tags. That is where my problem lies;
programmatically created an HtmlGenericControl and set IT's InnerHtml to
my
string variable. than I simply added the control to the page's control
collection.

However I found that this placed the Control outside the <html> tags
of the page and it didn't work. I'm pasting the C# code from the
code-behind file I used below:

string strScript = "<script language=jscript> ...";
ctlGeneric = new HtmlGenericControl();
ctlGeneric.InnerHtml = strScript;
Page.Controls.Add(ctlGeneric);

Perhaps someone could help me see where I've gone wrong?

Thanks,
Joanne
 
Back
Top