Does the ASP.Net Panel client side onLoad work?

G

Guest

I'm trying to create a web page and I need a javascript function to be called
on the load of a particular panel. (The panel is hidden during some but not
all postbacks.)

The function is bound to the panel in VB as follows:

pnlAdd.Attributes.Add("onLoad", "SetupScreenLite()")

This is basically how we attach all functions in our projects but this is
the first time I've worked with onLoad and the first time I've tried to
attach anything to a panel. Any ideas?
 
C

Chris R. Timmons

I'm trying to create a web page and I need a javascript function
to be called on the load of a particular panel. (The panel is
hidden during some but not all postbacks.)

The function is bound to the panel in VB as follows:

pnlAdd.Attributes.Add("onLoad", "SetupScreenLite()")

This is basically how we attach all functions in our projects
but this is the first time I've worked with onLoad and the first
time I've tried to attach anything to a panel. Any ideas?


The panel control generates a <DIV> tag. That tag does not support
the onload event:

http://msdn.microsoft.com/workshop/author/dhtml/reference/objects/div.asp

In fact, the onload event is valid for only a small selection of
HTML tags:

http://msdn.microsoft.com/workshop/author/dhtml/reference/events/onload.asp

Using the page's RegisterStartupScript method may be an
alternative way to achieve your goal.
 
G

Guest

Ok, the problem is this. I am a relative beginner in Javascript. I am
trying to develop an ASP.net 1.1 web page. I need a way for a Javascript
function to fire once after the load of the page into the browser. What are
my options?
 
G

Guest

add the onload attribute to the page's BODY tag:

<BODY onload="myJsFunction();" >

Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com




B. Chernick said:
Ok, the problem is this. I am a relative beginner in Javascript. I am
trying to develop an ASP.net 1.1 web page. I need a way for a Javascript
function to fire once after the load of the page into the browser. What are
my options?
 
C

Chris R. Timmons

Ok, the problem is this. I am a relative beginner in
Javascript. I am trying to develop an ASP.net 1.1 web page. I
need a way for a Javascript function to fire once after the load
of the page into the browser. What are my options?

As Peter noted, the onload attribute of the <BODY> tag can be
used, as can the RegisterStartupScript method.

Personally, I prefer to use RegisterStartupScript. It's
possible that some browsers have buggy implementations of
onload, plus I can insert as much JavaScript as I want
with RegisterStartupScript. JavaScript inserted into the
onload attribute is realistically limited to method calls and
short segments of code that can be expressed in one line.

More info on client-side scripting w/ ASP.Net:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/clientsidescript.asp
 
G

Guest

You refer to 'buggy implementations of onload'.

Could this, in practice, result in one or more web controls appearing as
nulls (and crashing the script) when the onload function fires, even if the
controls have been 'wired' into the Javascript in a conventional fashion?
(i.e. document.getElementById("<%= {control-name}.ClientID %>");
 
G

Guest

A worse problem, or perhaps I have misstated the problem. I was testing with
the body onload event and it appears that this event only fires once on
initial load and does not fire on postbacks. I was assuming otherwise. I
need an event that fires on every postback.
 
C

Chris R. Timmons

You refer to 'buggy implementations of onload'.

Could this, in practice, result in one or more web controls
appearing as nulls (and crashing the script) when the onload
function fires, even if the controls have been 'wired' into the
Javascript in a conventional fashion? (i.e.
document.getElementById("<%= {control-name}.ClientID %>");

I don't think so. Is that line of JavaScript before or after the
control in question? The control needs to appear before the
JavaScript that accesses it so the browser can put the control in the
page's document object model (DOM) tree.

For example, this HTML will generate an "object required" error
message, because the <INPUT> control comes after the JavaScript
that's trying to access that control's value:

<HTML>
<HEAD>
</HEAD>
<BODY>
<SCRIPT>
var value = document.getElementById("myInputBox").value;
alert(value);
</SCRIPT>
<INPUT type="text" id="myInputBox" name="myInputBox"
value="Hello, world!"/>
</BODY>
</HTML>

But placing the <INPUT> control before the JavaScript will work:

<HTML>
<HEAD>
</HEAD>
<BODY>
<INPUT type="text" id="myInputBox" name="myInputBox"
value="Hello, world!"/>
<SCRIPT>
var value = document.getElementById("myInputBox").value;
alert(value);
</SCRIPT>
</BODY>
</HTML>

This is where the RegisterStartupScript method comes in handy. It
always places the JavaScript at the very end of the HTML, just before
the closing </FORM> tag.

If that's not the problem, what might be happening is either "name
mangling" of the ClientID by ASP.Net, or a known bug in .Net 1.1:

http://support.microsoft.com/default.aspx?id=818803

Concerning name mangling, when multiple instances of the same user
control are present on a page, they cannot all have the same ID in
the generated HTML page. Therefore, ASP.Net gives each instance of
the control a unique name when it generates the HTML.

A control named BlogSideBar1, with an embedded textbox named
SearchBox, would be referenced by C#/VB in an .ascx file as
BlogSideBar1.SearchBox. However, in the HTML it would appear as
_ctl0____ctl0___BlogSideBar1___SearchBox.

Your line of code should work, depending on when it's executed in the
page's life cycle on the server. If there are multiple instances of
"control-name" in your page, that situation might be causing some
kind of problem.

I generally don't put inline <% %> tags in my pages, except for
mundane things like the page's title or meta tags. I prefer to build
the JavaScript, complete with ClientIDs, in my code behind files.
That way I feel I have better control over exactly what gets sent to
the browser, and where it's placed in the page.
 
C

Chris R. Timmons

A worse problem, or perhaps I have misstated the problem. I was
testing with the body onload event and it appears that this
event only fires once on initial load and does not fire on
postbacks. I was assuming otherwise. I need an event that fires
on every postback.

Are you assigning the value to the onload event in the page's
Page_Load event? If so, is that assignment in an if/then statement
that uses IsPostBack?

It sounds like what's happening is this:

::In the Page_Load event::

if not IsPostBack then
assign value to <BODY> onload
end if

The fix is to move the onload assignment outside of that if/then
block, so it's always executed when the page is initially created,
and after every postback.
 
G

Guest

Thanks, but help me out a little more (Javascript beginner, remember?) I'm
drawing a blank.

When you say 'assign value to <BODY> onload' , are you referring to
something like RegisterStartUpScript or is this yet another Javascript
technique I haven't seen yet?
 
G

Guest

On second thought, are you refering to giving the body an ID, declaring it in
the codebehind, and then doing an <idname>.Attributes.Add("onLoad" etc?

Just found an example and I can see it in the HTML source but it doesn't fire.
 
G

Guest

Sorry about the number of appends. I moved my code up in the Page_Load
codebehind so that it always gets called but the HTML code only fires the
first time, never on postback. (but I can still see it in the HTML source)
 
C

Chris R. Timmons

Sorry about the number of appends. I moved my code up in the
Page_Load codebehind so that it always gets called but the HTML
code only fires the first time, never on postback. (but I can
still see it in the HTML source)

That sounds strange. Could you post some code that demonstrates the
problem?

Thanks.

Chris.
 
C

Chris R. Timmons

On second thought, are you refering to giving the body an ID,
declaring it in the codebehind, and then doing an
<idname>.Attributes.Add("onLoad" etc?

Yes.

The body tag should look something like this in the .aspx file:

<BODY runat="server" id="body">

Declare it as an HtmlControl in the code behind file:

protected System.Web.UI.HtmlControls.HtmlGenericControl body;

And the onload attribute can be assigned like this in the Page_Load
event handler:

this.body.Attributes["onload"] = "alert('Hello, world!');";
Just found an example and I can see it in the HTML source but it
doesn't fire.

Does the body tag have the runat="server" attribute?
 
G

Guest

Not much to show.

In my aspx file I have <body id="body1" runat="server" >

In my vb file I have the declaration: ' Protected WithEvents body1 As
HtmlGenericControl' (I have tried this both with and without the WithEvents
declaration. It apparently has no effect on results.)

In the page_load (above the usual test for postback, so it's always called)
I have: 'body1.Attributes.Add("onLoad", "alert('test');")'

In the HTML source of the postback screen I see: '<body id="body1"
onLoad="alert('test');">'

But it never fires except on the very first time.
 
G

Guest

B. Chernick said:
Not much to show.

In my aspx file I have <body id="body1" runat="server" >

In my vb file I have the declaration: ' Protected WithEvents body1 As
HtmlGenericControl' (I have tried this both with and without the WithEvents
declaration. It apparently has no effect on results.)

In the page_load (above the usual test for postback, so it's always called)
I have: 'body1.Attributes.Add("onLoad", "alert('test');")'

In the HTML source of the postback screen I see: '<body id="body1"
onLoad="alert('test');">'

But it never fires except on the very first time.

I'm pretty much out of ideas. If possible, could you post a complete .aspx
page that demonstrates the problem?

Thanks.
 
G

Guest

I should apologize for wasting your time. I've been looking in the wrong
place. If you set up a test screen with only the code I have described it
works perfectly every time. I suspect that the third party (Telerik)
controls we're using are somehow interfering with the postback.
 
C

Chris R. Timmons

I should apologize for wasting your time. I've been looking in
the wrong place. If you set up a test screen with only the code
I have described it works perfectly every time. I suspect that
the third party (Telerik) controls we're using are somehow
interfering with the postback.

No problem.

Chris.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,807
Latest member
ryef

Latest Threads

Top