asynchronous __doPostBack - javascript

Requirement
In User control ( In single update panel) - user will change some data and click on button or link button other than save button
We prompt Dialog box to user for (Yes/No/Cancel)
On cancel we did nothing and remain on same page
On No - we redirect/process further without saving data.
On Save we have to save changes and redirect/process further.
Here the problem is with Save
We have different panels on page (same on user control)
On Selection of Yes, Here we call "_doPostBack" for save button click event (Works fine), and in same javascript function below save postback calls other "_doPostBack" to hide current panel (Panel-1) and show another one (Panel-2) and server side process.
But Its always shows (Panel-1)
However in FF, Panel-2 just prompt and hide again.
EDIT: Hide panels are at server side, as there are multiple buttons which shows/hide panels.
Expected result is : After save data, show Panel-2 and hide Panel-1
can anyone has idea how to overcome this prob ?

Without some actual code it is a bit difficult to see what is going on, but from what I understand it is a problem with timing.
When you click 'Yes', you perform the postback function and immediately after you hide the panel.
Your 'doPostback' is actually a-synchronous. Meaning the response comes a bit later (not immediately). Thus when the response comes back it updates your page and that is where you flow is breaking up.
FF is probably fast enough to actual show you what is really happening. Your hide function works ok, but is overwritten by the ajax response.
I suggest you hide the panel asynchronously, with the response that comes from the server.
Here are some links on that:
- RegisterClientScriptBlock within AJAX method call
RegisterStartupScript doesn't work with ScriptManager,Updatepanel. Why is that?
I hope this helps you get on the right track.
Good luck

Related

Is there a way to create a Yes/No popup in ASP.Net VB by calling a function from the code behind?

I need to check a variable in the code behind of a ASP.Net web page and if the variable is a certain value I want to display a modal popup with a message and a Yes/No button. The message will be sent from the code behind.
You can't really do this. However, you can get much the same effect.
First approach - is very easy
This assumes the user going to click on a button on the form. So, say a delete button, or some action to say run a process.
You drop in your standard asp.net button onto your form like this:
<asp:Button ID="Button3" OnClientClick="return myconfirm();"
runat="server" Text="Delete record" Width="90px" />
<script>
function myconfirm() {
var x = confirm("Do you want to delete this record");
return x;
}
</script>
So the above will FIRST run the "js" script code. Note the "on client click". If that function returns true, then you standard code behind button (server side) will run.
In fact, in the vast number of cases, the user "has" to click on something for action to take place. You really say click on a button, then code behind starts running. However during this so called "round trip", you can't have your code STOP and ask for input such as yes/no.
Why? Because when you click a button, the web page travels up to server. Your code behind starts running. Any thing you change on the web page is now changed with the code behind. And THEN the whole page is sent back to the browser. If you stop or interrupt the code while in code behind, then the web page NEVER will get sent back down to the browser until ALL OF your code behind is 100% done, and is finished. When all that code behind is all done, THEN the page gets sent back down to the browser. So you can't interrupt the code.
There are a few other ways to pop up a dialog. And you CAN have a dialog pop and launch as a result of code behind, but it will in effect be the LAST thing your code does, and this will mean that a script block is send down along for the rid in the web page also being sent down to client side.
And in place of the "lame" confirm() in js, you can certainly use say jQuery.UI which allows you to make some half decent dialog boxes compared to the horrid and ugly and simple confirm dialog box. To be fair, the Chrome browser confirm() does look quite nice, but confirm()/alert() dialogs in most browsers leaves a lot to be desired, and calling them "ugly" is being kind.
The other way to ask/get prompts? Don't use a confirm/dialog. Simply put your yes/no on the form as say radio buttons. If you have 2-3 questions, then simply set auto-post back = true, and after the first yes/no (say a radio button group), then a post back occurs, and you display the next question. This works VERY well I find with code behind.
Last but not least?
You can certainly have code behind pop up a dialog box if it is the LAST thing or LAST line of your code. You can do a registerScipt block, and it can popup a dialog for yes/no. But since it is only the VERY last code you can execute in your code behind, then your code behind in 99% of cases is going to be the result of a user having clicked on a asp.net button. Since that is the case, then the above first example to ask/confirm to run the button code again will suffice in 99% of the cases. So just keep in mind that you can't have ANY blocking code in code behind during that round trip. As a result, you can't have a blocking prompt appear and THEN the code behind continues. The code behind has one shot, one change to run. That code runs while the page is up on the server. Your code runs, changes things, values of controls, and then once done the page travels back to the client side. This is the so called round trip.
As noted, you can certainy in that round trip setup the browser to prompt with a yes/no dialog AFTER the page travels back to the browser. So for example, I do say setup a toast message. If a person say clicks on some button, and say to do something (say download a file), and the file does not exist? I can have the browser say spit out a toast message. So you can in code behind trigger a dialog box, or toast messages and even have a dialog box pop up from the code behind, but just keep in mind that setup of the toast message or popping up of a dialog box will be the LAST action you do in code behind, and then the browser travels back to client, is displayed, and THEN your dialog box pops up. You can (and will) then have code behind (and another round trip) occur as a result of that dialog box popping up. I can post an example of how to do this, but in this exmaple while code behind is setting up and triggering a dialog box, there will be no code blocking, or waiting for a yes/no and then the code block continues.

How to show a confirmation box inside code behind in the middle of a procedure

I have an issue I wonder if this fine community can help me out.
I have a webform asp.net (using vb.net code behind) web application, on one of my forms has a datagrid. Each record can have a different status and each record can be selected via a checkbox. At the bottom of the grid I have a dropdown of possible actions that users can take, these actions will be against the records selected and a button to invoke the action selected.
Now the issue I have and I am getting rather annoyed at it is that due to the difference in status before I complete the action selected i need to check whether the records selected can do such action.
Example
I select records 1,2,3,4,5. The action I select is to download in a CSV format, however only records which are unlocked can be downloaded, so in my example only records 1,3,5 are unlocked. This means I can only download 1,3,5 and not records 2,4. Once downloaded I want to mark the record that it has been downloaded and refresh the grid.
I have this completed this by finding the records to be downloaded (aka 1,3,5), create a CSV (or XML as that is also one of the actions) as a string, mark the records 1,3,5 as downloaded, refresh the grid and then I invoke a hidden button on the form which will actually download the data.
If Not ViewState("Data") Is Nothing Then
Response.Clear()
Response.ClearHeaders()
Response.Buffer = True
Response.ContentEncoding = Encoding.UTF8
Response.ContentType = IIf(ViewState("ExportType").ToString() = SharedApplication.ExportedType.XML, "application/xml", "text/csv")
Response.AppendHeader("Content-Disposition", String.Format("attachment;filename={0}", "records-" & "." & ViewState("ExportType").ToString()))
Response.Write(ViewState("Data"))
HttpContext.Current.Response.Flush()
HttpContext.Current.Response.SuppressContent = True
HttpContext.Current.ApplicationInstance.CompleteRequest()
ViewState("Data") = Nothing
End If
The above downloads the data, to invoke the download button I use javascript to do a postback on the click event of the hidden button.
However the issue I have is before I download the data I want to add a confirmation box, something to say "Only 3 of the 5 records will be downloaded, do you want to continue". With yes continuing download and No to stop action. How do I go about doing this. I have tried all sorts of techniques but cannot figure it out.
I have tried adding a return confirmation on the hidden button in its onlcientclik event, but this never gets fired. It does work if you actually click on the button but not when I do a postback to its click via JS.
The closet i can find is using the registeronsubmit in the code behind.
ClientScript.RegisterOnSubmitStatement(btnHidden.GetType, "confirm", "return confirm('Are you sure?');")
But this gets fired constantly, even when we I click on another button. I have tried adding another hidden button with the hope I can add the clientclick to show a confirmation box but that does not work as well.
Does anyone have an idea, I have herd of possibly creating a popup to display the messagebox but then how do I capture if my code needs to continue.
Hi I thought I better share what I eventually did to complete my issue incase someone else moves into this territory.
So I went away and thought about this more and the solution I came up and actually works pretty well although some may say this is rather an ugly way of doing this.
I use a dropdown list to mark what action is needed, so I thought why don't we use the postback event on the dropdownlist to build up the message of the confirmation box and then add the JS confirmation box on the OnClientClick event of the button.
btnRun.OnClientClick = "return confirm('" & sMessage & "');"
The message variable is a string which builds up the message depending on which process action is selected.
This means as soon as the dropdownlist is changed to an action, the codes will work out how many selected items will be changed. This is a sql query which picks up which codes/ids will be changed, then the count of data against the total count of selected items will be changed/exported whatever the action selected. This builds the process button confirmation, so when the user clicks on the process button it displays X out of X will be changed, do you want to continue. If cancel is selected nothing happens if ok is selected it carries on and completes it action as per usual before I wanted to show some message box.

Reloading a page using jQuery while retaining some data

Are there any clever ways of resetting a page back to it's original state (basically a reload) without having the screen physically look like it resets.
Basically i have a bunch of ajax requests, variables and content that i want wiped when a user clicks 'new' (currently i'm using just using location.reload(); ) but want a more graceful way of doing it.
I'm really wanting to refresh it without the screen going white for a split second and also want to retain a single modal popup i have which is open when the user clicks 'new'.
So the user clicks the 'new' button, a popup appears taking a parameter, the site refreshes and the parameter is passed to an Ajax request kicking off the start process.
If anyone could point me in the direction of what to even look for it'd be much appreciated.
"Are there any clever ways of resetting a page back to it's original state (basically a reload) without having the screen physically look like it resets."
You can't refresh the website without making it look like it refreshed, the browser needs time to display the content.
You can, however, use jQuery .load to load some standard markup into your site to make it appear as it did when it was initialized, the browser won't refresh, just like making an AJAX call doesn't require the website to refresh.
I'm, however, unable to see why you want the website to refresh if only to make an AJAX call.
The simple answer is to have the content you want to reload inside a container i.e.:
<div id="container"> page content </div>
Then when you have successfully got new data from the ajax call you can empty the container with:
$("#container").empty();
and repopulate it with
$("#container").append(newcontent);
You can use jQuery's .load to request and replace a portion of your page, e.g. a container element.
For example, calling the following on index.html would effectively "reset" the #container element:
$("#container").load("index.html #container");
See "Loading Page Fragments" on the docs for $.load.
As for resetting variables and cancelling any pending ajax requests - you could perhaps write a reset() function to do all that for you.
Another possibility would be to put data in local storage, or in the url after a # before the reload. But your options for having it look like it isn't refreshing are pretty limited outside of jQuery .load or an XHR request (which is what the jQuery load does)

"Prevent this page from creating additional dialogs" on the second click

I have a weird problem in javascript alerts in my application. On a button click, alert displaying with an ok button. For the second click , i am getting a sentence with a check box "Prevent this page from creating additional dialogs" .
I know this a feature of browser. But for all other sites this is displaying after 10th click. For my application this is coming for the second click .Please help.
The alert is coming as a result of ajax call.
if (result.message!=null && result.message!=''){
alert(result.message);
}
I found the problem coming only when the time between clicks is less than 4 seconds, Any opinion how to increase this .?
Most common browser implement such a feature in order to prevent alert spam. Use another notification method, for example an absolute positioned div.
The jQuery.UI dialog module could give you some ideas.

ajax Modal pop up

I am using ajax modal pop in my asp.net + C# application. My application must show this modal in two situations.
When every thing is ok and the file is read and the data is imported to the database.
When the server side code checks the values that need to be inserted to the database and if they are not correctly formatted, it shows a warning message and a button, so the user would have the option to still insert the erroneous data of the file to the database.
I am using this modal to stop the user from interacting with the controls while the import is being done and it works fine. But for the second scenario I see the modal and when the message and button appear on the screen the modal is still covering the page so the user cannot click on the button or do anything basically. How can I solve this problem?
I thought maybe making the message and button appear on the modal would be an option, but I don't know how to that either. Any suggestions?
/Mono
Heres a tutorial to help you get started wiht the modal itself.
http://yensdesign.com/2008/09/how-to-create-a-stunning-and-smooth-popup-using-jquery/
This is actually a more difficult task then it first appears. Your probably going to want to use ajax, and contact a webmethod to see if there is any errors. Probably make times calls checking for complete, or errors.
File uploads want to do a post back, getting them to do this async is tricky. You may want to research async uploads(or a flash uploader) and see if there are any free uploaders out there. The regular html file uploader control is not asycn.
What you could do is let the page post back, then have javascript run on the page reload, and if it is complete or errors, show the popup.
-Show the modal overlay, while it uploads.
-postback, then run script after postback and on the client page reload show modal success or failed.
I suggest you to change to 3 modal dialogs
Uploading..
Everything went ok
Please fix the following data below:
You will always start with 1.
And then replace it with number 2 or 3, according with what happened at the upload.
If the file upload usually takes usually more than 30 seconds, you may want to consider using a flash upload to provide some feedback to the user at Uploading screen.

Categories

Resources