Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / February 2006

Tip: Looking for answers? Try searching our database.

Does the ASP.Net Panel client side onLoad work?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
B. Chernick - 19 Feb 2006 03:24 GMT
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?
Chris R. Timmons - 19 Feb 2006 04:57 GMT
"=?Utf-8?B?Qi4gQ2hlcm5pY2s=?="
<BChernick@discussions.microsoft.com> wrote in
news:DEFBCC18-E3D7-4317-9CD0-0C5C159E0306@microsoft.com:

> 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
[quoted text clipped - 7 lines]
> 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.

Signature

Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/

B. Chernick - 19 Feb 2006 14:46 GMT
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?

> "=?Utf-8?B?Qi4gQ2hlcm5pY2s=?="
> <BChernick@discussions.microsoft.com> wrote in
[quoted text clipped - 24 lines]
> Using the page's RegisterStartupScript method may be an
> alternative way to achieve your goal.
Peter Bromberg [C# MVP] - 19 Feb 2006 15:31 GMT
add the onload attribute to the page's BODY tag:

<BODY onload="myJsFunction();" >

Peter

Signature

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

> 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
[quoted text clipped - 29 lines]
> > Using the page's RegisterStartupScript method may be an
> > alternative way to achieve your goal.
Chris R. Timmons - 19 Feb 2006 17:35 GMT
"=?Utf-8?B?Qi4gQ2hlcm5pY2s=?="
<BChernick@discussions.microsoft.com> wrote in
news:B30FEEFD-F220-4B26-B700-FF2E36DD97A8@microsoft.com:

> 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/cli
entsidescript.asp


Signature

Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/

B. Chernick - 20 Feb 2006 13:56 GMT
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 %>");   

> "=?Utf-8?B?Qi4gQ2hlcm5pY2s=?="
> <BChernick@discussions.microsoft.com> wrote in
[quoted text clipped - 18 lines]
>
>   http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/cli
entsidescript.asp
Chris R. Timmons - 21 Feb 2006 00:24 GMT
"=?Utf-8?B?Qi4gQ2hlcm5pY2s=?="
<BChernick@discussions.microsoft.com> wrote in
news:86922A80-B315-45D3-A6E1-3242DBAC6DD6@microsoft.com:

> You refer to 'buggy implementations of onload'.  
>
[quoted text clipped - 3 lines]
> 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.

Signature

Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/

B. Chernick - 20 Feb 2006 14:19 GMT
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.

> "=?Utf-8?B?Qi4gQ2hlcm5pY2s=?="
> <BChernick@discussions.microsoft.com> wrote in
[quoted text clipped - 18 lines]
>
>   http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/cli
entsidescript.asp
Chris R. Timmons - 21 Feb 2006 00:30 GMT
"=?Utf-8?B?Qi4gQ2hlcm5pY2s=?="
<BChernick@discussions.microsoft.com> wrote in
news:E27AAD44-B94B-4FCF-A852-ACF316F1F162@microsoft.com:

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

Signature

Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/

B. Chernick - 21 Feb 2006 02:13 GMT
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?

> "=?Utf-8?B?Qi4gQ2hlcm5pY2s=?="
> <BChernick@discussions.microsoft.com> wrote in
[quoted text clipped - 21 lines]
> block, so it's always executed when the page is initially created,
> and after every postback.
B. Chernick - 21 Feb 2006 02:49 GMT
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.

> "=?Utf-8?B?Qi4gQ2hlcm5pY2s=?="
> <BChernick@discussions.microsoft.com> wrote in
[quoted text clipped - 21 lines]
> block, so it's always executed when the page is initially created,
> and after every postback.
Chris R. Timmons - 21 Feb 2006 10:51 GMT
"=?Utf-8?B?Qi4gQ2hlcm5pY2s=?="
<BChernick@discussions.microsoft.com> wrote in
news:334D5DBB-CB22-4812-8C3D-A0718B398D49@microsoft.com:

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

Signature

Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/

B. Chernick - 21 Feb 2006 02:56 GMT
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)

> "=?Utf-8?B?Qi4gQ2hlcm5pY2s=?="
> <BChernick@discussions.microsoft.com> wrote in
[quoted text clipped - 21 lines]
> block, so it's always executed when the page is initially created,
> and after every postback.
Chris R. Timmons - 21 Feb 2006 10:44 GMT
"=?Utf-8?B?Qi4gQ2hlcm5pY2s=?="
<BChernick@discussions.microsoft.com> wrote in
news:C69FDD32-4479-4F5D-A788-E7285BAAD824@microsoft.com:

> 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.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
B. Chernick - 21 Feb 2006 14:08 GMT
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.

> "=?Utf-8?B?Qi4gQ2hlcm5pY2s=?="
> <BChernick@discussions.microsoft.com> wrote in
[quoted text clipped - 14 lines]
> C.R. Timmons Consulting, Inc.
> http://www.crtimmonsinc.com/
Chris R. Timmons - 21 Feb 2006 17:20 GMT
> Not much to show.
>
[quoted text clipped - 11 lines]
>
> 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.

Signature

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/

B. Chernick - 21 Feb 2006 18:30 GMT
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.

> > Not much to show.
> >
[quoted text clipped - 16 lines]
>
> Thanks.
Chris R. Timmons - 21 Feb 2006 23:35 GMT
"=?Utf-8?B?Qi4gQ2hlcm5pY2s=?="
<BChernick@discussions.microsoft.com> wrote in
news:381809A1-D4BF-42A6-8D9A-A90EE20196ED@microsoft.com:

> 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.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.