Page_Load fires infinity times - c#

I have problem with my iframe asp.net page.
Browser url containst parameter which I need to use in my iframe page.
Obviously I can't get access via .NET so I came up with the idea that at the end of the Page_Load method add sth like that :
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bool isReloaded = Request.QueryString.GetValue<bool>("reloaded");
ContentId = Request.QueryString.GetValue<int>("contentId"); //I need this value
if (!isReloaded)
{
StringBuilder js = new StringBuilder("<script language='javascript'>");
js.Append("var last = window.top.location.href.substring(window.top.location.href.lastIndexOf('/') + 1, window.top.location.href.length); ");
js.Append("window.location.href = window.location.href + '?reloaded=true&contentId=' + last;");
js.Append("if(window.location.href.indexOf('reloaded=true') == -1) window.location.reload();");
js.Append("<" + "/script>");
Response.Write(js.ToString());
}
}
}
In shortcut I use Javascript to get value I need and fire reload() but with changed QueryString.
Page_Load is firing again and now I have bool isReloaded filled with true.
The condition (!isReloaded) blocks that this time javascript will not be added to Response.
I don't know why, but Page_Load fires again, this time without added parameters so it's the same situation as at the beginning and again is adding JS etc.
Result is that Page_load fires endlessly.
What did I do wrong ? What is the reason ?

if you have a look at your code, you have this line:
js.Append("if(window.location.href.indexOf('reloaded=true') == -1) window.location.reload();");
you are checking the location.href for the 'reloaded' var, but note that your page is reloaded as soon as you change the location, and your script keeps executing before it is done, so it results in reloading of the page over an over again without the query string.
remove this line and it should work fine.
another thing though, i changed your code a little bit to register the script on page instead of response.write it,
it shouldnt make any difference, but if your code still doesnt work then try my version:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bool isReloaded;
int ContentId;
bool.TryParse(Request.QueryString["reloaded"],out isReloaded);
int.TryParse(Request.QueryString["contentId"],out ContentId); //I need this value
if (!isReloaded)
{
StringBuilder js = new StringBuilder();
js.Append("var last = window.top.location.href.substring(window.top.location.href.lastIndexOf('/') + 1, window.top.location.href.length); ");
js.Append("window.location.href = window.location.href + '?reloaded=true&contentId=' + last;");
ExecScript(js.ToString());
}
}
}
void ExecScript(string script)
{
Page page = HttpContext.Current.CurrentHandler as Page;
if (page != null && !page.ClientScript.IsClientScriptBlockRegistered("AttachedScript"))
{
page.ClientScript.RegisterClientScriptBlock(page.GetType(), "AttachedScript", script, true);
}
}

Thanks for help.
Now I have sth like that and it's ok.
StringBuilder js = new StringBuilder("<script language='javascript'>");
js.Append("var last = window.top.location.href.substring(window.top.location.href.lastIndexOf('/') + 1, window.top.location.href.length); ");
js.Append("if(window.location.href.indexOf('reloaded=true') == -1) window.location.href = window.location.href + '?reloaded=true&contentId=' + last;");
js.Append("<" + "/script>");
I didn't know that editing location executes reload automatically ;)
Thanks again

Related

C# String comparison not working

I'm having this wierd problem within the application I'm currently working on.
string searchText = "onMouseOver=\"CallList_onMouseOver(this);\" id=\"";
List<int> searchOrders = AllIndexesOf(scraper.clientBrowser.DocumentText, searchText);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < searchOrders.Count; i++)
{
string order = scraper.clientBrowser.DocumentText.Substring(searchOrders[i] + searchText.Length, 6);
scraper.clientBrowser.Document.GetElementById(order).InvokeMember("Click");
for (int j = 0; j < scraper.clientBrowser.Document.Window.Frames.Count; j++)
{
if (scraper.clientBrowser.Document.Window.Frames[j].Document != null && scraper.clientBrowser.Document.Window.Frames[j].Document.Body != null)
{
string orderText = scraper.clientBrowser.Document.Window.Frames[j].Document.Body.InnerText ?? "Nope";
//MessageBox.Show(j + Environment.NewLine + orderText);
if (!orderText.Contains("Nope"))
{
sb.AppendLine(orderText + Environment.NewLine);
}
}
}
}
Clipboard.SetText(sb.ToString());
The thing is, whenever I uncomment the MessageBox.Show, I can clearly see orderText is filled with another value than "Nope", the Stringbuilder gets filled, and the correct text is copied.
However if I comment the Messagebox.Show, the outcome of this loop is always "Nope". I'm stuck here, I have no idea what could cause something like this.
The scraper.clientBrowser is a System.Windows.Forms.WebBrowser.
Update:
Solved the issue by waiting for the document to be loaded, created this mechanism:
public bool DocumentLoaded
{
get { return documentLoaded; }
set { documentLoaded = value; }
}
private void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
this.DocumentLoaded = true;
this.clientBrowser = sender as WebBrowser;
}
void clientBrowser_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
this.DocumentLoaded = false;
}
Then in the class I'm using:
while(!scraper.DocumentLoaded)
{
System.Threading.Thread.Sleep(100);
}
It sounds like you need to ensure that the page is fully loaded, like there might be a race condition. I would suggest wiring up the WebBrowser.DocumentCompleted event, and then attempting your scrapping logic.
Update
I overlooked this initially, this certainly has something to do with your issue. The line where you are invoking a click, like so scraper.clientBrowser.Document.GetElementById(order).InvokeMember("Click");. This is done in the iteration, which will more than likely manipulate the DOM -- will it not? I suggest going about this problem entirely different. What are you trying to achieve exactly, (not how you're trying to do it)?
With this alone, I would suggest that you refer to this SO Q/A and look at how they're waiting for the click to finish.
Only one thing I can guest here:
When you uncomment MessageBox.Show, at the time the message box show the info, the clientBrowser use this time to finish loading page. Then when you press OK on message box, the page is load completed, so you get the result. When you comment it, you dont wai for page loaded, so the result is diffent.

Fields re-initialized in PageLoad c#

I am perplexed, and maybe I am just familiar with the properties of pageLoad, IsPostBack, or IsCallback. I created a Boolean variable called "first" and set it to True.
First time through PageLoad, there is a line of code if first = False, if so, write = true.
Then I have a Run_write routine attached to a button, when it runs, if the user response Yes, to the initial question, I make another group of radio buttons visible and set first to false. (i ran this in debug, and I know it hits this line of code) ... so the write to sql is ignored because write == false and the window reappears with the new set of Buttons... Great!
Furthermore, I go through the PageLoad routine again, and it hits the line if (!first), set write to TRUE. my issue is first has been re-set to true? What am I missing?
Note, I was able to work around this by utilizing whether the new set of buttons is checked, but I may not want to go this route, and I do want to understand what is going on.
code is below.
namespace MEAU.Web.Components.SupportCenter
{
public partial class feedback : System.Web.UI.Page
{
String login;
String myurl;
String response;
String s_call;
String p_ship;
String wrnty;
Boolean write;
Boolean first = true;
protected void Page_Load(object sender, EventArgs e)
{
login = Sitecore.Security.Accounts.User.Current.Profile.Email;
myurl = Request.QueryString["value"];
s_call = "No";
p_ship = "No";
wrnty = "No";
// Hide the question Buttons
scall.Visible = false;
parts.Visible = false;
wrnt.Visible = false;
lit.Visible = false;
write = false;
if (!first)
write = true;
}
protected void Run_Write(object sender, EventArgs e)
{
// Get Reponse
if (yes.Checked)
{
response = "Yes";
// Display the quesiton buttons, and Hide the NO button
scall.Visible = true;
parts.Visible = true;
wrnt.Visible = true;
lit.Visible = true;
no.Visible = false;
first = false;
// Did this Prevent a Service call?
if (scall.Checked)
{
s_call = "Yes";
write = true;
}
// Did this Prevent a parts shipment?
if (parts.Checked)
{
p_ship = "Yes";
write = true;
}
// Is this under warranty?
if (wrnt.Checked)
{
wrnty = "Yes";
write = true;
}
// write = true;
}
if (no.Checked)
{
response = "No";
write = true;
}
if (write == true)
{
SqlConnection conn = new SqlConnection(Sitecore.Configuration.Settings.GetConnectionString("feedback"));
SqlCommand cmd = new SqlCommand("Insert_fb", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#login", login);
cmd.Parameters.AddWithValue("#url", myurl);
cmd.Parameters.AddWithValue("#response", response);
cmd.Parameters.AddWithValue("#dateTime", DateTime.Now);
cmd.Parameters.AddWithValue("#serviceCall", s_call);
cmd.Parameters.AddWithValue("#partsShipment", p_ship);
cmd.Parameters.AddWithValue("#warranty", wrnty);
try
{
conn.Open();
cmd.ExecuteNonQuery();
Response.Write("<script type='text/javascript'>parent.$.fancybox.close();</script>");
Response.Write("<script type='text/javascript'>return false;</script>");
}
catch (Exception ex)
{
throw new Exception("Error on file update" + ex.Message);
}
finally
{
conn.Close();
}
}
}
}
}
Every HTTP request to your site creates a new instance of your page class.
Instance state is not preserved.
Instead, you need to store the state in session or ViewState, depending on what you want to apply to.
Page_Load will be called every time your server is requested for the page. This includes post-backs.
You can check IsPostBack to see if the current Page_Load execution is for the first display of the page, or a subsequent post-back.
protected void Page_Load(object sender, EventArgs e)
{
if (this.IsPostBack)
{
// Do first-time things
}
else
{
// Do non-first-time things
}
}
Note that the particular instance of your page object will not persist from access to access. So there may be some information that you need to initialize every time the page is called.
Every time you visit the page, you're creating a new instance of the class.
To distinguish a page load from a user clicking a button vs. a user arriving on the page for the first time, you want to check the IsPostBack property.
So rewrite your if along the lines of
// Code that always executes
if (IsPostBack)
{
// Code that only executes on initial page load
}
else
{
// Code that only executes when a postback event occurs
// e.g. A user clicks on a button.
}
There are some ways by which you can maintain the state or value of the controls, so I have been maintaining the Viewstate of the controls in the not is postback, as check of
(!IspostBack) // means when page loads first time
and whatever written in the else means when postback occurs in which you can maintain the viewstate of the objects.
Also we can use the session, if Viewstate is not used.
Rykiel,
the basic concept of the web is (state-less), and that why you have to handling, but anyway you can read about page life cycle I recommend you read it http://www.codeproject.com/Articles/20659/The-ASP-NET-Page-Lifecycle-A-Basic-Approach
you can use
if(!isPostBack)
{
first=true;
this portion of code only run when the page is requested first time
}else
{
first = false;
}
if you change the value of control in the page or click a button isPostBack will be true. but if you refresh the page or hit F5 you page will be requested again and isPostBack will be false;.
also you can use cookies or session variable (I also recommend do not load too much the Session Variable). try to read the prior link and you will be more clear where put your code to get the best performance.
J.S.
The answers on here are good, but technical. I will try to explain a little of what is happening.
When a browser requests your page, and on the server, a new instance of your class is created.
ASP.NET then runs the rest of the page, starting with your page_load. This will call all your other functions and then render the HTML as a response to your request and send it back to the browser. I like to explain this as a disconnected environment. Once the response is sent, everything is disposed of in a sense. Your variables, previous work, etc... are all gone. The Server as far as it is concerned, never expects to get anything from the browser again... its done its job. It took your request for a page, created a result and posted it back to the browser. Done.
So, think of your code as a new request each time it is called.
You can use the IsPostback as stated by ThatBlairGuy, because that will return true if you are responding to a postback from the browser, meaning that it has already served up this page to the browser on the previous postback.

How to detect page refresh in .net

I have a Button_click event. While refreshing the page the previous Postback event is triggering again. How do I identify the page refresh event to prevent the Postback action?
I tried the below code to solve it. Actually, I am adding a visual webpart in a SharePoint page. Adding webpart is a post back event so !postback is always false each time I'm adding the webpart to page, and I'm getting an error at the else loop because the object reference is null.
if (!IsPostBack){
ViewState["postids"] = System.Guid.NewGuid().ToString();
Cache["postid"] = ViewState["postids"].ToString();
}
else{
if (ViewState["postids"].ToString() != Cache["postid"].ToString()){
IsPageRefresh = true;
}
Cache["postid"] = System.Guid.NewGuid().ToString();
ViewState["postids"] = Cache["postid"].ToString();
}
How do I solve this problem?
using the viewstate worked a lot better for me as detailed here. Basically:
bool IsPageRefresh = false;
//this section of code checks if the page postback is due to genuine submit by user or by pressing "refresh"
if (!IsPostBack)
{
ViewState["ViewStateId"] = System.Guid.NewGuid().ToString();
Session["SessionId"] = ViewState["ViewStateId"].ToString();
}
else
{
if (ViewState["ViewStateId"].ToString() != Session["SessionId"].ToString())
{
IsPageRefresh = true;
}
Session["SessionId"] = System.Guid.NewGuid().ToString();
ViewState["ViewStateId"] = Session["SessionId"].ToString();
}
This article could be of help to you
http://www.codeproject.com/Articles/68371/Detecting-Refresh-or-Postback-in-ASP-NET
you are adding a Guid to your view state to uniquely identify each page. This mechanism works fine when you are in the Page class itself. If you need to identify requests before you reach the page handler, you need to use a different mechanism (since view state is not yet restored).
The Page.LoadComplete event is a reasonable place to check if a Guid is associated with the page, and if not, create one.
check this
http://shawpnendu.blogspot.in/2009/12/how-to-detect-page-refresh-using-aspnet.html
This worked fine for me..
bool isPageRefreshed = false;
protected void Page_Load(object sender, EventArgs args)
{
if (!IsPostBack)
{
ViewState["ViewStateId"] = System.Guid.NewGuid().ToString();
Session["SessionId"] = ViewState["ViewStateId"].ToString();
}
else
{
if (ViewState["ViewStateId"].ToString() != Session["SessionId"].ToString())
{
isPageRefreshed = true;
}
Session["SessionId"] = System.Guid.NewGuid().ToString();
ViewState["ViewStateId"] = Session["SessionId"].ToString();
}
}
Simple Solution
Thought I'd post this simple 3 line solution in case it helps someone. On post the session and viewstate IsPageRefresh values will be equal, but they become out of sync on a page refresh. And that triggers a redirect which resets the page. You'll need to modify the redirect slightly if you want to keep query string parameters.
protected void Page_Load(object sender, EventArgs e)
{
var id = "IsPageRefresh";
if (IsPostBack && (Guid)ViewState[id] != (Guid)Session[id]) Response.Redirect(HttpContext.Current.Request.Url.AbsolutePath);
Session[id] = ViewState[id] = Guid.NewGuid();
// do something
}
If you want to detect a refresh on an HTTP GET rather than only POSTs, here's a hacky work-around that, in modern browsers, mostly works.
Javascript:
window.onload = function () {
// regex for finding "loaded" query string parameter
var qsRegex = /^(\?|.+&)loaded=\d/ig;
if (!qsRegex.test(location.search)) {
var loc = window.location.href + (window.location.search.length ? '&' : '?') + 'loaded=1';
window.history.replaceState(null, document.title, loc);
}
};
C#:
public bool IsPageRefresh
{
get
{
return !string.IsNullOrEmpty(Request.QueryString["loaded"]);
}
}
When the page loads, it will change add a QueryString parameter of loaded=1 without reloading the page (again, this--window.history.replaceState--only works in post-archaic browsers). Then, when the user refreshes the page, the server can check for the presence of the loaded parameter of the query string.
Caveat: mostly works
The case where this doesn't work is when the user clicks the Address Bar and presses enter. That is, the server will produce a false-positive, detecting a refresh, when odds are, the user actually meant to reload the page fresh.
Depending on your purposes, maybe this is desirable, but as a user, it would drive me crazy if I expected it to reset the page.
I haven't put too much thought into it, but it might be possible to write some magic in order to distinguish a refresh from a reset via the address bar using any/all of:
SessionState (assuming SessionState is enabled) and the value of the loaded QueryString parameter
the window.onbeforeunload event listener
keyboard events (detecting F5 and Ctrl + R to quickly change the URL back to removing the loaded QueryString parameter--though this would have a false-negative for clicking the browser's refresh button)
cookies
If someone does come up with a solution, I'd love to hear it.
Another way to check page refresh. I have written custom code without java script or any client side.
Not sure, it's the best way but I feel good work around.
protected void Page_Load(object sender, EventArgs e)
{
if ((Boolean)Session["CheckRefresh"] is true)
{
Session["CheckRefresh"] = null;
Response.Write("Page was refreshed");
}
else
{ }
}
protected void Page_PreInit(object sender, EventArgs e)
{
Session["CheckRefresh"] = Session["CheckRefresh"] is null ? false : true;
}

Ajax toolkit file upload is not called

I have two ajaxtoolkit file ulopads on the same page like
<ajaxToolkit:AjaxFileUpload
id="AjaxFileUpload1"
AllowedFileTypes="jpg,jpeg,gif,png"
OnUploadComplete="ajaxUpload2_OnUploadComplete"
runat="server" />
<ajaxToolkit:AjaxFileUpload
id="ajaxUpload1"
AllowedFileTypes="jpg,jpeg,gif,png"
OnUploadComplete="ajaxUpload1_OnUploadComplete"
runat="server" />
and code behind
protected void ajaxUpload2_OnUploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
{
string filePath = "~/Images/" + e.FileName;
filePath = filePath.Split('\\').Last();
Session["img2"] = filePath.ToString();
AjaxFileUpload1.SaveAs(MapPath(filePath));
}
protected void ajaxUpload1_OnUploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
{
string filePath = "~/Images/" + e.FileName;
filePath = filePath.Split('\\').Last();
Session["img1"] = filePath.ToString();
ajaxUpload1.SaveAs(MapPath(filePath));
}
The question is whenever I use upload AjaxFileUpload1 it works on and calls void ajaxUpload2_OnUploadComplete method but if I use ajaxUpload1 the method ajaxUpload2_OnUploadComplete is called again but the method ajaxUpload1 is not called
Why??
Thanks.
We got the same problem yesterday and we found out that you cannot have more than one instance of AjaxFileUpload on the same page.
If you look at the source code, you'll see that this control use a constant GUID to identify its events. Since the GUID is a constant, all instances of AjaxFileUpload use the same GUID...
Result :
the first instance swallow all the events...
Here is the GUID in action :
private const string ContextKey = "{DA8BEDC8-B952-4d5d-8CC2-59FE922E2923}";
(...)
if (this.Page.Request.QueryString["contextkey"] == ContextKey && this.Page.Request.Files.Count > 0)
We customized the September 2012 toolkit as follows - hope this is a temporary workaround and that this is fixed in a future release:
OLD
private const string ContextKey = "{DA8BEDC8-B952-4d5d-8CC2-59FE922E2923}";
NEW
private string ContextKey = "";
OLD
public AjaxFileUpload()
: base(true, HtmlTextWriterTag.Div)
{
}
NEW
public AjaxFileUpload()
: base(true, HtmlTextWriterTag.Div)
{
if (HttpContext.Current.Items["lastAjaxFileUploadContextKey"] == null)
{
HttpContext.Current.Items["lastAjaxFileUploadContextKey"] = 1;
}
else
{
HttpContext.Current.Items["lastAjaxFileUploadContextKey"] = (int)HttpContext.Current.Items["lastAjaxFileUploadContextKey"] + 1;
}
ContextKey = HttpContext.Current.Items["lastAjaxFileUploadContextKey"].ToString();
}
There actually is a way to use multiple AjaxFileUpload controls on a single page, with each control firing its own event. The solution is very simple; it involves overriding one of Microsoft's client-side functions for the AjaxFileUpload control to inject information on the control that actually caused the upload complete event, then using a single event handler for all of the AjaxFileUpload controls as a "switchboard", which will subsequently fire the correct event handler for the control which created the event server-side.
Here's how to do it:
Add this script block somewhere after the head element of your page. If you're using master pages, put this in a placeholder for HTML content:
<script type="text/javascript">
Sys.Extended.UI.AjaxFileUpload.Control.prototype.doneAndUploadNextFile = function (c) {
var a = new XMLHttpRequest, b = this;
a.open("POST", "?contextKey=" + this._contextKey + "&done=1&guid=" + c._id + "&uplCtrlID=" + b.get_id(), true);
a.onreadystatechange = function () {
if (a.readyState == 4) if (a.status == 200) {
b.raiseUploadComplete(Sys.Serialization.JavaScriptSerializer.deserialize(a.responseText));
b._processor.startUpload()
}
else {
b.setFileStatus(c, "error", Sys.Extended.UI.Resources.AjaxFileUpload_error);
b.raiseUploadError(a);
throw "error raising upload complete event and start new upload";
}
};
a.send(null);
}
</script>
This code is the same function being used to call your page and trigger the UploadComplete event, only modified to add an extra parameter - uplCtrlID - which will contain the ID of the control that REALLY caused the event.
Set up your server side code as follows:
//set the OnUploadComplete property on all of your AjaxFileUpload controls to this method
protected void anyUploader_UploadComplete(object sender, AjaxFileUploadEventArgs e)
{
//call the correct upload complete handler if possible
if (Request.QueryString["uplCtrlID"] != null)
{
//uplCtrlID (the query string param we injected with the overriden JS function)
//contains the ID of the uploader.
//We'll use that to fire the appropriate event handler...
if (Request.QueryString["uplCtrlID"] == FileUploaderA.ClientID)
FileUploaderA_UploadComplete(FileUploaderA, e);
else if (Request.QueryString["uplCtrlID"] == FileUploaderB.ClientID)
FileUploaderB_UploadComplete(FileUploaderB, e);
//etc (or use a switch block - whatever suits you)
}
}
protected void FileUploaderA_UploadComplete(AjaxFileUpload sender, AjaxFileUploadEventArgs e)
{
//logic here
}
protected void FileUploaderB_UploadComplete(AjaxFileUpload sender, AjaxFileUploadEventArgs e)
{
//logic here
}
You're all set. Multiple AjaxFileUpload controls on the same page, no problems.

C# validate repeat last PostBack when hit Refresh (F5)

i have a webform that generates a file, but when i click the button that produces the postback to generate the file Once it finish if i press Refresh (F5) the page resubmit the postback and regenerates the file, there's any way to validate it and show a message to the user or simply DO NOTHING!
thanks :)
The simpler way will be to use Post Rediret Get pattern.
http://en.wikipedia.org/wiki/Post/Redirect/Get
Make sure to check out External Links on that Wikipedia article.
the browser should warn them if they hit refresh on a page that has been postbacked. how i handle it though is in the session track what i have done so i don't repeat certain actions. a simple flag should suffice.
Check for the existence of the file in question in your postback logic and only create the file if the file doesn't already exist:
if (false == System.IO.File.Exists(filename))
{
// create the file
}
else
{
// do whatever you do when the file already exists
}
i wrote a solution for this problem and here it is if anyone needs it.
protected void Page_Load(object sender, System.EventArgs e)
{
/*******/
//Validate if the user Refresh the webform.
//U will need::
//A global private variable called ""private bool isRefresh = false;""
//a global publica variable called ""public int refreshValue = 0;""
//a html control before </form> tag: ""<input type="hidden" name="ValidateRefresh" value="<%= refreshValue %>">""
int postRefreshValue = 0;
refreshValue = SII.Utils.convert.ToInt(Request.Form["ValidateRefresh"]); //u can use a int.parse()
if (refreshValue == 0)
Session["ValidateRefresh"] = 0;
postRefreshValue = SII.Utils.convert.ToInt(Session["ValidateRefresh"]); //can use a int.parse()
if (refreshValue < postRefreshValue)
isRefresh = true;
Session["ValidateRefresh"] = postRefreshValue + 1;
refreshValue = SII.Utils.convert.ToInt(Session["ValidateRefresh"]); //can use a int.parse()
/********/
if (!IsPostBack)
{
//your code
}
}
you just have to evaluate:
if (!isRefresh)
PostFile();
else
{
//Error msg you are refreshing
}

Categories