I have an asp:button called "Delete" and what I want to do is have a JavaScript confirm popup with the options "Yes" and "No". If "Yes" then delete a record from a SQL DB else if "No" then nothing should happen.
<asp:Button runat="server" ID="btnDelete" Text="Delete"
OnClientClick="if(confirm('Delete?'))alert('You chose yes!');else alert('You chose no!')"
OnClick="btnDelete_Click" />
btnDelete_Click contains SQL delete logic.
The problem I am having is that the OnClick method always gets executed. Whether you pick "Yes" or "No" from the JavaScript popup the record gets deleted regardless. It seems to always cause a postback.
Can I somehow get the "Yes" or "No" result into my code behind so I can actually do a simple "if" statement for the delete logic?
Try this, on your javascript do this
var result = Confirm("Are you sure . . . . ");
if(result)
return true;
else
return false;
what it does is if it's true, it should postback your code, else it'd cancel your click.
That should be:
<asp:Button runat="server" ID="btnDeleteSite" Text="Delete" OnClientClick="return confirm('Format Delete?');" OnClick="btnDeleteSite_Click" />
You should ideally put JS event handlers in the script tag, and call them from your OnClientClick event in the button definition. Like so:
<script type="text/javascript">
function ConfirmDelete() {
return confirm("Delete?");
}
</script>
And then in your button's OnClientClick event, you do this:
OnClientClick="return ConfirmDelete()"
This keeps your markup clean and readable, and will also prevent the postback from happening if the user chooses 'No'.
Try with this
<asp:Button runat="server" ID="btnDelete" Text="Delete" OnClientClick="if(confirm('Delete?')){return true;}else {return false;} OnClick="btnDelete_Click" />
When you are using OnClientClick event with JS confirm dialog box at that time you need to return true or false. If it will return True then OnClick(Server side) event will fire otherwise it will not.
Related
I have an asp.net linkbutton, which contains the OnClientClick property, however the function within the OnClientClick never gets called, it directly jumps to OnClick function.
Below are the 2 ways I am using LinkButton as:
<asp:LinkButton ID="btn" CssClass="button" Text="Browse Thumbnail" runat="server"
OnClientClick="return confirm('Are you sure you want to delete?');">
</asp:LinkButton>
and:
<asp:LinkButton ID="lnkDelete" runat="server"
OnClientClick="return confirm('Are you sure you want to delete this slide?');"
CommandName="DeleteThumbnail" CommandArgument='<%# Container.DataItemIndex %>'>
<asp:Image ImageUrl="~/images/delete.gif" ID="imgDelete" runat="server"></asp:Image>
</asp:LinkButton>
Both of the approaches does not works.
Can anyone please provide some solution for the same.
OnClientClick="javascript:return confirmAction();"
function confirmAction() {
if(confirm('Are you sure you want to delete?')) {
// you clicked the OK button.
// you can allow the form to post the data.
return true;
}
else {
return false;
}
}
implement the Onclick on the server side
protected void lnkdelete_Click(object sender, EventArgs e)
{
}
and if you dnt want to call server method use this
OnClientClick="javascript:confirmAction(); return false;"
There is most probably some other page element that is preventing this event from being fired.
Do you have any other page elements that might interfere? Have you tried removing all other page elements but this one? Do you have some AJAX calls that might interfere? Have you tried this with a simple html element (not asp.net)?
You are most probably doing everything fine in your link button but it seems like problem is elsewhere.
Place it in single quotes like below,
<asp:LinkButton ID="btn" CssClass="button" Text="Browse Thumbnail" runat="server" OnClientClick="return confirm('Are you sure you want to delete?');"></asp:LinkButton>
Use like this
function Navigate()
{
javascript: window.open("microsoft.com");
return false;
}
and on clientclick as follows
OnClientClick="return javascript:Navigate()"
or even
function Navigate()
{
window.open("microsoft.com");
return false;
}
OnClientClick="return Navigate()"
Seems like you have Disabled the javascript in IE. Just enable it & you are good to go. You can follow this post to enable/disable the javascript in IE:
http://browsers.about.com/od/internetexplorertutorials/ss/disable-javascript-ie9.htm
There is no problem with your OnClientClick method it should prompt confirm window. but you haven't specify the onclick event of the link button. So you will not able to get the event from code behind.
You may need to enable java scripts for your browser
How to enable JavaScript in a web browser?
Are you using Ajax toolkit? Update panel? then you need to register script by using script manager
To inject a confirm script from a AJAX postback,
ScriptManager.RegisterOnSubmitStatement(btn, Page.GetType(), "confirm", "return confirm('Are you sure');");
I have a button that, when an user clicks on it and some fields are not correct, a message will be displayed. I would like to make that validation with javascript(or maybe someday I will), but I have no idea how to make that. Currently when the user clicks the button, the onclick function on the server side wont be executed.
Just use <asp:Validator tags; there are a number of different types of validators such as required field validators regex validators, etc.
They will validate the data both in client side code as well as server side code (unless you disable the client side check, which you can do).
This is much easier than manually validating the content in javascript and also manually validating it in server side code.
Technically though, to answer your question, when you have an onclick javascript handler for a submit button the return value, as a boolean, indicates whether or not the form should be submitted, so you just need to return false if the data is not valid to not submit the form.
<asp:Button Text="Submit" OnClientClick="return Validate();" />
<script type="text/Javascript">
function Validate()
{
if(requiredFieldAIsMissing) return false;
return true;
}
</script>
How about:
<asp:Button ID="myButton" Text="Click Me!" OnClick="myButton_OnClick" OnClientClick="javascript:confirm('Really?')" />
The OnClientClick property emits the javascript (ahead of the javascript required to handle the postback), and if you return false from that event the rest of the event is cancelled and the postback will not fire.
I think something like below would help you to understand.
HTML:
<asp:textbox id="myText" runat="server" text="Hello" />
<asp:button id="myButton" runat="server" text="Click Me" OnClick="Server_Event" />
ON Page_Load:
//Add onclick with return here
myButton.Attributes.Add("onclick","return ButtonClicked()");
Javascript:
function ButtonClicked(){
var txt = document.getElementById("myText");
if(txt.value != "Hello"){
alert("It is not Hello, I am NOT posting back!");
return false;
} else {
alert("It is Hello, I am posting back!");
return true;
}
}
I working on an ASP.Net C# application, I wanted to create a Button Control, when user click on the button, a JavaScript confirm popup, then get the Boolean value from the user (Yes/No) to perform further actions in the button onClick event.
my current approach was added OnClientClick and OnClick event in the button, where OnClientClick trigger JavaScript function and the (Yes/No) value is store into HiddenField Control to make use during OnClick event.
It is something like the following code fragments:
function CreatePopup(){
var value = confirm("Do you confirm?");
var hdn1 = document.getElementById('hdn1');
hdn1.Value = value;
}
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" OnClientClick="CreatePopup()"/>
<asp:HiddenField ID="hdn1" runat="server" />
Is there any better approach in order to do this? thank you in advanced.
Change your CreatePopup() function to return a boolean:
function CreatePopup()
{
return confirm("Do you confirm?");
}
And then ensure you return that from the OnClientClick() in the button:
<asp:Button ... OnClientClick="return CreatePopup();" />
Using that method, the OnClick() method will only fire if the OnClientClick() method returns true.
Why do you want to store the value of the JavaScript confirmation in the hidden field? Just simply return false in your JavaScript function when the confirm value is 'no' , to prevent the form from submitting. When cnfirm value is yes, return true to allow the form to be submitted. In your code behind in the button_click method you don't have to check what happened in your JavaScript confirm, since form will never be submitted if the user said no.
Hi im looking to find out after I do this:
c#
div.Attributes.Add("onclick", "return confirm_delete();");
javascript:
function confirm_delete()
{
if (confirm("Are you sure you want to delete this comment?")==true)
return true;
else
return false;
}
It pops up with a message box with yes and no if I press yes how can I add a function into my c# how can I call it?
If yes go to c# code behind
and add in confirm_delete
if yes was clicked
do this
I need it so when some one clickes yes I can (from code behind in asp.net c#) add in a method to delete from mysql database.
Just dont know how I refrence to the javascript
public static string confirm_delete()
{
return something (dont know how to)
}
How about this add a button with the clientside click event instead of adding it in the code behind. Check to see if the popup returns true. If you return false it should prevent the server side event from firing.
<asp:Button ID="btn" OnClientClick="if(confirm_delete()){
/* post back*/
}else{
return false;
};" OnClick="btnDelete_Click" runat="server" Text="delete"/>
protected void btnDelete_Click(object sender, EventArgs e)
{
//run your serverside code if confirm was pressed.
}
Your JavaScript will need to call a page on your server. This is typically accomplished with AJAX and a web service.
This page on MSDN has a good overview of what's involved.
If you're interested in using jQuery, this page has a simple example.
You may be able to use a PageMethod to call your codebehind function, check out the following link for an example
ASP.NET Ajax PageMethods
Wondering why you are using a div? Can't you use asp.net Button / LinkButton and limiting it's postback based on confirm value?
e.g.
<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False"
CommandName="Delete" Text="Delete"
OnClientClick="return confirm('Are you certain you want to delete this
product?');">
</asp:LinkButton>
Reference.
I've the Javascript code to do a confirmation before deletion of some records
function confirmDelete()
{
if(confirm('Delete all?'))
{
return true;
}
else
{
return false;
}
}
I've the button code here
<asp:Button ID="btnDelete" runat="server" onClientClick="return confirmDelete();" onClick="btnDelete_click" />
If i've the button outside an update panel (basically i'm using RadAjaxPanel by Telerik) it is working fine. But when the button is inside an ajax panel, even if i click OK for deleting the records the server side code is not called.
Any ideas?
Your code is being prepended to the click handler that posts the action back to the server. You need to check if your result returns true/false then optionally continue executing the server-generated code.
<asp:Button ID="btnDelete" runat="server"
onClientClick="if (!confirmDelete()) { return false; }"
onClick="btnDelete_click" />
This will result in client-side HTML like
<input type="submit" id="btnDelete"
onclick="if (!confirmDelete()) { return false; } __DoPostBack...
allowing the server-generated code to be executed (i.e., no return is evaluated unless the confirmation fails).