Results 1 to 5 of 5

Thread: RegisterClientScriptBlock woes

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2001
    Location
    Yewston, Texis
    Posts
    240

    RegisterClientScriptBlock woes

    Anyone ever have problems with RegisterClientScriptBlock not working? I watch the code being executed in the debugger just as I expect it to, but my JavaScript never gets created--I don't see my alert come up, nor do I see any of the JavaScript code come up when I do a view source in the browser. Any ideas?

    Thanks,
    cudabean

    Code:
    		override protected void Page_Load(object sender, System.EventArgs e)
    		{
    			string strJScript = "<SCRIPT LANGUAGE=javascript>\n";
    			strJScript += "document.forms.dataForm.scrollPx.value = " + Session["patListContentScrollPos"] + ";";
    			strJScript += "alert('Attempting to scroll to " + Session["patListContentScrollPos"] + ");";
    			strJScript += "ListManDataTable.scrollTop = " + Session["patListContentScrollPos"] + ";";
    			strJScript += "</SCRIPT>";
    			if (!IsClientScriptBlockRegistered("restoreScrollState"))
    			{
    				Page.RegisterClientScriptBlock("restoreScrollState", strJScript);
    			} 
    			else 
    			{
    				strJScript = "SOL";
    			}

  2. #2
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    just out of curiousity...why are you overriding the Page_Load event?
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2001
    Location
    Yewston, Texis
    Posts
    240
    just out of curiousity...why are you overriding the Page_Load event?

    Well, when I just now removed the override to see what would happen, the event stopped firing completely.

    I didn't write that part of the code, (I just added to what somebody had already written) but when I remove the override, lots of things stop working. I think the deal is, Page_Load is a special name that's part of the codebehind. You are allowed to write your own Page_Load function and if you do that it won't interfere with the codebehind's separate Page_Load function, but when you use the override, the compiler knows that you are explitly trying to extend the functions of the Page_Load.

    cudabean

  4. #4
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Cudabean... two issues really...

    One:
    because you are using RegisterClientScriptBlock, you need to wrap those javascript calls inside a javascript function, that would be called by an event raised the browser. RegisterClientScriptBlock is used to add script that reacts within a loaded javascript document.

    Two:
    Therefore, if you mean to have this script run when the page itself first loads, you should use RegisterStartUpScript instead.

    Three:
    Execute the vb or C# code inside the PreRender method, not the Load( the MSDN example below uses the Load event, but I never had problems using PreRender)

    http://msdn.microsoft.com/library/de...criptTopic.asp
    Last edited by nemaroller; Jun 21st, 2004 at 04:21 PM.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Nov 2001
    Location
    Yewston, Texis
    Posts
    240
    Excellent, nemaroller!

    I was able to get what I had working. And yes I did have to wrap the javascript in a function. I wasn't aware of the RegisterStartupScript--I'll definitely study that. The key, however, was the Page_Load event started inserting script code into my html once I set the form on the page to be runat="server".

    Thanks,

    cudabean

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