Avatar of capsoftuk
capsoftukFlag for United Kingdom of Great Britain and Northern Ireland

asked on 

Page_Load function repeating twice not on postback

Hi there,

We're creating an application at the moment which is very close to going live, but we have a strange bug which is really causing hair to be torn out. Basically, this program runs through an initial process where the user moves through the initial stages, and then once information has been collected, the data is passed through to a secondary assembly page.

Once the assembly page has run through the code to assemble the document, it is redirected to a function called displayDocument() which executes specific code based on whether the document is to be assembled as a PDF or an RTF. Now, out of four possible outcomes (based on PDF/RTF and another variable) three of the outcomes work correctly.

However, the fourth one (when we try to write out an RTF) causes the Page_Load event to fire twice - ie it will assemble the document, save it and hit the response.End() line, but after that it will inexplicably return to the top of the page_load function and start over, producing a blank document with no answers.

I'm hugely confused by this as like i say, there are three outcomes which work fine, but this final one causes all the problems - I've attached the code snippet below which shows the code that's causing the problems, but if you need any more let me know. This snippet is at the end of an if statement and after that there is no more code - not sure if thats a problem or not.

Anyway, if anyone has any ideas, they would be gratefully recieved.

Cheers

Andy
else if (thePDF == false && theType == "generic")
            {
                string outputpath = System.Configuration.ConfigurationManager.AppSettings["genericoutputdocpath"];
                string fname = (new Random().Next()).ToString() + ".rtf";
                FileStream tw = new FileStream(outputpath + "\\" + fname, FileMode.Create);
                tw.Write(buffer, 0, buffer.Length);
                tw.Close();
                Response.ContentType = "application/msword";
                Response.AddHeader("content-length", buffer.Length.ToString());
                Response.BinaryWrite(buffer);
                //File.Delete(outputpath + "\\" + fname);
                Response.End();
            }

Open in new window

OS SecurityC#.NET Programming

Avatar of undefined
Last Comment
capsoftuk
Avatar of P_Ramprathap
P_Ramprathap
Flag of India image

Try using
Response.ContentType = "application/rtf";
 
Avatar of capsoftuk
capsoftuk
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

This is the error I get when I try to use that line of code:

The XML page cannot be displayed
Cannot view XML input using style sheet. Please correct the error and then click the Refresh button, or try again later.

--------------------------------------------------------------------------------

Invalid at the top level of the document. Error processing resource 'http://localhost/HDRequest/assembly.aspx?type=generic&...

{\rtf1\ansi\deflang2057\ftnbj\uc1\deff0
^
If anyone can shed any further light on this situation, I'd appreciate it :)

Cheers
Avatar of abel
abel
Flag of Netherlands image

Can you show what the "View page info" says about the document type? Do you have firebug, and if so, can you paste the HTTP headers here? As it seems, the data is correct, but the content type is not recognized...

If you click View Source in the browser, is it RTF code you see, or is it escaped RTF code?

Is it possible to see this live on a test website?
Avatar of capsoftuk
capsoftuk
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

No see the problem isn't to do with the content type, as we have basically two seperate scenarios, which are each split into PDF and RTF. So lets call them sc1 and sc2 - so sc1RTF and sc1PDF, if you get my drift. SC1RTF and SC1PDF work fine, as does SC2PDF. It is only the SC2RTF scenario which doesn't seem to want to comply, and even now, when the code has been changed so that there's no huge if statement, the last scenario does not want to comply - it fires the page_load function twice every single time, and there doesn't appear to be any way of stopping it.

I tried setting autoeventwireup to false, but the program wouldn't run then - there are no HTML or ASP controls of any kind in the aspx page, everything is completely taken care of by the CS file. There really shouldn't be any kind of double firing of events as there should only be one page load fire.

Anyway, I'm still stumped, I'm googling away but any inspiration would be much appreciated - sorry, no real chance of putting it up on a test server.

Cheers
protected void displayDocument(bool thePDF, string theType, byte[] buffer)
        {
 
            if (theType == "sc1")
            {
                outputpath = System.Configuration.ConfigurationManager.AppSettings["rmpoutputdocpath"];
            }
            else if (theType == "sc2")
            {
            outputpath = System.Configuration.ConfigurationManager.AppSettings["genericoutputdocpath"];
            }
        
            if (thePDF == true)
            {
                fname = (new Random().Next()).ToString() + ".pdf";
                Response.ContentType = "application/pdf";
            }
            else if (thePDF == false)
            {
                fname = (new Random().Next()).ToString() + ".rtf";
                Response.ContentType = "application/msword";
            }
 
            FileStream tw = new FileStream(outputpath + "\\" + fname, FileMode.Create);
            tw.Write(buffer, 0, buffer.Length);
            tw.Close();
 
            Response.AddHeader("content-length", buffer.Length.ToString());
            Response.BinaryWrite(buffer);
            //File.Delete(outputpath + "\\" + fname);
            Response.End();
 
        }

Open in new window

Avatar of abel
abel
Flag of Netherlands image

from the best of my knowledge, there's only one control that fires events twice: the dropdown box. This is a known ASP bug and the way microsoft sees it, it is a feature.

But that won't be of much help to you as you don't seem to have any controls whatsoever (like you said: all is taken care of in the code behind).
capsoftuk,

Please could you post the *entire* code of the page so that I can try to reproduce your bug?

MPB
Avatar of abel
abel
Flag of Netherlands image

So, I don't have a cause for you (though you could try to intercept the full HTTP request and see if there are differences), but I may have a solution, which is basically equal to the solution for the dropdownlist problem: create a session variable counter and use

if(SC2RTF)
{
   int bugFixCounter = Int32.ParseInt(Session("bugfixcounter")) ;
   bugFixCounter++;
   Session("bugfixcounter") = bugFixCounter;
   if(bugFixCounter % 2 == 0) {
      //do your SC2RTF here
   }
} else
   // all others

not pretty, I know, and scary because you don't know (yet) whether there are certain situations where the counter will not be hit twice. However, if someone clicks a link and nothing happens, they usually click again, so that might solve it for the moment.
ASKER CERTIFIED SOLUTION
Avatar of abel
abel
Flag of Netherlands image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of capsoftuk
capsoftuk
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

the behaviour was tested on several different machines and on some, it occurred, and on some it did not, so we're still working through on getting it fixed, but we don't believe its a code thing now. Thanks for the help abel
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo