|
-
Jun 18th, 2004, 12:25 PM
#1
Thread Starter
Addicted Member
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";
}
-
Jun 18th, 2004, 01:02 PM
#2
Frenzied Member
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
-
Jun 18th, 2004, 01:40 PM
#3
Thread Starter
Addicted Member
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
-
Jun 21st, 2004, 04:12 PM
#4
I wonder how many charact
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.
-
Jun 23rd, 2004, 09:11 AM
#5
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|