Results 1 to 19 of 19

Thread: clientscript.RegisterStartupScript

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049

    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?

  2. #2
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    Re: clientscript.RegisterStartupScript

    Did not get to .net 2 yet, but try Response.Write(myscript)

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    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...

  4. #4
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    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*

  5. #5
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: clientscript.RegisterStartupScript

    You need to use ClientScriptManager.RegisterStartupScript, Page.RegisterStartupScript has been removed from .NET 2.0.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049

    Re: clientscript.RegisterStartupScript

    ok, but I can't type ClientScriptManager.RegisterStartupScript("ClientScript", strScript), what kind of arguments does this require?

  7. #7
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: clientscript.RegisterStartupScript

    You don't have to use ClientScriptManager, that was a Typo

    Anyways here is an example
    VB Code:
    1. ClientScript.RegisterStartupScript(Me.GetType(), "ClientScript", "alert ('hello')", True)
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049

    Re: clientscript.RegisterStartupScript

    Great, I will try that, thanks

  9. #9
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    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.

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049

    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\

  11. #11
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: clientscript.RegisterStartupScript

    Use
    Code:
    Page.ClientScript.RegisterStartupScript(Me.GetType(), "ClientScript", "alert ('hello')", True)
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  12. #12

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049

    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?

  13. #13

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049

    Re: clientscript.RegisterStartupScript

    It doesn't work, because a ";" is missing after the JS-code. Now it works ;-). Thanks

  14. #14
    New Member
    Join Date
    Jul 2008
    Posts
    1

    Re: clientscript.RegisterStartupScript

    Thank men

    ClientScript.RegisterStartupScript(Me.GetType(), "ClientScript", "alert ('hello')", True)

    Work...

    yea

  15. #15
    Lively Member
    Join Date
    Oct 2006
    Posts
    86

    Re: clientscript.RegisterStartupScript

    vb Code:
    1. 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.
    Visual Studio 2005 - Visual Basic and C#
    Love that SQL

  16. #16
    New Member
    Join Date
    Jul 2010
    Posts
    1

    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.

  17. #17
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

  18. #18
    Lively Member
    Join Date
    Dec 2009
    Posts
    116

    Re: clientscript.RegisterStartupScript

    how does this thread have 22,000 views??

  19. #19
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: clientscript.RegisterStartupScript

    Quote Originally Posted by jtm235 View Post
    how does this thread have 22,000 views??
    Because it was started in 2006

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width