How to ask the user for confirmation of an action with a popup box? - asp.net

I'm currently coding a button which will delete a record from a database if clicked. However, I want some sort of confirmation after they click it so there are no accidents. I was imagining a popup box that tells them they are about to delete this record with two buttons cancel and OK. If cancelled, nothing happens, but if OK is pressed, then the server would execute the delete as coded. How would I do this in asp/vb?

Here is a button with a confirmation message :
<asp:Button runat="server" ID="btnDelete"
OnClick="btnDelete_Click"
OnClientClick="return confirm('Do you want to delete the record ? ');" />
Here is the way to add confirm client script at server side :
btnDelete.Attributes.Add("onclick",
"return confirm('Do you want to delete the record ? ');")

Adding Client-Side Message Boxes in your ASP.NET Web Pages is a great tutorial to make client-side confirmation messages like this:
Using ASP.NET & VB.NET (Don't forget to read part2.)

Related

asp.net textarea postback before submit button

I have an ASP.NET form with a few controls and a submit button at the bottom, all inside an update panel:
<asp:UpdatePanel runat="server" ID="upContent">
<ContentTemplate>
<asp:TextBox runat="server" ID="tbxMyTextBox" AutoPostBack="true" />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClientClick="return doStuff()" OnClick="btnSubmit_Click" />
</ContentTemplate>
</asp:UpdatePanel>
If I write something in the TextBox and click 'submit' immediately (without clicking out of the TextBox first), the changes are not recorded (as seen in the server-side event handler). However, if I write something in the TextBox, and then change focus to another control, an AutoPostback happens through the UpdatePanel and then clicking 'submit' does recognize these changes. How can I force this content to update right as I click the submit button, while still running both the clientside and serverside events attached to it? Thanks!
Is is possible that your text-box gets cleared due to some on-submit/on-click event handler? I will suggest you do use some tool such as Fiddler to inspect the POST data in request. Or you can put a break-point at your server side code and inspect the request data. Look for particularly Request.Form[tbxMyTextBox.UniqueID] - i.e. look for presence of value for your text-box's name (name property at client side which corresponds to UniqueID property at server side). If the request contains the value typed in the text-box then something is happening on server side but good news is you can always retrieve the value from Request object. If the value is not present in the Request object then it means that client side code is clearing the value before the submit.
If an onclick method returns false, it cancels the action that the button would normally perform. As your button would normally submit your form, but it appears not to be submitting, your client side javascript code in doStuff is probably returning false.

From client, force whole page validation

I have an ASP button for which I have set the OnClientClick property to display a javascript confirm message. However, I only want this message to be displayed AFTER all of the client side validations have passed.
How can I do this? Essentially, I believe that I need to force Page level validation from the client and then, only if it passes, display the confirmation box.
If you're using the ASP.NET validation controls add an OnClientClick like this to your submit button...
<asp:Button ID="blah" OnClientClick="if(Page_ClientValidate())return confirm('your message')" OnClick="your submit method" Text="submit" runat="server" />
...Page_ClientValidate() will return true if the page is validated then you need to return the results of your "confirm" in order for the form to be submitted.
There's something along these lines available here: http://www.codeproject.com/KB/aspnet/JavascriptValidation.aspx

Asp.net regularexpressionvalidator fires even when CausesValidation="false"

I have a regularexpression validator which validates a valid email. I have two buttons on my form. Submit and undo.
On undo, we are reverting the page state to default
submit has it validationgroup set while undo doesnot have any validationgroup and CausesValidation="false".
Now when i navigate to page and enter invalid emailaddress,i directly click undo. the validator fires and stops my page from posting.however if i press tab and navigate to other control and then click undo,the validator shows error message but posts back and furthur proessing is done.
This is very strange and i want the page to postback without any error message when i click undo.how to achieve it.
<tec:ThemedImageButton runat="server" ID="imgbtnSave" OnClick="imgbtnSave_Click"
ValidationGroup="CustomerGroup"/>
<tec:ThemedImageButton runat="server" ID="imgbtnCancel" CausesValidation="false"
OnClick="imgbtnCancel_Click" />
These are normal image buttons with added themes.CustomerGroup is the validation group for my textbox and regularexpressionvalidator
Are you possibly clicking the undo whilst your focus is still within the email textbox? If so, perhaps the issue is that the initial blur event of the textbox is firing which in turn is calling the email validator and preventing the click / submission.
The first thing to try is to make sure you can enter an invalid email address, move the focus onto a completely different area of the page and then press undo to determine if this is the cause.
Double check your settings.
You might want to check the code behind to make sure you are not overwriting the control settings there. For example, in code, you might have the CausesValidation set to "True" or something like that.
Until you post more information you could do the following in your Undo button:
<asp:Button ID="btnUndo" runat="server" Text="Undo"
OnClientClick="Page_ValidationActive = false;"
OnClick="btnUndo_Click" />
I don't recommend this as it might solve your problem but really doesn't explain what is wired up incorrectly but the Page_ValidationActive = false; will disable all client validation on your page.

Preventing Double Clicking with Server Side Validation

I am trying to prevent users from double clicking on the submit button for a page I'm updating. The problem is that there is server side validation that has to be done to determine if the user has to do any updates before we can move on. I currently have the following code on my submit event for my form.
<asp:Button ID="SubmitPayment" runat="server" OnClientClick="this.disabled='true';" UseSubmitBehavior="false" OnClick="SubmitPayment_Click" Text="Submit" />
Where this becomes an issue is where the page does not pass validation. In this instance everything I have tried to enable the button has failed. It's hard for them to resubmit corrected information with the submit button disabled. I have tried both of the following methods at the end of my validation to re-enable it.
SubmitPayment.Enabled = true;
SubmitPayment.Attributes.Add("disabled", "false);
What must I do to re-enable this control? This is in Visual Studio 2005. Thanks in advance.
-edit-
The approach that I used was as follows. I added an additional decoy button that was disabled and had the display css property set to none. I then used javascript that would hide the submit button and show the decoy button in the OnClientClick event. Finally, the end of the server side method would update the css properties on the buttons to hide and show them afterwards.
Code segments are as follows. Thanks again for the help
function PreventDoubleClick()
{
var sp = document.getElementById("SubmitPayment");
var db = document.getElementById("DecoyButton");
sp.style.display = "none";
db.style.display = "inline";
}
<asp:Button ID="SubmitPayment" runat="server" OnClientClick="javascript:PreventDoubleClick();" UseSubmitBehavior="false" OnClick="SubmitPayment_Click" Text="Submit" />
<asp:Button ID="DecoyButton" Enabled="false" runat="server" style="display:none;" Text="Please Wait..."/>
DecoyButton.Style.Add("display", "none");
SubmitPayment.Style.Add("display", "inline");
I have had a similar problem before and the best solution I came up with was to hide the button and replace it with a please wait (or validating) message. If it fails validation you can then show the button again

How to display a Yes/No style message box in web part, and get client's choice?

I want to show a message box when users trying to do any major operations in my web part. If user choince [Yes], then continue to do sometiing ...[No] for nothing.
How can i implements this function in my web part , Sharepoint 2007 ??
pls help me ~~
thx
If you're using an ASP.Net control like a Button, you can add the following attribute to give the user a JavaScript popup before the postback actually happens:
<asp:Button runat="server" ID="Button1" OnClick="Button1_Click" OnClientClick="return confirm('Are you sure you want to do this?');" />
You can also set this property on the button object in your code:
Button1.OnClientClick = "return confirm('Are you sure you want to do this?');";

Resources