Prevent opening of Application on browser refresh - c#

I have an ASP.NET/C# application that exports some data into an Excel spreadsheet using COM interop at the click of a Button control. When I click the button, Excel is opened with the generated spreadsheet. For what it's worth, here is the button code:
<asp:Button ID="export" Text="Export to spreadsheet" runat="server" OnClick="Export_Workbook" />
This works fine, except when I click the button, close the subsequent spreadsheet, and refresh the page. When the page is refreshed after clicking the button, the call to Export_Workbook() is made again and so the spreadsheet opens again. Firefox, for example, says this when you refresh: "To display this page, Firefox must send information that will repeat any action (such as a search or order confirmation) that was performed earlier." This is something I want to avoid.
I'm sure there's a better way to accomplish what I'm trying to do, I'm just not sure what the best approach is.
Edit
I've accomplished this by doing the following:
protected void Export_Workbook(object sender, EventArgs e)
{
Response.Redirect(Request.Url.AbsolutePath, false);
ItemList.Prepare_Workbook();
}
Not a particularly elegant solution, but it works. I'm still open to suggestions.

You have to redirect back to the current page at some point at the end of the postback to prevent duplicate form submission. This is called the Post/Redirect/Get pattern.

I dont think there is a better way to do it, except to keep track of if it has been refreshed before, and just not call the content the second time you refresh it.

Related

Button not enabling another control - life cycle issue?

I hope I can explain this well without having to post scads of source.
I have a page in an online store that lets the user pick a date for a tour. That page has a ConLib which contains a couple of panels. The first is:
<asp:Panel ID="pnl_Grid" runat="server">
<cb:SortedGridView ID="VariantGrid" runat="server"
AutoGenerateColumns="False" Width="100%"
SkinID="PagedList" DataKeyNames="OptionList">
<Columns>
</Columns>
</cb:SortedGridView>
</asp:Panel>
and then another asp:Panel with other stuff - calendar, buttons, etc. The SortedGridView is supplied by our shopping cart provider and is basically a regular asp:Grid.
When the user says they want 3 tickets I do a
pnl_Grid.Enabled="false"
to keep them from changing it after picking a date/time. At various points they can click a reset button that will do many things but one task is to set the value to zero in the gridrow text boxes and enable the Grid since they reset the process and want to pick new things.
I have stepped through the code with breakpoints in place and it seems that the problem I am having is that my reset button click event handler fires after the Grid is rendered with the disabled status in the Page Render stage of life. If that is true then setting the Grid in code behind will never show the enabled grid on the page without a refresh since it's already rendered. If I refresh the page manually it does indeed show as enabled so I think I'm on the right problem.
My question is twofold, if there is enough info here to answer it:
1. Is it likely that what I think I am seeing is true - the page renders the control disabled and then the button handler tries to enable it but it's too late at that point since the control is already rendered?
2. How can I work around this? I would prefer to avoid JQuery if at all possible... it has some unintended side effects with the way our original store software is written.
Further info:
I have a status flag _EventSelected which tells if the calendar event has been selected. On the reset button click I set that to false and on PreRender I check that to see if it is false and enable the Grid. Again, the status doesn't change until the Reset Button Click event handler and that is after the PreRender.
Thanks for your input! I swear, some days ASP.NET makes perfect sense to me and other days it is clear as mud.
I found a working solution. To force the page to refresh after the button enables the Grid, which is not displayed without the refresh, I added this to the btn_Reset_Click handler, found by searching for "cause page reload":
Page.Response.Redirect(Page.Request.Url.ToString(), false);
It's not a bad solution since I'm in a reset / start-over state anyway. Thanks for taking time to read my question! Hopefully this will help someone else some day.

Is it possible for a server to treat a postback as not a postback?

Currently I have a site that loads everything on initial load (when it's not a postback). Then it proceeds to load more data that should be fine to load regardless if it's postback or not. I thought everytime the page is refreshed or the button is pressed there is a postback. What I thought that if the user doesn't go to another page, any action he takes will be a postback.
However I'm getting very inconsistent errors when the site is actually on a server and was curious if perhaps, when clicking a button after a bit of inactivity, will the server possibly forget about the previous activity and treat the action as the person hitting the site for the first time again?
Below is how the button is defined....
<asp:Button CssClass="btn btn-default controls" ID="btnAddAdditionalCom" runat="server" Text="Add Comment" OnClick="btnAddAdditionalCom_Click"/>
Any asp:button click will cause a postback to the server because asp is a server side language so it has to talk to the server to execute the button click. If you want to do button clicks without talking to the server use something like javascript
You cant modify it.
Because this is a essential concepts in asp.net.
Even you cant change the value of IsPostback Property (It has no setter).
If you want to treat Postback as page load then u forgot the need of IsPostback

Submit a form from code behind

I'm having trouble implementing a functionality on my c#/asp.net app.
I have a form with a RadioButtonList and a submit button.
The RadioButtonList is generated on Page_Load() from a list of objects I retrieve from the database.
I would like to automatically submit the form if there is only 1 object in the list.
I have access to my Form object, to the submit button etc... but I can't seem to find a solution (in the end I'm looking for a kind of form.Submit() ) method.
Does anyone have an idea of how I could do this ?
Thanks in advance !
EDIT >> Here is the code :
.aspx : http://pastebin.com/0E6T7dqH
.aspx.cs : http://pastebin.com/54payZJP
EDIT2 >>>
As it seems there is no way to do what I wanted to do at first, I ended up using a session variable with a response.redirect()
Source :
http://dotnetslackers.com/Community/blogs/haissam/archive/2007/11/26/ways-to-pass-data-between-webforms.aspx
Post happens in the client side. As in Page_Load you are currently executing in the server side, just call the code you want to execute on post.
Edit: For actually going to another aspx
public void Page_Load(object sender, EventArgs e) {
if(!IsPostback && OnlyOneItem) {
Server.Transfer("TheOtherPage.aspx");
}
}
Server.Transfer will maintain the entire request, so your post data will be available.
http://msdn.microsoft.com/en-us/library/system.web.httpserverutility.transfer.aspx
Try something like this
In your Page_Load
if(!IsPostBack)
{
if(check for only one object)
{
//your submit code
}
}
I actually had to do something similar, once. Here is a way you can do it.
Asp.Net buttons have a property called PostBackUrl, and it does exactly what you would expect - it controls where the form will post if you click the button.
You can also use the RegisterStartupScript function to render javascript on the page.
Now, with these two pieces, you can achieve your goal.
if(!IsPostBack)
{
if(results == 1)
{
button.PostBackUrl = "next page's url"
//Register script to click the button using RegisterStartupScript
}
}
Now, having shown you this, I will warn you it may not make for the best user experience. When I did it, it was for a very specific case that had no other solution. The page will actually post back to the user, and they will see the page for a moment before the javascript to click the button takes effect. Additionally, when you set a button's PostBackUrl, that means that when it is clicked, your entire form will be posted to the page specified. The code behind for the current page will not fire at all, so if you have any validation, it won't run.
There's nothing wrong with letting the user click the button to submit the form even if they only have one choice. In my experience, users like to feel like they are in control on the system; they don't like it when pages just do things without their input.
Also, there is not really anything wrong with putting the information the next page needs into the session or even a database table, and using Response.Redirect. It's a fairly common practice and works reliably in most scenarios.

Asp.net connection

i have an asp.net website where thers an apply button and the webpage uses microsoft sql. The problem is that when the user clicks the apply button for a very long time right about 80-100 times, the webpage usually seems to lose connection and takes forever trying to load the page, and then either says a connection is close or timeout error.
Any ideas? Only happens if the user clicks the apply button for a very long time, but if i wait for like 2-5 mins and reopen the browser, everything works fine again
Do you want the functionality by which use can click the button twice. Why don't you think of disabling the button once it is clicked and update the text to 'Processing.....'
You can do as follows...
Considering you have a button with ID="ButtonProcess" runat="server"
Add the script as following
<script language="javascript" type="text/javascript">
$(document).ready(function() {
$('input[id*=ButtonProcess]').click(function() {
//Your logic goes here to change text, do validation, bla bla bla.....
$('input[id*=ButtonProcess]').attr('className', 'processbookingbutton');
$('input[id*=ButtonProcess]').attr('disabled', 'disabled');
});
});
</script>
So, disabling the button and replacing the text with "Processing....." gives the user indication that you have already clicked the button and now wait for the output.
You can also use UpdatePanel and UpdateProgress controls to make it more user friendly.
Hope this helps....
Sounds like you aren't managing DbConnection properly. Show us your data access code, specifically how you are opening/closing connections.

JQuery: how to keep a dialog box open after an asp.net postback has occurred

Looking at the other dialog questions, mine seems to be the exact opposite to postback queries. I have a dialog box which opens when a button is clicked. all that is fine. I have attached the dialog to the form and posting to asp.net is fine too, but, my problem is when a postback occurs of course the dialog closes which I do not want to happen as the user may want to post via the dialog function several times. Basically the dialog connects to an asp process that creates a folder, the user may want to create several folders (for image upload) but postback causes the dialog to close, rather abruptly!, and I would rather the user dismissed the dialog when they have finished. Any ideas how I might achieve that? I'm using Jquery and Asp.net C#. I'm fairly new to both c# and Jq so am flummuxed. I've tried this lastly........... Thanks
protected void Page_Load(object sender, System.EventArgs e)
{
if (IsPostBack)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "callme", "$('#opener').click(function(x)", true);
}
}
I'd suggest looking at using an AJAX post to call your 'Create Folder' function, that way you can separate the postback from the currently displayed page and maintain your state there.
You might want to consider using AJAX to post the data from the dialog box. Otherwise, you could check for Page.IsPostback and open the dialog if true.
You could trigger the .click() event on your #opener object on Postback:
<script type="text/javascript">
$(function() {
<% if (Page.IsPostback) { %>
$('#opener').trigger('click');
<% } %>
});
</script>

Categories

Resources