BUG: Setting Server Control's InnerHtml forces Constructore to be called.

G

Guest

While creating a shopping cart application I noticed a strange bug which resulted in the Constructor and everything being called twice. I was using Inherited classes ClassShowProducts inherited TemplatePage which inherited System.Web.UI.Page which I thought was the problem so I spent all day recoding everything so that ClassShowProducts was inherited from System.Web.UI.Page. Then I found out that it still happend.
I then quickly narrowed it down to one line of code in Page_Load(

i have a server control <td id="tdMainContent" runat="server"> and in my Page_Load i go
tdMainContent.InnerHtml = render_MainBody(); // returns a massive string

if i go string MainContent = render_MainBody(); it works fine
however the minute I try setting tdMainContent.InnerHtml it borks and basically when Page_Load ends the class constructor is called and goes through everything again, Class Constructor, OnInit, InitializeComponent, Page_Load etc.

The Length of the string is 4927 bytes and is Valid HTML Content.
After doing some tdMainBodyContent.InnerHtml = string('c', randomlenghs);
it does not appear to be "content length" related I am thinking it may be related to complexity of the structure of the HTML. I have checked the HTML and it is W3C Valid and since it is generated by XML/XSLT i know that there will not be any missing/open tags . I am at a loss
 
G

Guest

From the looks of it it appears that the problem ties into me doing
baseUrl.Attributes["href"] = Constants.urlDomain
If i comment out that line in my web project and set InnerHtml of the tdMainDody then it works without causing the constructor to be thrown. However this is not reproduceable in a test project that i made that has the exact same code except is all by itself instead of in the massive project i am in.


----- Natty Gur wrote: ----

Hi

I try it with simple table but I can't reproduce the described behavior
Can you send me sample code

Natty Gur[MVP

blog : http://weblogs.asp.net/ngu
Mobile: +972-(0)58-88837


*** Sent via Developersdex http://www.developersdex.com **
Don't just participate in USENET...get rewarded for it
 
Y

Yan-Hong Huang[MSFT]

Hello John,

I couldn't repro it as Natty either. Generally speaking, it is quite hard
to isolate the root cause due without a repro sample. For troubleshooting
this type of issue, you may need use some debugging dump to check call
stack when it is constructed again. Anyway, I am glad to see that you have
found the exact line which cause the problem. If there is any we can do for
you, please feel free to post here.

Thanks.

Best regards,
Yanhong Huang
Microsoft Community Support

Get Secure! ¨C www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Actually I did not find the root problem. Well I sort of did.
I more found a way to reproduce it every single tim
I have rebuilt over 7 different projects and recoded another one racking my brain trying to figure it out but at least I think i can now reproduce it as i've created 2 new projects and gotten it to exhibit this behavior. I will be posting a sample in just a second.

It seems to be tied to the fact that I have a class named _Default because one of my webpages is Default.asp
For example i did thi

Created a New Web Project -> Copied the Sample code. added a constructor and set a breakpoint on the constructor
Ran the project and it only got called once
So i went (because i already knew this was reproduceable) and added a new page called Default.aspx and set it as the Startup Page. I added the same code and created a constructor and set a breakpoint. Running the project prove that the constructor got called a second time.

Now because my last project was using an Inherited Page Structure this was happening on every single page. So i tested something by setting WebForm1.aspx as the startup page and ran it with breakpoints on both constructors
Webform1.aspx got called and a breakpoint happned in Webform1's Constructor. i hit f5 and then the the next breakpoint popped on Default's Constructor even though Webform1.aspx page was the Startup page.

Is there something I am missing that is special about "Default.aspx" ?
 
G

Guest

What seems to be even funnier is that after one has added a Web Form via Project -> Add New Item -> Webform -> Default.aspx -> and then renamed it appears that this still happens.

I will post sample files under a different ID as I can't attach emails via the web interface


----- (e-mail address removed) wrote: ----

Actually I did not find the root problem. Well I sort of did.
I more found a way to reproduce it every single tim
I have rebuilt over 7 different projects and recoded another one racking my brain trying to figure it out but at least I think i can now reproduce it as i've created 2 new projects and gotten it to exhibit this behavior. I will be posting a sample in just a second.

It seems to be tied to the fact that I have a class named _Default because one of my webpages is Default.asp
For example i did thi

Created a New Web Project -> Copied the Sample code. added a constructor and set a breakpoint on the constructor
Ran the project and it only got called once
So i went (because i already knew this was reproduceable) and added a new page called Default.aspx and set it as the Startup Page. I added the same code and created a constructor and set a breakpoint. Running the project prove that the constructor got called a second time.

Now because my last project was using an Inherited Page Structure this was happening on every single page. So i tested something by setting WebForm1.aspx as the startup page and ran it with breakpoints on both constructors
Webform1.aspx got called and a breakpoint happned in Webform1's Constructor. i hit f5 and then the the next breakpoint popped on Default's Constructor even though Webform1.aspx page was the Startup page.


Is there something I am missing that is special about "Default.aspx" ?
 
S

Steven Cheng[MSFT]

Hi,


Thanks for your responses and the sample project you've attached. I've
tested on our application and here is the result I've got:
#I've added break point in all the page classes' constructor(default,
WebForm1 and WebForm2) and set them as startup page separatly. However, I
found that each page's aspx has the below code
<body onload="bodyOnLoad();">
but the "bodyOnLoad()" client script functdion is not found in aspx page so
that everytime after the page's constructor(first time), the will throw a
"no Object Reference error" which indicate that the BodyOnLoad function not
defined. Would you please provide the "bodyOnLoad()" function so that I can
contintue the test?

In addition, if I remove the onload="bodyOnLoad();" block, each page is ok
to run under F5 debug , but each time their construct will be called only
once rather than second time. So I also doubt whether the problem is due to
the "bodyOnLoad()" function ?

Please check out the above things. If you have any new findings, please
feel free to post here.


Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
G

Guest

the onload="Onload..." has nothing to do with that. That was client side javascript left over from the project that got messed up. According to my discovers as stated before it appears that naming a class _default is causing the constructor to be called twice.
In the attached project or by going
File -> New Web Project ->
Switch to HTML view and copy the HTML code provided in my project - remove the onload so you do not get JS errors.
Switch back to Design view and double click and then copy the code i provided in the Page_Loa
set a breakpoint here or create a constructor and put a breakpoint in there.

Then Project -> Add New Page -> Default.aspx
Repeate the above steps and then set it up as the Start page
On every single new project that I created the minute I did this whenever i ran the project the constructor was called twice.

And as I described before renaming the class to something normal does not rectify the problem. I had to manually close the project and create a new one in order to get it working.

(I can't believe it is not reproduceable as i've been able to do it every single time once i figured it out exactly what was causing the problem) namely having a web form called Default.aspx which would create a class called _Default
 
G

Guest

Weee. 5th project wasted. 3rd day wasted. I am not sure how this is not coming up as reproduceable as i've laid waste to many hours rebuilding projects only to have it show back up. I think this time it showed up by me just renaming Webform1.aspx to a Default.aspx while leaving the class name the same. Going to spend a few more hours destroying and rebuilding from scratch again
 
S

Steven Cheng[MSFT]

Hi,


I'm viewing the issue and found that this is the same one with another
thread subjected:
BUG: Setting Server Control's InnerHtml forces Constructore to be called.
I've repro your problem and found the root cause of the issue. Also, I've
posted my reply in that one. Please check out it and feel free to post
there if you have any questions on it. Thanks.



Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
G

Guest

Okay. That sound's reasonable. So hopefuly if it comes back i should just
look for an <img tag without any empty src specified. I wish i would have
seent he pattern before. Thanks for everything
 

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,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top