OnClientClick and OnClick relation not working on second hit - javascript

good evening!
I've been trying to use the OnClientClick and OnClick for a validation process. It means, I use the OnClientClick for a confirmation, and proceed with the OnClick if confirmed. The "no" is always ok, we are focusing on the "yes". It all goes well on the first hit, but on the second hit, the PostBack can be seen (it means the "click" was effective), but no action is triggered on code behind.
Here is the code:
<script type="text/javascript" >
function testExample(button) {
var confirmed = confirm("Are you sure?");
return confirmed;
}
</script>
<asp:Button ID="btnButton" runat="server" OnClientClick="if(!testExample(this)) return false;" OnClick="btnClicked" Text="Button Test"/>
On code behind there is nothing done, just a check if it was triggered:
protected void btnClicked(object sender, EventArgs e)
{
return;
}
Adding or removingUseSubmitBehavior="false" does nothing, as also setting ClientIDMode="Static" or AutoID.
Changing to OnClientClick="return testExample(this);" also has no impact. If I do on other buttons individually it happens the same, they all run the first time, the second fails. I am trying to avoid the CssClass and pageLoad() relation.
Can you help me figuring out what I am doing wrong or missing?
EDIT: This seems to happen every time there is a postback event, even after removing OnClientClick. This Button is not inside any place holder.

Change your OnClientClick event to
OnClientClick="return confirm('Are you sure?');"

Related

Asp.net OnClick event and OnClientClick event used together

suppose i have a create form and with button save data in database and i want to show that save successfully in div tag using JavaScript but when i click button then at a time on event work but not 2 event onclick and onClientClick ..
<here>
<asp:Button runat="server" CssClass="myButton" Text="Registration" ID="btnRegistration" OnClick="btnRegistration_Click " OnClientClick="return YourJavaScriptFunction();" Height="65px" Width="184px" ClientIDMode="Predictable"></asp:Button>
and
<script type="text/javascript">
function YourJavaScriptFunction() {
$("#message").slideDown("slow");
// this will prevent the postback, equivalent to: event.preventDefault();
return false;
}
</script>
I want that first data saved then work onClientClick event in one asp:button
You need to tell your Javascript code to display the message on page load after the button click postback event happens.
There are many ways to accomplish this.
One way is through a session
aspx code with js:
<% if(Session["ShowMessage"] != null){ %>
$("#message").slideDown("slow");
<% } %>
Code behind on Page_Load:
Session["ShowMessage"] = null; //This will make sure that only the button will set the session state
on Button Click event
Session["ShowMessage"] = "Not null";
Another way is to Add Client Script Dynamically to ASP.NET Web Page
as #boruchsiper suggested, I will go for the second option by calling registerclientscriptblock after the postback has successfully completed.

Login control, set onauthenticate="LoginAuthenticateEvent" by keypress

I have asp.net Login control, example
<asp:Login ID="Login" onauthenticate="LoginAuthenticateEvent" runat="server"/>
have *.cs function like
protected void LoginAuthenticateEvent(object sender, AuthenticateEventArgs e)
{
//..
}
But it launch Authenticate only if pressing login Button, not by 'Enter' in textboxes.
So I decided to set this event to javascript 'onkeypress' event, and want it to check if keycode is 13(Enter) - and launch LoginAuthenticateEvent.
But there is problem - to call C# function from JS function should be static, but in my case it is 'protected void'
In JS(jQuery) code I get necessary element like this:
var tboxUserName = $("input[name*='UserName']");
I understand that there should be somethin like
tboxUserName.attr("onkeypress","....");
But how to hang there key check and make it such as onauthenticate="LoginAuthenticateEvent"?
upd.: Tried find control on Server side, and add TextChanged event there
TextBox tb = (TextBox)Login.FindControl("UserName");
tb.TextChanged += tbox_TextChanged;
But 'TextChanged' event fires only on focus change(when click other textbox or button) - but I need to check every keypress. So question is still opened.
Yes, solution from #Dave Becker is working. In my case my Login control is branded and there was not Button, but ImageButton. In this the code looks like this:
<asp:Panel ID="panelLogin" runat="server" DefaultButton="Login$LoginImageButton">
<asp:Login ID="Login" onauthenticate="LoginAuthenticateEvent" runat="server">
</asp:Login>
</asp:Panel>

How to trigger server side button's click event after javascript confirm dialog returns true

I have an input as count named in a repeater with hdncount hidden input. And I have a server side button out of repeater. This script runs onblur event of count. I want to trigger server side button's click event if client click OK button on confirmation. What should I do to do that?
<script type="text/javascript">
function changeItemCount(input) {
var hdnCount = $(input).closest("div").find("input[id=hdnCount]").val();
var crrCount = $(input).closest("div").find("input[id=count]").val();
if (hdnCount != crrCount) {
var answer = confirm("Ürün adedi değiştirilecektir. Onaylıyor musunuz?");
return answer;
} else {
return true;
}
}
</script>
How to fire a button click event from JavaScript in ASP.NET
var answer = confirm("Ürün adedi değiştirilecektir. Onaylıyor musunuz?");
if(answer)
__doPostBack('btnSubmit','OnClick'); //use the server-side ID of the control here
Use OnClientClick together with OnClick
<asp:Button ID="btnGo" runat="server" Text="Go" OnClick="btnGo_Click" OnClientClick="return CheckGoJSEvents();" />
EDIT:
Didn't see that you had the function execute on the blur event. In that case you would not use the OnClientClick of the button. The answer posted by mattmanser would be correct.
If you wanted it to function from the button click, then you could use this method. Sorry for the confusion.

how to call javascript and codebehind function on one link button click?

I want to call javascript function and click method on single asp button click event.
I was trying with following code.
<asp:LinkButton ID="lbtnSave" runat="server"
OnClientClick="myFunction();"OnClick="lbtnSave_Click">Save</asp:LinkButton>--%>
//javascript
function myFunction() {}
//code behind method
protected void lbtnSave_Click(object sender, EventArgs e) {}
Thanks in advance.
#Aparna, as you are using validation control than i believe those validation controls get triggered when button is clicked.
To overcome this issue there are two alternates either define validationgroup to your validation control or set the CausesValidation = false of your link button.
Example
<asp:LinkButton ID="lbtnSave" runat="server" CausesValidaion="false"
OnClientClick="myFunction();"OnClick="lbtnSave_Click">Save</asp:LinkButton>
Hope this will help !!
If you want call the js function on client click use this:
<asp:LinkButton ID="lbtnSave" runat="server" OnClientClick="myFunction()" OnClick="lbtnSave_Click">Save</asp:LinkButton>
to call it in a method(codebehind) u can use this:
ScriptManager.RegisterStartupScript(this,GetType(), "myFunction", "myFunction()", true);

Asp.Net button onclientclick problem

I have a strange problem, it seems, that I don't know how to solve.
I have a button like this:
<asp:Button ID="btnSave" runat="server" ClientIDMode="Static" Text="Save" OnClientClick="return ConfirmSave();" OnClick="btnSave_Click" />
If I write my client function like the following, it works as expected:
function ConfirmSave()
{
return confirm('Confirm?');
}
But I should check, inside the function, for the confirm result, like this:
function ConfirmSave()
{
if (Page_ClientValidate('validationGroup'))
{
var conf = confirm("Confirm?");
if (conf)
{
$('#btnSave').attr('disabled', 'disabled');
}
return conf;
}
else
return false;
}
Doing this, the page postback but the server click event doesn't fire.
Anyone could help? Thanks
use a timeout to disable the button immediately after your code completes and the submit happens.
if (conf)
{
setTimeout( function() { $('#btnSave').attr('disabled', 'disabled') }, 0 );
}
Yes its fires because the Button is input with submit type.
You probably have some other error, maybe a duplicate of your button id, in the page (because you have set it on static mode).
Check if you have give the btnSave again somewhere else, or check also if you have any JavaScript error.
(After your page is render, on your browser see the source code of your page and search for the btnSave id if exist more than one time)
If you have a method called btnSave_Click() in your code behind then when you post back it will fall into your Page_load method first and then fall into that method. If you are using VB then you will need a method with Handles btnSave_Click() after it.

Categories

Resources