I want to confirm user to proceed the saving .
Let's say I have two processes , Process-One and Process-Two .
After complete Process-One , I want to show confirmation message box to user to proceed saving Process-Two .
protected void btnSaveProcess_Click(object sender, EventArgs e)
{
..Saving Process-One....
..Saving Process-One Complete....
// here to show "Are you sure you want to Save Process-Two"
//if user confirm "OK" , save Process-Two
//if user "Cancel" , stop and cancel saving Process-Two
}
Actually , I'm a little weak in Javascript . How can I make this process using Javascript ?
TIA :)
Since you said you are not great with javascript, you can try this. Break the process into 2 buttons, first part is done in btnSaveProcess_Click, and now make a second button btnSecond. btnSecond will handle the second part, after btnSaveProcess_Click is done have it trigger the click event of the btnSecond button. when you create the btnSecond set it up with an alert like:
<asp:Button id="btnSecond" Text="2nd Button" CssClass="HideMe" runat="server" OnClientClick="javascript:return confirm('Would you like to proceed?');"/>
this way the 1st button completes and calls the 2nd ones click. that will cause the alert to come up, if the user clicks ok it proceeds to the 2nd ones event, if the user hits no it doesn't continue.
you could use the ASP.NET AJAX Control Toolkit. it has a ConfirmButtonExtender that will show a confirm dialog before triggering the asp.net postback.
if you want some thing more lightweight you could add the following to a asp.net button onClientClick="return confirm('Are you sure?');"
Related
When I am clicking a submit button on my HTML form, the function related to the button is called and it does its work. After completing the work a mail notification is sent which is consuming too much time. After that a confirmation message is displayed on the same HTML page (without using Ajax; i.e., the page is refreshed).
I want to avoid letting the user click the submit button multiple times in confusion during the waiting period of sending mails. So I am thinking that I should disable the button after it is pressed once.
How can I do this?
Can you please suggest any other technique to achive this goal without disabling the button?
Simply:
<form action="file" method="post" onsubmit="this.submit_button.disabled = true;">
<input name="submit_button" type="submit" value="Submit" />
</form>
You can achieve this without disabling the button by using a nonce, however it is a bit more complex. Essentially, when the user requests the page that has the form that will be submitted, assign a unique id to that user's request (store it somewhere on the server, and make sure it's submitted along with the form). When the form is then submitted, look up the unique id to make sure it's not in process or already processed, and then if it's OK to proceed, mark the unique id as "in process", process the form, and then mark it as processed. If when you do the initial check and the page is in process or already processed, you'll need to take the necessary action (redirect them to a confirmation page if it was successfully processed, or back to the form if it was not successfully processed).
How can I do this?
You can take a look at the javascript code in this page:
http://www.codinghorror.com/blog/archives/000096.html
<input type="Button" onClick="this.value='Submitting..';this.disabled=true;" value="Submit">
Can you please suggest any other technique to achive this goal without disabling the button?
Show a busy panel:
"... Your request is being processed please wait..."
(source: vodafone.co.uk)
If you disable a button right before submitting, then the parent form will not be submitted. You need to disable the button after submitting. Best way it to use JavaScript's setTimeout() function for this.
<input type="submit" id="foo" onclick="setTimeout('document.getElementById(\'' + this.id + '\').disabled=true;', 50);">
50ms is affordable enough to give the form the chance to get submitted.
To enhance the user experience more, you could of course append a message or a loading image dynamically during the same onclick event as already suggested by others.
Assuming you don't want to disable the button you could always pop up a modal on the page. This will block the user's interaction with the page. You could throw some kind of loading spinner in there with a message that the submit is in progress.
I don't understand why it is a problem, as you are doing a regular submit, the user should see a white page while you are processing in the back end.. But in case if you want to disable the button, here is the code, use it on the button
onclick="this.disabled=disabled"
You could have the button be disabled, but still seem active to the user. In the function that gets called after the button is hit the first time, have the first thing it does set a global variable like disableButton to true. When the user presses the button, have that go to a function called something like checkSubmitStatus. If disableButton = true, return false. if disableButton = false, trigger the submit function.
You have still disabled the button, but your users can press away unaware.
I'm not submitting anything, but Google Chrome 31 doesn't update the button look while calculating, so i came up with this workaround:
<style>
.btnMenu{width:70px; font-size:12px}
.btnMenu:disabled{background-color:grey}
</style>
<input type="button" class="btnMenu" value="Total" onmousedown="b=this; b.disabled=true; b.v=b.value; b.value='Calculating...'; setTimeout('updateTotals(); b.value=b.v; b.disabled=false', 100)"/>
So a bit of background, I basically have a form where users can change stuff. This form is a bootstrap modal form. I want to display a warning or something when users close the form down if they have made changes to the form. So I thought I would add a hidden field and when things changes on the form (multiple ways this can happen) then I will mark the hidden field with a true value.
I have this working but now all that is left is to create something that will look at the hidden value then show a confirmation box to say something in the realm of "You have made changes and will loose all changes you have made. Would you like to continue?" with a yes, no or ok, cancel buttons.
I though this would be rather simple but for some reason I am struggling and I am not sure why.
My button is create like this
<asp:Button ID="btnX" runat="server" CssClass="close" Text="×" OnClientClick="return confirmClick();" />
I have a server onclick function done in vb.net/c# language is no issue for me I can always translate it like this
Public Sub btnX_Click(sender As Object, e As EventArgs) Handles btnX.Click
CloseModalForm()
End Sub
I also have javascript which would ideally call before the server code
function confirmClick() {
var _changed = document.getElementById('<%=hiddenField.ClientID %>').value;
if (_changed == true) {
alert(_changed);
}
}
However this does not work, I know this is an alert box, I ended up changing it to an alert while trying to get this to work and seeing if i could get a popup message to say true when hidden field is true and for it not to appear when the hidden field is false. I have messed around a lot on this hence why i ended up with alert. But nothing seems to work if i try just
if (alert(_changed)) {
alert(_changed);
}
Then it shows the alert box regardless if the value is a true or false
This seems very simple to me, but I cannot get it to work, which must mean I am missing something which I have not thought about. I have googled how to do if statements with Booleans in case I got that wrong but I cannot see anything wrong in what I am doing. The only other thing is maybe I have completely negated something or miss interpreted the way JS works. Ideally this would be client side rather than server side as this is just a case of asking users if they want to continue. I still need the server side procedure as many more things happen while the modal form closes down.
Any help will be much appreciated.
Ok, first, I assume you have some kind of dialog system you are using. You can try to use bootstrap ones, but they look fantatic, but the markup and really easy way to pop such a dialog with code beyond sucks. So, I suggest using jQuery.UI dialogs. (only issue with jQuery.UI dialogs is the don't look all that great. But, for this example, that's what we going to use.
next up:
We all know that when you drop in a server side button, you can can ALSO add that client side click, and if the routine returns true, then server side click code behind will run. if the js routine returns false, then the server side click button code does not run. This is rather clear, and even your posted code shows this knowledge.
so, for super quick and dirty, and in a hurry, we can, and often do this:
<asp:Button id="cmdDelete2" runat="server" class="btn myshadow" style="margin-left:15px"
type="button"
onclick="cmdDelete_ServerClick"
onclientclick="return confirm('Really delete?')" >
</asp:Button>
Of course for above, we get/see the web browser dialog, and those look REALLY poor!
but, it does work. And thus you get this:
(I am editing a single record in a popup dialog in above)
So, the above use of js confirm() will work. But as you can see, the position and how ugly that dialog box is? Well, its just ugly.
so, we can adopt jQuery.UI dialog. They are much better looking. Of course there are bootstrap ones, and I see quite a few people using sweetAlert (very very nice).
Anyway, ONE BIG HUGE issue with all of these so called "dialog" libraries that look great, function great, and even allow you to set the position of the dialog?
Well, first lets change above to use a jQuery.UI dialog (and I show how in a bit).
So, that rather ugly built in browser dialog now becomes this:
The above is now much nicer. And even better is when you click on that button, the dialog pops RIGHT below and to the right of the dialog.(where you eyes are current looking since you just looked to click on that button!
You can try the above and play with a working demo here:
(it is vb.net + web forms).
Ok, so the next issue to tackle?
All of the current JavaScript (js) frameworks operation on NON BLOCKING code! That means that while confirm() does halt the code, does wait, and then continue when you hit ok (or cancel)? Such blocking code, or halt js code is not allowed anymore. About the only exception is alert() and confirm(). But, they can lock up and freeze the browser code, so they are not recommended anymore.
So, all such 3rd party - and even in the future? Non blocking code is to be used. That means the code is asynchronous.
so, if we call code to pop a dialog? The function will run, display the dialog AND THEN RETURN and EXIT!!!!
but now, that means we can't wait to return the true/false value to the button click (client side).
But, you can use a neat-o little trick!!!
You now (have to) do this:
You run the client side code - return false (server side button click does not run). then when the user makes their choice in the pop dialog, you then set a flag to return true and CLICK the button again!!! This time, the client side routine fires again, but returns true (assuming you hit ok!!!).
So, the code will look like this:
The button looks like this:
<button id="cmdDelete" runat="server" class="btn myshadow" style="margin-left:15px"
type="button"
onserverclick="cmdDelete_ServerClick"
onclick="if (!MyConfirmDel(this)) {return false};" >
<span aria-hidden="true" class="glyphicon glyphicon-trash"> Delete</span>
</button>
Note in above, I did not use a asp:Button, since I wanted the fancy looking bootstrap icon in the button (trash). But, how this works for a button, or asp.net button is the same. But I had to change the comfirm routine a bit, and use the above !MyConfirmDel(this) (the button combines both onclick and onserverclick - and they actually run as one js routine - we can't change that. If you using plain jane asp.net button, then the first markup will work the same.
and the js code now looks like this:
var MyConfirmDelOK = false
function MyConfirmDel(Btn) {
if (MyConfirmDelOK) {
MyConfirmDelOK = false
return true
}
var myDialog = $("#mydelpop");
myDialog.dialog({
title: "Delete Hotel",
width: "26em",
modal: true,
dialogClass: 'ui-widget-shadow',
position: { my: "left top", at: "right bottom", of: Btn },
buttons: {
Delete: function () {
myDialog.dialog('close')
MyConfirmDelOK= true
$(Btn).click()
},
cancel: function () {
myDialog.dialog('close')
}
}
});
Note careful in above. We passed the button, pop the dialog. User makes choice from dialog, and if ok/yes clicked, we set flag = true, and THEN click the button again. The button click runs the client side code again, but this time, it returns true, and thus the server side click code will now run.
If you are using a plain jane asp.net, then this:
<asp:Button id="cmdDelete2" runat="server" class="btn myshadow" style="margin-left:15px"
type="button"
onclick="cmdDelete_ServerClick"
OnClientClick="return MyConfirmDel(this)" >
</asp:Button>
You can try and play with a working demo here for above:
http://www.kallal.ca/WebSite11/WebForm2
Your next idea?
I want to "confirm" or "warn" or have some dialog if they actually changed things?
Don't!!! - just don't!!! If you do that, then EVERY place where you have edits, then for the sake of a consistant UI experiance, then you have to do that.
And it just becomes a "nag" for the users. (and you going to have to write quite a bit of extra client side code to make this work). When you pop a dialog, give them a save button, and a cancel button. Users will then make a habit of hitting the cancel button to go back. They will not care if they accident edit, since they can learn to always hit cancel if they did not mean to edit.
(you can see this UI in action in the above link).
You can have such a confirm dialog - but the less dialogs, the more enjoyable the software becomes. Give the users a cancel or un-do button. Let them decide if they want to bail out - not nag them all day long.
I have a widget that displays information with ObjectDataSource
I have a delete button with a delete command that sends the command directly to ObjectDataSource
I want to show a confirmation before deleting, clicking the confirm button will not do the deletion operation
Please help set up the postback
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkRemove" runat="server" CommandName="delete" OnClientClick="return confirms(this.name,'Do you want to delete the record?');"><img src="/icons/trash.png" title="Delete" /></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
my script:
function confirms(uniqueID,message)
{
var r = false;
$.confirm({
title: 'Alert',
content: message,
buttons: {
confirm: function () {
__doPostBack(uniqueID, '');
},
cancel: function () {
}
}
});
return r;
}
Only the page is refreshed and the record is not deleted
Like most if not all jQuery functions, they are NON BLOCKING. The code does NOT wait.
So, you have to wire up the call to the button AFTER you confirm.
this means:
OnClientClick="return confirms(this.name,'Do you want to delete the record?');"
Has to always return false. Eg:
OnClientClick="confirms(this.name,'Do you want to delete the record?');return false;"
the "confirms" routine WILL NOT WAIT and NOT block the js code.
Few exceptions exist when using js - say alert(), and confirm().
Start down the road of jQuery, or plug-ins, they are async code these days.
You can use the built in JavaScript "confirm" and that will work.
OnClientClick="return confirm(this.name,'Do you want to delete the record?');"
The above works, because the code halts. But jQuery code does not pause or lock up the browser - and for the most part, blocking code is VERY fast being removed from all browser code.
On the other hand, what jConfirm are you using? There is about 500 different ones - some perhaps MIGHT block the code, and perhaps you can have the function return true or false - but then again, we can only guess, right?
I don't see (or know) what "confirm" widget you are using. So, it is a wild guess on my part. And being busy? Don't think I can get back to this post until about 2-3 days from now. And Thanksgiving weekend is coming up. (hint: when you decide to use some js code widget, it would REALLY help if you noted which thinghy you are using).
One approach would be to fire you own post back (and you do seem to be going down that road - I see a postback in your code).
And when you do this, then you have to pick up the postback in the page load event.
(the __DoPostBack will NOT fire the server side (button code behind) events either). However, the post back does occur - and you can thus pick up what you pass with __DoPostBack in the forms on-load event.
So, you could say do this in your page load:
If IsPostBack then
If Request("__EVENTTARGET") = "Some Button Name ID" Then
' the 2nd parm of __DoPostBack can be picked up
' with Request("__EVENTARGUMENT")
End If
End If
So, you passing '' for the post back - you might want to say pass some PK ID or some such (say rowindex). However, you will NOT have any events of the grid view fire (such as row command, or index row changed event). So, you quite much have to pass some PK id or other means to figure out what row you are working on. And then call your routine from above to do whatever you need or want.
You can pass the current PK value with say this:
OnClientClick='<%# "myclick(this.name," + Eval("ID").ToString + "," +
Container.DataItemIndex.ToString + ");return false;" %>' />
In above, I pass all 3 values.
The control name,
a Eval("row from data source") (you can pick up any data column),
and also the grid index row value.
Thus the script for this would be:
function myclick(mycontrolname,MyPKID, MyRowIndex) {
alert('control name = ' + mycontrolname);
alert('My PK ID = ' + MyPKID);
alert('My grid row index = ' + MyRowIndex);
}
so, you can pick which of the 3 values you want to pass in your _DoPostBack command, but you have to catch that _DoPostBack in the forms page load event.
So, in theory, you can pass one of the above 3 values (or all of them). Then you pass back one of the above in your _DoPostBack and pick it up in the forms on-load event as per above.
I am attempting to submit form data from within a javascript popup window.
The popup opens fine and displays all elements as expected. When I click the asp:button, before inputting anything, it performs required field validation as expected. All of that is working just fine.
The problem I am having is in getting the OnClick to work and submit the form data.
To say I am a beginner to javascript/JQuery is an understatement, so requesting a bit of hand-holding on this one. Thanks in advance for the help.
<asp:Button ID="submitRes" CssClass="button" runat="server" ClientIDMode="Static" Text="Submit This Form Data" OnClick="Submit_Click" />
$( document ).ready(function() {
$(document).on("click", ".divPopupAdd", function(event){
showPopup()
});
function showPopup(){
$("#popup_add").dialog({
show: { effect: "blind", duration: 200 },
resizable: false,
modal: true,
width: 750,
height: 450,
open: function(){
$('#<%=submitRes.ClientID %>').click();
}
});
}
});
Ok, this is how you post back - click a button to run your code behind.
We going to lay this out step by step.
We assume, that you click a button on your form, and it launches the jQuery dialog? Beyond important here that this “is” in fact your flow in this form.
So, I’m going to assume user clicks a button, and the dialog is launched.
(and avoid that document on-ready. Please dump it).
Now the dialog is displayed. In that dialog, we assume user enters data, does whatever, and THEN either clicks a button for ok, or clicks a button to cancel?
Next up:
You CAN NOT do/use an asp.net button for a post back in that dialog. It WILL NOT work – end of story!
And if you did get the post back to work, it would mess up your current page “dom” that is holding the current web page, and also that of the dialog. It just does not work – don’t do it, ok?
(so no post backs in that dialog!!)
But we can still happy make this work!!!
So, how do we setup a button in the dialog?
Lets assume you need to click a button inside the dialog?
Button MUST be client side JavaScript (js).
That button will:
Close the dialog, and THEN do the post back.
So, we thus need this flow:
Display dialog on button click (client side js).
User does whatever in that dialog.
User now clicks on a button (say ok, or cancel).
We CAN and will for the sake of going crazy? We will drop on the form and use standard asp.net buttons. There is really no need to adopt HTML “input” buttons. (You can, but no real need here).
I am going to use BOTH built in jQuery buttons and ALSO two standard buttons in that dialog (the reason is many, but that way YOU can choose either way, and learn this. (boy, do I with someone had laid out how this works for me!!!). So, I going to save you much pain and suffering here.
So, lets start from the top:
Our button code to pop the dialog.
The asp button will be this:
<asp:Button ID="showdialog" runat="server" Text="Show the dialog"
OnClientClick="showpop();return false;"/>
Note close in above!!!!
We use OnClientClick=showpop();return false.
This will thus run js code, and NOT do a post back. The return = false is VERY important here, since if you leave that out, then the standard asp button will post back like it “normally” does. And we break our new rule!! – no post backs inside the dialog!!.
Ok, so that is the button to launch the dialog.
The js code to launch the popup is similar to what you have.
Eg this:
function showpop() {
var mydiv = $('#popdialog');
mydiv.dialog({
autoOpen: false, modal: true, title: 'My cool dialog, width: '30%'
});
// Open the dialog
mydiv.dialog('open');
}
Now, again for this example, we assume that you dropped some standard asp buttons in that dialog. But, OFTEN you want to use the built in dialog buttons. So let’s do BOTH for this example.
So, the above code thus becomes this:
function showpop() {
var mydiv = $('#popdialog');
mydiv.dialog({
autoOpen: false, modal: true, title: 'My cool other page', width: '30%',
position: { my: 'top', at: 'top+150' },
buttons: {
'ok': mycustomok,
'cancel': mycustomcancel
}
});
// Open the dialog
mydiv.dialog('open');
}
Note the two built in buttons we added. (ok, and cancel).
Note how I also broke out the 'setup' and THEN the one line that pops the dialog.
And in our pop up div, we also have two custom buttons. As noted, I am including both custom and built in dialogs. This will take you “hours” to get nice examples of this, and now you can choose either road.
So, our div has some text, and two custom buttons, and a simple text box.
The div looks like this:
<div id="popdialog" runat="server" style="display:none">
<h2>My cool pop dialog</h2>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:Button ID="MyOk" runat="server" Text="My custom ok"
OnClientClick="mycustomok();return false;"/>
<br />
<asp:Button ID="MyCancel" runat="server" Text="My custom cancel"
OnClientClick="mycustomcancel();return false;"/>
</div>
The only real thing to note in above is the style=”display:none” for the WHOLE “div”. This will thus hide the div, and it will not display when the page loads.
So, when we click on the button, it will show the dialog. You will see this:
So, we have our two custom buttons, and the two jQuery but in buttons. If you don't want the built in ones then use my first sample code. As I stated, your choice as to which buttons you want, like, or feel to use here. The main point is that BOTH sets of buttons do the SAME thing.
So, for this example, you click “ok”, or “my custom ok”.
Either way? We want our post back to run for that choice.
And for my custom cancel, or the built in cancel, we want code behind (server code) to run for that cancel choice. Do keep in mind you often don't need any code - but JUST the dialog to close and cancel and do nothing.
So, the two js stubs we have for this are:
function mycustomok() {
// first, close the dialog
$('#popdialog').dialog('close');
#('#HiddenOk').click();
}
function mycustomcancel() {
// first, close the dialog
$('#popdialog').dialog('close');
$('#HiddenCancel').click();
}
As noted, if you use BUILT IN buttons for the dialog, then you do NOT need the “close” of the dialog I have above. But your using (we assume) custom buttons, and not the built in jQuery ones.
And as noted, based on either choice, we will do a post back and run code behind. As I stated, the short way, easy way is to drop two hidden buttons on the form, they will look like this, and of course our outside the above div
<asp:Button ID="HiddenOk" runat="server" Text="hidden ok"
style="display:normal"/>
<asp:Button ID="HiddenCancel" runat="server" Text="hidden cancel"
style="display:normal"/>
Note careful (note BEYOND carefull here).
I have display = normal for the two buttons. The reason is that then with the designer you can double click on the button, and write your code behind. When you are DONE and have both code behind stubs written, then change the above display:normal to display:none to hide them. So we left the buttons visible for easy development. Once you wired up the code behind (simply double click on the buttons in the designer), then you are jumped to the code behind editor, and can write that code.
I have this for now:
Protected Sub HiddenOk_Click(sender As Object, e As EventArgs) Handles HiddenOk.Click
Debug.Print("dialog ok code run")
End Sub
Protected Sub HiddenCancel_Click(sender As Object, e As EventArgs) Handles HiddenCancel.Click
Debug.Print("dialgo cancel code")
End Sub
Of course you write whatever you need. As noted, you may well not need any code for the cancel button – you can leave that out of this design if you wish.
So, with the two code subs automatic wired up for you (this is why we all love asp.net forms, right???).
The “click” button trick as noted is rather nice. It solves a LONG list of issues, and does so with great ease.
You get the needed post back.
You get to run your own cute little code stub behind.
You don't have to write up ajax calls to do this!!!
So this follows the whole asp.net design pattern in which you drop buttons on a form, click them, and you get to run that nice little short code behind stub. And it all wired up automatic for you.
Thus in summary:
Don’t try + attempt post backs in the dialog – you REALLY can’t do this.
And you find they don’t work anyway! So buttons on that dialog WILL run “js” code.
you can use asp buttons if you want, just remember the extra return=false part.
If you must set/send some information to the server in that dialog before you close? Well, that is a different question and answer. But the jQuery "ajax" method works REALLY nice, and you can directly call functions in your existing web page code behind, and do so with next to no effort. You don't even have to know how to setup web methods - asp.net will do all the dirty work. but lets leave that example for another day.
Try the above idea - the "click" button trick in js really is the magic key to making this all oh so very easy to write and setup.
If you wish to call the JavaScript popup from Backend side you can use ClientScript.RegisterStartupScript
Syntax:
ClientScript.RegisterStartupScript(Type type, string key, string script, bool addScriptTags)
Parameters
Type: The type of the startup script to register. This is of type 'Type'
key: The key of the startup script to register. This is of type String
script: The startup script literal to register. This is of type String
addScriptTags: A Boolean value indicating whether to add script tags. This is of type bool - true or false.
You can write like this:
protected void Button_clilck(Object sender, EventArgs e){
ClientScript.RegisterStartupScript(GetType(), "Key","showPopUp(),true);
}
And if you want to call your pop up method from Front End side you can simply call the JS method in asp:button by writing OnClientClick
<asp:Button ID="submitRes" CssClass="button" runat="server" ClientIDMode="Static" Text="Submit This Form Data" OnClick="Submit_Click" OnClientClick="showPopup" />
I have looked around for a way of implementing this. Here is a pseudocode representation of what I have:
bool hasData = ItemHasData(itemid);
Confirm = "false"; // hidden variable
if (hasData)
{
//Code to call confirm(message) returns "true" or "false"
if (Confirm == "true")
{
//Delete item
}
else if (Confirm == "false")
{
return;
}
}
The code to call confirm uses a asp:Literal control and sets it equal to the confirm. I can get the popup but only after the function exits. And it does nothing with the conditions after that.
The general consensus seems to be that calling the javascript at that specific line is impossible (makes sense due to the server side/client side gap), but how can I achieve this? I tried using the ConfirmButtonExtender from the ASP.NET AJAX Toolkit but I couldn't interact with the confirmbuttonextender object from the code behind when the object is set to runat="server".
edit:
Sorry, I did miss those tidbits. Thanks Icarus.
The control itself is the GridView (the pseudo version is actually from the gvData_RowCommand function)'s rowcommand. The first check looks to see if the CommandName is DeleteItem and if so goes into this.
The gvData's columns are set based off a list of headers (and the dataset) passed as the table it is working against is for multiple items with different required information. The gvData's data is there, I just need to get a Yes/No (or in reality it'll end up being Ok/Cancel) dialog to verify they want to delete the item when there is data.
One method I end up using in some situations is to have a Panel that displays the Confirm / Cancel buttons. This avoids the need to handle JavaScript events and uses ASP.NET entirely.
<asp:Panel ID="pDeleteConfirm" runat="server"
CssClass="AlertDialog"
Visible="False">
<p>Do you wish to delete the selected record?<br />
<asp:Button ID="btDeleteYes" runat="server" OnClick="btDelete_Click" Text="Delete" />
<asp:Button ID="btDeleteNo" runat="server" OnClick="btDelete_Click" Text="Cancel" />
</p>
</asp:Panel>
<asp:GridView ID="gvData" runat="server"
AutoGenerateColumns="False"
CssClass="GridView"
DataKeyNames="ID"
DataSourceID="sqlData"
EmptyDataText="There is no data entered in the system."
OnRowDeleting="gvData_RowDeleting">
......
</asp:GridView>
I use the OnRowDeleting event to show the Panel
protected void gvData_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
// Show confirmation dialog
pDeleteConfirm.Visible = true;
// Select the row to delete
gvData.SelectedIndex = e.RowIndex;
// Cancel the delete so the user can use the confirm box
e.Cancel = true;
}
Handle the button Click events
protected void btDelete_Click(object sender, EventArgs e)
{
Button bt = (Button)sender;
switch (bt.ID)
{
case "btDeleteYes": // they confirmed a delete
sqlData.Delete();
break;
case "btDeleteNo": // they clicked cancel
// Do nothing
break;
default:
throw new Exception("Unknow button click in btDelete_Click");
}
// clear selection and hide the confirm box
gvData.SelectedIndex = -1;
pDeleteConfirm.Visible = false;
}
This isn't JavaScript but you can add in some UpdatePanels to do AJAX work on it.
Just one method to do it through ASP.NET rather than JavaScript handling.
The job of your code-behind is to render HTML out to the browser. It does this, and then the socket is closed and your server side code is no longer executing.
You'll have to implement this logic using a Javascript function on the client side.
Are you attempting to warn the user if there's data loaded before they take a certain action? Or perhaps before they try to leave the page? You'll have to popup the alert using script when that action happens on the page.