clientscript.RegisterStartupScript
In asp.net 1.1 I had no problems using this:
Page.RegisterStartupScript("ClientScript", strScript)
But as I moved to asp.net 2.0 I receive an error, it recommends me to use
ClientScript.RegisterStartupScript instead. Which arguments do I need to execute the same as in 1.1?
Re: clientscript.RegisterStartupScript
Did not get to .net 2 yet, but try Response.Write(myscript)
Re: clientscript.RegisterStartupScript
Although I see no documentation on it, I'd assume it's the same as Page.RegisterStartupScript().
Response.Write isn't a good idea in most cases...
Re: clientscript.RegisterStartupScript
Quote:
Originally Posted by mendhak
Although I see no documentation on it, I'd assume it's the same as Page.RegisterStartupScript().
Response.Write isn't a good idea in most cases...
I can swear it was you that recommended that to me....*scratching head*
Re: clientscript.RegisterStartupScript
You need to use ClientScriptManager.RegisterStartupScript, Page.RegisterStartupScript has been removed from .NET 2.0.
Re: clientscript.RegisterStartupScript
ok, but I can't type ClientScriptManager.RegisterStartupScript("ClientScript", strScript), what kind of arguments does this require?
Re: clientscript.RegisterStartupScript
You don't have to use ClientScriptManager, that was a Typo :blush:
Anyways here is an example
VB Code:
ClientScript.RegisterStartupScript(Me.GetType(), "ClientScript", "alert ('hello')", True)
Re: clientscript.RegisterStartupScript
Great, I will try that, thanks :wave:
Re: clientscript.RegisterStartupScript
Quote:
Originally Posted by StrangerInBeijing
I can swear it was you that recommended that to me....*scratching head*
I was the one who was totally against it. I must've given so many lectures on it that subject that you associated it with me. :D
Re: clientscript.RegisterStartupScript
Now I've tried the code
ClientScript.RegisterStartupScript(Me.GetType(), "ClientScript", "alert ('hello')", True)
but now it says
Code:
Error 442 Name 'ClientScript' is not declared. e:\Visual Studio 2005\WebSites\GF\Pressure1Abs.ascx.vb 383 9 e:\...\GF\
:confused:
Re: clientscript.RegisterStartupScript
Use
Code:
Page.ClientScript.RegisterStartupScript(Me.GetType(), "ClientScript", "alert ('hello')", True)
Re: clientscript.RegisterStartupScript
It doesn't work, nothing happens when I execute that code??? Does it work for you? I am using a beta of IE7, could it be that?
Re: clientscript.RegisterStartupScript
It doesn't work, because a ";" is missing after the JS-code. Now it works ;-). Thanks
Re: clientscript.RegisterStartupScript
Thank men
ClientScript.RegisterStartupScript(Me.GetType(), "ClientScript", "alert ('hello')", True)
Work...
yea
Re: clientscript.RegisterStartupScript
vb Code:
ScriptManager.RegisterStartupScript(Me, Page.GetType, "scriptkey", "yourScript();", True)
Use it all the time, just make sure you have a script manager on the page above it.
Re: clientscript.RegisterStartupScript
Hi -- My question is related to this thread. I'm using clientscript.RegisterStartupScript in a child page to register a script and have it execute after the page loads. The code is as follows
ClientScript.RegisterStartupScript(GetType(Page), "", "OnInitial();", True)
THe script OnInitial() is contained in an external file and it looks like this:
var nSubtopics;
var nTopics;
var aTopicText;
var aTopicValue;
var aSubtopicText;
var aSubtopicValue;
var nSelectedTopic;
var nSelectedSubtopic;
function OnInitial()
{
//debugger;;
var i, j, n, strTopic, strSubtopic;
nTopics =document.getElementById("_ctl0_ContentPlaceHolder1_ddlTopic").options.length;
aTopicText = new Array(nTopics);
aTopicValue = new Array(nTopics);
for (i=0; i< nTopics; i++)
{
aTopicText[i] = document.getElementById("_ctl0_ContentPlaceHolder1_ddlTopic").options[i].text;
aTopicValue[i] = document.getElementById("_ctl0_ContentPlaceHolder1_ddlTopic").options[i].value;
}
nSubtopics = document.getElementById("_ctl0_ContentPlaceHolder1_ddlSubtopic").options.length;
aSubtopicText = new Array(nSubtopics);
aSubtopicValue = new Array(nSubtopics);
for (i = 0; i < nSubtopics; i++) {
aSubtopicText[i] = document.getElementById("_ctl0_ContentPlaceHolder1_ddlSubtopic").options[i].text;
aSubtopicValue[i] = document.getElementById("_ctl0_ContentPlaceHolder1_ddlSubtopic").options[i].value;
}
nSelectedTopic = document.getElementById("_ctl0_ContentPlaceHolder1_ddlTopic").selectedIndex;
nSelectedSubtopic = document.getElementById("_ctl0_ContentPlaceHolder1_ddlSubtopic").selectedIndex;
if (nSelectedTopic == 0) {
document.getElementById("_ctl0_ContentPlaceHolder1_ddlSubtopic").options.length = 1;
}
else {
strTopic = document.getElementById("_ctl0_ContentPlaceHolder1_ddlTopic").options[nSelectedTopic].text;
n = strTopic.length;
j = 0;
for (i = 1; i < nSubtopics; i++) {
strSubtopic = aSubtopicValue[i];
if (strTopic != strSubtopic.substring(0, n))
{
document.getElementById("_ctl0_ContentPlaceHolder1_ddlSubtopic").options[i - j] = null;
j++;
}
}
}
}
function OnChangeTopic()
{ var i, j, n, strTopic, strSubtopic,strSubtopicVal;
if (nSelectedTopic != document.getElementById("_ctl0_ContentPlaceHolder1_ddlTopic").selectedIndex)
{
nSelectedTopic = document.getElementById("_ctl0_ContentPlaceHolder1_ddlTopic").selectedIndex;
strTopic = document.getElementById("_ctl0_ContentPlaceHolder1_ddlTopic")[nSelectedTopic].text;
document.getElementById("_ctl0_ContentPlaceHolder1_ddlSubtopic").options.length = 1;
if (nSelectedTopic != 0)
{
j = 1;
n = strTopic.length;
for (i = 1; i < nSubtopics; i++)
{
//strSubtopic = aSubtopicValue[i];
strSubtopic = aSubtopicText[i];
strSubtopicVal = aSubtopicValue[i];
if (strTopic == strSubtopic.substring(0, n))
{
document.getElementById("_ctl0_ContentPlaceHolder1_ddlSubtopic").options[j] = new Option(strSubtopic.substring(n + 1, strSubtopic.length), strSubtopicVal);
if (nSelectedSubtopic == i) {
document.getElementById("_ctl0_ContentPlaceHolder1_ddlSubtopic").selectedIndex = j;
}
j++;
}
}
}
}
}
function OnChangeSubtopic()
{ var i, n, strSubtopic;
n = document.getElementById("_ctl0_ContentPlaceHolder1_ddlSubtopic").selectedIndex;
if (n == 0)
{
nSelectedSubtopic = 0;
}
else
{
strSubtopic = document.getElementById("_ctl0_ContentPlaceHolder1_ddlSubtopic")[n].value;
for (i = 1; i < nSubtopics; i++)
{
if (strSubtopic == aSubtopicValue[i])
{
nSelectedSubtopic = i;
break;
}
}
}
}
<!--
function MM_reloadPage(init) { //reloads the window if Nav4 resized
if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);
//-->
In IE7 this works OK. In IE8/FIreFox it does not work. It t cannot find the script. I get an "Object Expected" error.
Any help would be appriciated.
Re: clientscript.RegisterStartupScript
Hello jliotine,
Welcome to the forums!!
A couple of points I think that need to be made.
First of all, when posting into the forum, can you remember to surround your code in [CODE][/CODE] tags, it makes it so much easier to read.
Secondly, reviving a thread that is almost two years old doesn't really make much sense, it might have been better to start your own thread.
So to save me digging through the JavaScript, can you give a brief description of what your function does? Do you know at which point the JavaScript is failing? Have you tried debugging your JavaScript to try and isolate where the problem is?
Gary
Re: clientscript.RegisterStartupScript
how does this thread have 22,000 views??
Re: clientscript.RegisterStartupScript
Quote:
Originally Posted by
jtm235
how does this thread have 22,000 views??
Because it was started in 2006 :)