I have a custom dll loaded into my web application, I updated it and reloaded the reference in my main web app. Basically the way it works is a user uploads a file that gets checked by a function in the dll. The user can then click a button on the results section that redirects to the main section of the app so they have a choice of rerunning the checker; I say section because the web app uses a single page with two divs, one for the form and one for the results, the code behind then swaps the visibility of the form div from the results div, here's the basic layout to clarify.
<body>
<div id="divForm" style="visibility: visible;">
</div>
<div id="divResults">
</div>
</body>
The redirect call is in the code behind as follows:
protected void btnReturn_Click(object sender, EventArgs e)
{
Response.Redirect("http://localhost/file_checker/");
}
And the main piece of the file checker:
protected void Page_Load(object sender, EventArgs e)
{
divForm.Visible = true;
divResults.Visible = false;
if (Page.IsPostBack)
{
//Do file checking
//Show results
divResults.Visible = true;
divForm.Visible = false;
}
}
The web app works fine on the first pass, however, I notice strange behavior after every subsequent pass. For example, I notice that the results page shows stale content from an older version of dll, i.e. It displays a string that was generated by an older version of the dll, I had removed the string and yet it still gets generated. I'm not sure if I'm redirecting correctly or if there's some other fundamental misunderstanding I have with how redirects work. Any help or insight is appreciated.
UPDATE:
Ok, instead of redirecting, I just cleared a gridview in my results section and swapped the styles of the divs to show the main section and hide the results section as follows:
protected void btnReturn_Click(object sender, EventArgs e)
{
//Clear the Gridview and show the upload Form
GridView1.DataSource = null; //<--Is popoulated by a DataTable
divForm.Visible = true;
divResults.Visible = false;
//Response.Redirect("http://localhost/file_checker/");
}
That seems to fix the problem, a lingering question I have is, I'm using a Datatable to populate the Gridview in the results page. Does the Datatable automatically dispose itself after each page load or button click event? Or will it persist? I just want to avoid any memory leak issues, since each page load instantiates a new DataTable object.
A Response.Redirect sends a 302 response to the browser. The browser then makes a new request to the new location. So it isn't a server side operation, and the browser can cache the response and not make the call again. Try appending a querystring parameter to make the browser do a true request. If you have fiddler available, you might use that to verify, but you are probably getting the page from cache.
protected void btnReturn_Click(object sender, EventArgs e)
{
Response.Redirect(String.Format("http://localhost/file_checker/?{0}", DateTime.Now.Ticks));
}
Related
I'm doing a project for school and i have this page with some search results.
When i go back to the search results page, the browser asks to "Confirm Form Re-submission" and i would like to avoid this.
I haven't done nothing so far to solve this issue besides searching the web for a solution and not had not found one.
Browser message:
Confirm Form Re-submission
This web page requires data that you entered earlier in order to be properly displayed. You can send this data again, but by doing so you will repeat any action this page previously performed.
Press the reload button to resubmit the data needed to load the page.
ERR_CACHE_MISS
The problem lies in that you are doing a post request, and if the person making use of your web page refreshes it, it does what it is supposed to do resubmit the post request.
Hence, there is no way of preventing this issue from happening, what you could rather do is changing the way you obtain the values from a post request to get request.
Hi. Found the solution for my problem.
So what i did was, stored the value in session, redirect to a loop page and back, and just preform the search on form load. Now i can go back and forth with no problem. Here is the code i used:
Search page:
protected void btnSearch_Click(object sender, EventArgs e)
{
Session["searchValue"] = txtSearchValue.Text;
Response.Redirect("loopPage.aspx");
}
Code on LoopPage.aspx:
protected void Page_Load(object sender, EventArgs e)
{
Response.Redirect("searchPage.aspx");
}
search page again:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["searchValue"] != null)
{
...preform search and create objects to present results...
}
}
Here a button that does a postback
<asp:LinkButton runat="server" ID="btnBestPrice" OnClick="btnSearchBestPrice_Click">Search Best Price</asp:LinkButton>
Assume that this button is clicked on page
http://localhost:47207/Default?ClusterId=131
Now after the postback is completed, the page is still
http://localhost:47207/Default?ClusterId=131
However, after the postback, i want to make URL to be
http://localhost:47207/Default
Is that possible?
If i make a redirect, the postback event would be loss. I still want to process the postback event perfectly fine. So if somehow i can set the postback url to be raw url of the page on the client side or?
asp.net 4.5 web forms c#
I assume your postback is displaying some information to user, but have to change the URL without interrupt the process.
First of all you have to understand that if you changed the URL in the server side, the browser will treat it as a new page and make a new request. Response.Redirect is basically telling the browser it is time to move onto another page. So you cannot change the URL while remaining in the same request. (while Server.Transfer is remaining at the same URL but different page which is not what you want)
So I have 2 solutions for you, the following one make sense to me but there is still redirecting the page:
protected void Page_Load(object sender, EventArgs e) {
if (Session["ClusterId"] != null) {
try {
int ClusterId = int.Parse(Session["ClusterId"]);
// Code here
} catch { }
Session.Remove("ClusterId");
return;
}
}
protected void btnSearchBestPrice_Click(object sender, EventArgs e) {
int ClusterId = int.Parse(Request["ClusterId"]);
Session.Add("ClusterId", ClusterId.ToString());
Response.Redirect("~/Default");
}
Here is another solution that does all the actions in your btnSearchBestPrice_Click event without Redirect and Session, and bind a JavaScript page ready event, call history.pushState and also wipe out the unnecessary parameters in the action attribute of your form element.
I want to make this code work on c# code behind. I know in C something similar can be done, but can it be done here?
protected void Page_Load(object sender, EventArgs e)
{
Bar.Text = "Updating Information";
InsertData();
Bar.Text = "Information Updated";
}
protected void InsertData(object sender, EventArgs e)
{
//loops and statements
}
any ideas?
Two things. First, you're not passing the expected parameters:
protected void Page_Load(object sender, EventArgs e)
{
Bar.Text = "Updating Information";
InsertData(sender, e);
Bar.Text = "Information Updated";
}
protected void InsertData(object sender, EventArgs e)
{
//loops and statements
}
Second, this looks like ASP.NET Web Forms. The changes to the Text property of your control will not update the UI. To understand why, you have to understand what's really going on outside the bounds of the actual code you write. ASP.NET WebForms have a very high degree of "inversion of control"; your code lives to serve the ASP engine resident in the IIS webserver, and has very little ultimate control over when it runs (or even when it exists).
This has two major consequences for you; first, the amount of state information retained by the server about each client asking for pages from it is very low; ideally, no state is saved (but realistically the server must usually keep track of some "session state", especially for secured web apps requiring a login). In fact, your codebehind class only lives as long as it takes to render a page; after that, the class (and any state data it was trying to store in memory) are disposed of and garbage-collected.
Second, and more importantly for you, anything done in the codebehind to render the next requested page will not result in real-time updates for the client; everything your code does in the codebehind, from Page_Load to Page_OnPreRender, is performed before a single bit of the resulting HTML is sent back to the client. The upshot is that changing the value of a control on the form several times during the execution of codebehind event handlers has zero bearing on what the client sees, unless you specifically set up additional communication with the client. The updating of Bar.Text before calling InsertData() will not cause the client to see the message "Updating Information". All the client will ever see is "Information Updated", because that is the text of the control as of when the page is finally rendered into HTML, after all your event handlers execute.
Problems like these are typically resolved with asynchronous architectures such as AJAX (Asynchronous Javascript And XML). JavaScript's level of control over the rendered page in the browser window rivals that of a WinForms application (enough that you can program simple games with HTML as your UI using only client-side JavaScript), and there are methods and objects JavaScript can use to send requests for data to your server. So, to keep the user's displayed page responsive during this insert operation, you could employ these tricks. You'd have a button on your page that, instead of triggering a full postback of the entire page, would instead trigger a Javascript method on the client that would send a web service request to your server, and also update the value of your Bar UI control text with the status "Updating Information". The server would perform an action (your "InsertData()" call) and then respond to the client's browser, which would trigger a second JavaScript method that would update the status again with "Information Updated".
As an aside, it is in fact possible to create entire web applications using this Javascript-based "service-oriented architecture" for all content updates, where only one initial HTML page is ever served to the client browser, and all further changes to that page's layout, content and behavior is controlled by JavaScript updating the one page's DOM based on data received from web service calls. Such architecture of a website has its downsides, but also some big advantages.
Bar.Text = "Updating Information";
InsertData(this, EventArgs.Empty);
Bar.Text = "Information Updated";
You can change your InsertData() method, since you are calling it with no parameters.
protected void InsertData()
{
//loops and statements
}
Do you need to know the sender or pass EventArgs? You could simple redefine the InsertData Method as
protected void InsertData()
{
//loops and statements
}
and then call the method via
Bar.Text = "Updating Information";
InsertData();
Bar.Text = "Information Updated";
I have an ASP.NET page where an iframe src is being dynamically set:
<iframe id="specialframe" src="<%= IframeSrc %>"></iframe>
And the codebehind:
internal string IframeSrc { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
IframeSrc = SpecialService.GetNewUrl();
}
SpecialService is a third-party service that, when called, returns a one-time use URL that looks something like:
http://www.specialservice.com/oneTimeUseId=fh8396123lkjufgh49
For security purposes, this ID can only be used once before expiring. However, this leads to a problem.
The first time I display the page in a browser, it loads the iframe correctly. However, if I hit refresh, I will receive an error message in the iframe because it's trying to use the same one-time use ID. It works properly if I Ctrl+F5 refresh to force a cache clear, but I can't tell my users to do that every time.
My question is, is it possible to force the browser to never use the cache and always request a fresh copy from the server?
You need to set expiration and caching one your page correctly so browser always will have to request page from the server.
This question maybe a good start http://msdn.microsoft.com/en-us/library/y18he7cw.aspx.
You need cacheability to none (and maybe to set expiration in the past, maybe SetMaxAge too):
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.Now.AddMinutes(-30));
i am using Intelligencia UrlRewriter for converting my page
www.mywebsite.com/subject.aspx?subject=sub1
to
www.mywebsite.com/subjects/sub1.aspx
On this page, I have next and previos buttons to browse the different subcategories on that subject and have used DataList with paging to support that.
When the page is first shown(IsPostBack=False) it works fine, but when next button is fired, the URL converts into this:
www.mywebsite.com/subjects/subject.aspx?subject=sub1
Is there any idea why it is happening ?
My web.Config file is as follows :
Mt web hosting company uses IIS 7.
EDIT: I have windows 7 and I tried by using local IIS and it ran fine there.
You can code this in your master page for this problem
Here form1 is the form tag and place it in master page's load event
protected void Page_Load(object sender, EventArgs e)
{
form1.Action = Request.RawUrl;
}