trigger a click event if confirmation message - c#

Here is my code :
<script type="text/javascript">
$(document).ready(function () {
$(window).bind('beforeunload', function () {
alert("unload");
if (closeIt())
$("#<%=Button1.ClientID %>").trigger('click');
});
function closeIt() {
var ans = confirm("save current layout ?");
if (ans) return true;
}
});
</script>
<asp:Button ID="Button1" runat="server" OnClick="btnSaveState_Click" style="display:none;" />
the new problem is that the confirm message is displayed twice on firefox and non of chrome

All the beforeunload handler is really supposed to do is return a string, which the browser then displays in an "are-you-sure-you-want-to-leave" dialog. If the user clicks OK, whatever navigation was about to happen occurs; if they click Cancel, it doesn't. Take a look here for more detail.
The usual thing to do here would be to display a message (by returning a string, not calling confirm yourself) along this lines of "You're about to lose the changes you've made to the current layout; are you sure you want to leave?" and then let the user themselves click Cancel and then Save, or OK if they don't care.
You're having issues, I expect, because you're trying to perform a postback in the handler, and the postback itself would cause an unload. I wouldn't be surprised if the browser deliberately stops this kind of behaviour in the handler, because malicious sites could use it to stop you leaving.
As per your update: only some browsers even allow beforeunoad behaviour. By the looks of it, Firefox does, so you get two dialogs - your confirm and then the browser default one. And by the looks of it, Chrome doesn't, so your event never gets called. (Or maybe Chrome just ignores the event if it does something unexpected, like post back.)

Try this:
<asp:Button ID="Button1" runat="server" Text="Button" CausesValidation="False" onclick="btnSaveState_Click" onclientclick="return confirm('Are you sure you want to delete?')" />
Alternatively you can look at the ASP.Net Ajax <ajaxToolkit:ConfirmButtonExtender>

Related

OnClientClick custom message box before Onclick event handler not working

I wanted to have a Custom Message Box OnClientClick . If the User Selects Yes then the Onclick Event handler in the C# code should get trigger. But somehow i am not able to do this using ASP.net and jquery.
As of now what is happening
Only C# code is triggered
What i was expecting
ClientSide Confirmation message (If User Clicks "YES" ) Then Server-side
code triggers.
My HTML
<form id="form1" runat="server">
<div>
<asp:Button ID="btnSubmit" Text="submit" runat="server" OnClick="btnDelete_Click"
OnClientClick="if(!ShowDeleteFormConfirmation()) {return false;};" />
</div>
</form>
Jquery
function ShowDeleteFormConfirmation() {
var confirmationMessage,
dlgButtons = {
"No": function () {
$(this).dialog("close");
return false;
},
"Yes": function () {
$(this).dialog("close");
return true;
}
};
confirmationMessage = "This form has already been assigned and will be marked as deleted.";
var $panelContainer = $("<div>" + confirmationMessage + "</div>").appendTo('body');
$panelContainer.attr("title", "Confirmation to delete a form");
var myPos = [$(window).width() / 2 - 100, 50];
$panelContainer.dialog({
modal: false,
draggable: false,
position: myPos,
button: dlgButtons
});
}
C# ////OnClick
protected void btnDelete_Click(object sender, EventArgs e)
{
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Delete button clicked.');", true);
} ```
This is what i wanted to implement.
http://jsfiddle.net/y5z01nbr/
Thanks for having a look.
Ok, while your code would work if you used "confirm" in the js code, that's because alert() and confirm() HALTS the calling code.
However, today, near ALL WEB code libraries are written to NOT halt, and NOT freeze up the browser. And jQuery is one such system. (it does not HALT the code). While I could introduce the concepts of await - that's becoming a wee bit too complex for this POST.
So, what this means:
the jQuery code does NOT halt,
and thus when you click on the button,
the client side code runs WITHOUT halting
and thus the button click (server side code) will
ALSO run right away - not waiting.
So, in the case of a jQuery dialog? You can't HALT the code. this means you have to flip this backwards. The jQuery dialog is to be displayed, AND THEN you have to call/run/click on that server side button. So, you have to add a new button, and use style="display:none" ot the existing button. Then display the dialog, and based on the answer you THEN click on (call) that origional button you have/had now.
The code will thus look like this:
<div>
<asp:Button ID="btnSubmit" Text="submit" runat="server" OnClick="btnDelete_Click"
Style="display:none" clientIDmode="static" />
<asp:Button ID="btnSubmitX" Text="submit" runat="server"
clientIDmode="static" OnClientClick="ShowDeleteFormConfirmation();" />
<\div>
So I dropped in another button - no server behind code. Hide first button with display none, and removed the client click. I moved the client click to 2nd button.
Now, we can do this:
dlgButtons = {
"No": function () {
$(this).dialog("close");
},
"Yes": function () {
$(this).dialog("close");
$('#btnSubmit').click();
}
};
So what we do is launch the dialog. And based on yes, then we click our button. If you choose no, then the dialog is dismissed, but no other action need take place.
A a GENERAL hard and fast rule?
Your browser code is RARE these days blocking code - calling a jQuery.ui dialog and in fact most of these newer UI controls? The code does NOT wait, does NOT halt. And this means you can't use the return true/false to control if the server side event stub will run or not (you can use js confirm(), but not jQuery, since it don't wait, nor halt the code).

Input "Reset" button fails to work when a grid is shown in page

I have an Advanced Find Form that contains a few asp:textbox controls as well as asp:dropdownlist controls. When I hit my reset button, it works well in clearing or resetting my textboxes and dropdownlists.
However, when the user clicks "Go" to submit the search query and a grid is shown with the results, the reset button no longer works.
Here's my input button:
<input type="reset" value="Clear All" />
EDIT:
Please note that I need to reset the fields to "Default Values" and also I need to be doing without a postback, to be doing it on client side
Have you try with normal button :
<asp:Button ID="txtResetbtn" runat="server" OnClick="txtResetbtn_Click" />
protected void txtResetbtn_Click(object sender, EventArgs e)
{
TextBox1.Text = string.Empty;
}
I think the problem is that HTML button don't reset after a postback
Or you can try to do this in the :
<asp:Button runat="server" ID="reBt" Text="Reset"
OnClientClick="this.form.reset();return false;"
CausesValidation="false" />
From : Reset a page in ASP.NET without postback
That link can help you too : Is there an easy way to clear an ASP.NET form?
The input type "reset" does not clear the form, it resets the form to its default values. To clear the form after submitting, you'll have to use javascript to set all values to empty.
Edit:
Since you're using asp.net, you could also use an <asp:button /> to call a method that clears the values of each control in the form.
Edit 2:
If you need to keep this function client-side, you'll have to use Javascript. Also, I think it's important to make a distiction between resetting to a default value (what the "reset" input type does) and clearing the values in a form, even after a submit. To do the later on the client side, you'll have to write a little javascript.
I managed to workaround the problem. As TenneC and Nikolay have mentioned in previous answers, I used an ASP.Net button but I've performed the reset function using jquery as follows:
$(document).ready(function(){
$("#BtnClear").click(function () {
$('#FillForm input').val(function () {
return this.defaultValue;
});
$('#DDLId1').val(function () {
return this.defaultValue;
});
$('#DDLId2').val(function () {
return this.defaultValue;
});
});
});

Trigger a click button event from another button, using javascript

I have an iframe in asp.net, using c# .
When the iframe with a story ends, I want to fire a button in parrent window via java script.
The iframe window has its own html file and js file. when the iframe come to an end of story
in the js file run
case "ClosePlayer":
trig();
break;
then I call another js file with a simple function
function trig()
{
//$('#<%= SubmitButton.ClientID %>').click();
//document.getElementById('<%= SubmitButton.ClientID %>').click();
document.getElementById("SubmitButton").click();
// alert("javascript here");
}
in the main window in asp.net I have this button
<asp:Button ID="btnProceedToNextTab" runat="server" Text="Proceed" OnClick="btnProceedToNextTab_Click" />
and I want to trigger this with another button via javascript, so the code behind of the function btnProceedToNextTab_Click(); run
<asp:Button ID="SubmitButton" runat="server" Text="Button" OnClientClick="trig();" />
Can anyone help me? I tried to use jquery, but nothing fires the button. The alert messages show ok, but the button is never clicked.
Try something like this in your parent window:
$("#iFrame").contents().find("#ButtonInIframeWindow").live('click',function(){
$('#submitButtonInParentWindow').click();
});
this is false:
document.getElementById("SubmitButton").click();
you have to use jQuery selector like this:
$("#SubmitButon").click();
Can you try this...
You can invoke your click function in javascript function..
document.getElementById('btnProceedToNextTab').click();
Please feel free to mark as answer if it satisfies....
ok found it.
from child (iframe window) call a js file with trig();
function trig()
{
var proceedButton = parent.$('[id$="btnProceedToNextTab"]');
proceedButton.trigger('click');
return false;
}
and in parent window:
<asp:Button ID="btnProceedToNextTab" runat="server" Text="Proceed" Style="display:none;" OnClick="btnProceedToNextTab_Click" />
works for me! thanks.

Message box cant be displayed When run in IIS?

I wants to display an OK/CANCEL message box. I have written like this.
if (MessageBox.Show("Are you sure you wants to Save the details ? ", "Validate", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
// do something if "OK "
}
it works locally well.. but on IIS shows an error " Showing a modal dialog box or form when the application is not running in UserInteractive mode is not a valid operation. Specify the ServiceNotification or DefaultDesktopOnly style to display a notification from a service application."
please help.
To get the effect I believe you are wanting, you'll want to use the Javascript confirm() function. It is typically used like this:
<asp:Button runat="server" OnClientClick="return confirm('Are you sure you want to save the details?')" id="btnSubmit" OnClick="btnSubmit_Click" />
This would, upon clicking the button, display a confirmation box that would stop the server OnClick event from firing if the user clicks No, Cancel, etc.
OnClientClick will render as an onclick event on the <input> tag. The rest is determined by the browser's handling of Javascript. See What's the effect of adding 'return false' to a click event listener? for more details on what the return value of the code in OnClick (OnClientClick) does.
Here's an example of the return values of confirm() using Chrome. For the first execution I had clicked Cancel.

OnClientClick postback even though return false on ie 7

Ive got ASP:Button on my page:
OnClientClick="return CheckTerms();" CssClass="submit" OnClick="BtnRegister_OnClick" />
OnClientClick I do js to check something and according to the check result I would like to proceed postback or not.
On all browsers it works (when false is returned from OnClientClick there is no postback) but situation is different on ie 7 where postback goes
any hints ?
thank You very much for help
for the sake of clarity here is the code that doesnt work either:
function CheckTerms() {
return false;
}
Here is how buttons are rendered:
IE
chrome:
<input type="submit" name="ctl00$plhMain$Register$btnRegister" value="??????????????????" onclick="return CheckTerms();" id="ctl00_plhMain_Register_btnRegister" class="submit">
Please notice that I use PIE.htc on my inputs but on button no.
what is this ? jQuery16100078065287469022415
My guess is that an error is being thrown in IE7, and when an error is thrown it doesn't return false, and it will continue on with the postback. You probably used a method that's not supported in IE7, or that acts differently than intended. Without the actual javascript method and any relevant markup I couldn't say for sure though.

Categories

Resources