An aspx page is called sometime and some time not - asp.net

I am using a generateimage.aspx page which is used as an image source for an image.
When this page is called I am passing a querystring and then using a Session["abc"] var whose value is being returned as a jpg image.
The page is called as
Context.Items["display"]="img src=" + Context.Items["Ch_BaseSvr"] +
"GenerateImage.aspx?text=P"
Now the problem is that sometimes this page is called and sometimes not.
Thus, when the page is not called the image value that is being returned is that which is assigned to the var in previous Session.
Please let me know what might be the reason that the page is called sometimes and sometimes not.

Sounds like it may be cached. Try setting
Response.Cache.SetCacheability(HttpCacheability.NoCache)
on your generateimage.aspx page.

Related

HTML to PDF: Avoid having the download button in the downloaded PDF

I have an ASP.NET page that generates a document for the user based on parameters in the URL. At the bottom of the document is a button that says "Download to PDF".
To implement that feature, I am using ABCPdf. I just feed my web page to the library and it spits out a PDF, and it works very well. Almost TOO well in fact - because the PDF includes the "Download" button itself. How can I include my entire page EXCEPT that specific button?
Here is what I have tried:
In the AddImageToURL call, I added a parameter "&pdfmode" to the URL. Then in my page load, I check for that parameter. If it is there, I say "btnDownloadPDF.Visible=False". This has no effect. I tried a similar approach by checking the page.request.form arguments to detect whether the page is posting back because of a click of the button.
'This does not work; the PDF still includes the download button
Dim pbcontrol = GetPostBackControl(Page)
Dim pdfMode As Boolean =
(pbcontrol IsNot Nothing AndAlso pbcontrol.ID = "btnDownloadPDF") _
OrElse (Not String.IsNullOrEmpty(Context.Request.QueryString("pdfmode")))
If pdfMode Then
btnDownloadPDF.Visible = False
End If
Hum, we probably need some details of how, and when the page is sent to that PDF system.
I suspect that that system takes the page URL and re-loads it?
Or does some code feed the PDF system a existing page class in code?
This suggests?
Hence on your existing page, you need a session() value, (or hidden field) and you have to set that persisted value to "hide" the button.
Then in your on-page load event, check for that session() value, and simple go mybutton.visible = false. So, that external library probably is feed a page url, and it re-renders the whole page - so in page on-load, you need to hide the button?
It just not clear how you hiding that button, but in your code behind, that may well not suffice, since the PDF library is re-loading its OWN copy of the page - hence you need code in the on-load event to hide the button.

asp.net bug? ResouceManager.GetString(string) when string not found, aspx file is executed twice

I tracked down a very weird ... bug...
I found that for some reason, an ASPX page always executed twice.
I tracked it down to this line in a user (asxc) control, I had :
<img src='<%=RS("buildhover")%>' />
RS is just a helper function that resolves to ResouceManager.GetString("buildhover")
I found that "buildhover" was simply missing from the resx file that was being read. When added, the ASPX page is no longer run twice...
This is very strange and since I use resource files extensively, I am really interested to find out why this is...
When you have an image element with a blank url for the string then it makes a request to the current page. When the resource doesn't exist you get a blank string. So the result of ResouceManager.GetString("buildhover") is an empty string.
Look at the html produced. You will have something like <img src="" />
If you are observing load event twice in a post back in ASP.Net page, check the following:
1.If Page_Load handler defined in Codebehind then AutoEventWireup property should be “false”
Check if you have mistakenly multiple event handlers registered for an event
Src or ImageURL attribute of your image control are defined and not empty (you can put %20 for blank)
bgColor or background is empty
Last two problems usually appears in one browser while disappears in other.
http://devshop.wordpress.com/2008/06/02/aspnet-page-loading-twice/

ASP.NET - how to change requested page names transparently?

Very new to this subject and under the gun to come up with a solution. The problem is how I could load one of several versions of the same ASPX page, for any given page. E.g. unknown to the unsuspecting user who requests catalog.aspx, I would actually serve one of catalog_1.aspx, catalog_2.aspx, or catalog_3.aspx, etc.
Strange request indeed. It's due to an inherited decade-old product having inlined styles all over the ASPXs. Instead of re-writing the hundreds of ASPXs to be flexible, I'm trying to regexp-replace them to get versions suitable for various screen sizes. I'd then choose the best one after measuring window size at user login (and perhaps store the size in a cookie).
I thought this would involve some lower level object like an http handler. Close?
LJ
Update: I ended up doing this through url rewriting which works much better. The easiest place to do this in asp.net is apparently global.asax, and under Application_BeginRequest event. Call context.RewritePath(newpath, False) to send the request to a different page than requested.
In the way I did it, the destination page can change from request to request, and that apparently upsets postbacks, if the recipient of the postback isn't the exact version of the page that generated the viewstate. I tried to turn off viewstate validation but didn't help. So had to prevent flipping between versions once a user's logged in. Hope this helps someone.
Server.Transfer is probably the quickest way of doing just that.
string TransferTo = string.Empty;
if( Something )
TransferTo = "catalog_1.aspx";
else if( SomethingElse )
TransferTo = "catalog_2.aspx";
else
TransferTo = "catalog_3.aspx";
Server.Transfer( TransferTo, false );
Documentation
Note
If the subsequent pages have postback controls on them, they will reveal the true URL of the page at that point. If that matters, then this method will not work.
I don't like this method, but maybe you could use a full-window IFRAME to hold the appropriate page - catalog.aspx would be nothing but a big frame, and you could set the source of that frame in your codebehind.

Image handler page

I created an image handler page which retrieves the physical path of an image on the local machine and then using filestream, resizes and displays it - using a integer (record id) passed as querystring.
What is happening is that, when the routine in pageload cannot find an image relating to the record id, it displays random images (from other records).
This only ocurrs when the related record id has no image. The routine assigns a default image if no image exists for the record, but instead of displaying the default image, the page is displaying images from other records. If I keep refreshing the page it displays different images from other records.
In my page load event, before doing anything else I have put:
Response.Cache.SetCacheability(HttpCacheability.NoCache)
But this has not changed the behaviour.
Any help appreciated.
thanks,
KS
You could handle the case in code so when no image is present, you display a static blank image instead.

Is there any way to modify query strings without breaking an ASP.Net postback?

From reading here and around the net, I'm close to assuming the answer is "no", but...
Let's say I have an ASP.Net page that sometimes has a query string parameter. If the page has the query string parameter, I want to strip it off before, during, or after postback. The page already has a lot of client-side script (pure JavaScript and jQuery).
As an example, say I load:
http://myPage.aspx?QS=ABC
The QS parameter is necessary to control what appears on the page when it first loads and is set by the page that "calls" it. myPage.aspx has form elements that need to be filled in and has a submit button that does a postback. When the page completes the postback, I need to returned URL to be:
http://myPage.aspx
in order to avoid the client-side code that is called when the query string is present. In other words, after a submit I don't want the client side actions associated with the query string parameter to fire. I know I could append the form contents to the URL as query string parameters themselves and just redirect to the new URL and avoid the submit/postback, but that will require a lot more type checking on the codebehind to avoid bad data and casual spoofing. I supposed I could also set a hidden field in the codebehind and look at it along with the query string to cancel the client-side behavior if I am coming back from the postback, but that still leaves the query string intact basically forever and I want to get rid of it after the initial page load.
Any ideas or best practices?
PS - Is there anything I can do with the Form.Action property that won't break the postback behavior?
I would use an HTTPModule to intercept and rewrite the URL and query as it came in. I'm not 100%, but I don't believe this has any impact with viewstate.
It sounds (and looks) complex, but it's actually fairly trivial and open to a ton of refinement and extension (as per .NET in general).
iam not sure this is what you are looking for, but if understand correctly you could do this:
-on page load check for the QS value, if its not there use a hidden input field.
-first time page loads with QS, do your normal processing and store QS value in a hidden input field.
-if no QS then use the hidden input value
-after postback, you could redirect to the same page, at that point you could user Request.Form[] to retrieve the hidden input field, still load the data properly but get rid of the QS.
it made sense in my head, i am not sure it make sense now, but i'll let you decide.
It is probably bad practice, but in those cases (and ones I just need to 'reset' the page), I simply do a Response.Redirect to the same page.

Resources