Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi to all,
Here i want to call two javascript in single onclientclick event two are different scripts one is normal script and another one is return(true or false).

code is attached below:

<asp:Button ID="cmdSave" ValidationGroup="purchaseval" runat="server" Text="" CssClass="savebutton1231" OnClientClick="ClientSideClick(this);return ConfirmNEW();"
                                                                                                               EnableTheming="false" OnClick="cmdSave_Click" SkinID="skinBtnSave" />


above button not fire cmdsave_click.bcz one script want to return and another is a normal script how to differentiate this two scripts

this is my scriptS:-
function ClientSideClick(myButton) {

// if (typeof (Page_ClientValidate) == 'function')
{
// var isPageValid = true;

var isGrpOneValid = Page_ClientValidate("purchaseval");

if (isGrpOneValid == true) {
if (myButton.getAttribute('type') == 'button')
{
alert(isGrpOneValid);
myButton.disabled = true;
myButton.className = "btn-inactive";
myButton.value = "processing...";

}
}
}
}
above script for multiple click avoid

C#
function ConfirmNEW() {

           if (Page_ClientValidate()) {

               var GridId = "<%=grvStudentDetails.ClientID %>";
               var grid = document.getElementById(GridId);
               rowscount = grid.rows.length;

               // var row = drpPrd.parentNode.parentNode;
               // var rowIndex = row.rowIndex - 1;
               // for (var i = rowIndex; i <= rowIndex; i++) {

               for (var i = 1; i < rowscount; i++) {
                   //alert(i);
                   var indexID = 01 + i;

                   var vatch = document.getElementById('ctl00_cplhControlPanel_tabs2_TabPanel2_grvStudentDetails_ctl' + ((indexID < 10) ? '0' + indexID.toString() : indexID.toString()) + '_txtVATPre').value;
                   var cstch = document.getElementById('ctl00_cplhControlPanel_tabs2_TabPanel2_grvStudentDetails_ctl' + ((indexID < 10) ? '0' + indexID.toString() : indexID.toString()) + '_txtCSTPre').value;
                   //alert(vatch);
                   __doPostBack('cmdSave', "")
                   if ((vatch != "" && vatch <= 0) && (cstch != "" && cstch <= 0)) {
                       if (confirm("VAT(%) and CST(%) are Zeros(0) in row" + i + ".Do you want to Continue?")) {

                           return true;
                       }
                       else {

                           return false;
                       }
                   }
               }
           }
       }


this for confirm purpose.
Posted
Updated 23-Jul-15 23:47pm
v3
Comments
Praveen Kumar Upadhyay 24-Jul-15 4:21am    
If your second function ConfirmNEW() will return false then it will not execute cmdSave server side button click event.
JOTHI KUMAR Member 10918227 24-Jul-15 4:33am    
hmm yeah it return false how to resolve this any idea
Praveen Kumar Upadhyay 24-Jul-15 5:11am    
Below I am posting my solution.
JOTHI KUMAR Member 10918227 24-Jul-15 5:47am    
i improve my question.kindly see that i deploy all my script using

Hello,

In ASP.net,It Executes ClientSide methods first and then it'll call server side methods.

If function ConfirmNEW() will return false then it will not execute cmdSave server side button click event but You wan't to Execute cmdSave at Server side then use __doPostBack() Event.

 __doPostBack('cmdSave', '');  within function ConfirmNEW() before return true or false.
 
Share this answer
 
Comments
JOTHI KUMAR Member 10918227 24-Jul-15 5:24am    
where i use dopostback .bcz im new for this so kindly help me
JOTHI KUMAR Member 10918227 24-Jul-15 5:48am    
i improve my question.kindly see that i deploy all my script using
Anil Vaghasiya 24-Jul-15 6:24am    
Just Refer the Example :-

<script>
function ConfirmNEW(obj) {
if (confirm('You may lose your changes if not saved.\nPlease press Save before navigating.\n\nAre you sure to continue?')) {
__doPostBack(obj.id, '');
} else {
return false;
}
}
</script>

<asp:Button ID="cmdSave" ValidationGroup="purchaseval" runat="server" Text="test" CssClass="savebutton1231" OnClientClick="ClientSideClick(this); ConfirmNEW(this);" EnableTheming="false" OnClick="cmdSave_Click" SkinID="skinBtnSave" />

Anil Vaghasiya 24-Jul-15 6:39am    
Hello,

It works definately first call the method ClientSideClick() after call ConfirmNEW() and After call "cmdSave_Click" server side Methods.

Just Check Below Example.

<asp:Button ID="aspbtn" runat="server" OnClientClick="ClientSideClick(this);confirmNew(this);" OnClick="aspbtn_Click" Text="Press Here">

<script>
function ClientSideClick(obj) {
alert(obj.id, 'Test Called ClientSideClick Methods');
}
function confirmNew(obj) {
alert('Test Called ConfirmNew Methods');
if (confirm('You may lose your changes if not saved.\nPlease press Save before navigating.\n\nAre you sure to continue?')) {
__doPostBack(obj.id, '');
} else {
return false;
}
}
</script>

<asp:Label ID="lbl" runat="server">
protected void aspbtn_Click(object sender, EventArgs e)
{
lbl.Text = "Called ASP Button";
}
Anil Vaghasiya 24-Jul-15 8:17am    
Hello, if you got the answer then Please Marked answer as Solution & +5 vote.
Ff you want to execute server side button click event irrespective of whether 2nd javascript function i.e. ConfirmNEW() returns true or false, then change your code to below.

remove return statement

ASP.NET
<asp:button id="cmdSave" validationgroup="purchaseval" runat="server" text="" cssclass="savebutton1231" onclientclick="ClientSideClick(this);ConfirmNEW();" xmlns:asp="#unknown">
                                                                                                                   EnableTheming="false" OnClick="cmdSave_Click" SkinID="skinBtnSave" /></asp:button>
 
Share this answer
 
Comments
JOTHI KUMAR Member 10918227 24-Jul-15 5:28am    
i need return statement for this script
Praveen Kumar Upadhyay 24-Jul-15 5:31am    
What you will do with the return value?
F-ES Sitecore 24-Jul-15 5:43am    
Have "ConfirmNEW" update a hidden field with the return value. In the postback of the page you can then read that hidden value from the form. Google "asp.net javascript update hidden value" and "asp.net read values from form" if you don't know how to do either of those things.
JOTHI KUMAR Member 10918227 24-Jul-15 5:40am    
for conforming purpose if i conform it it wills ave or otherwise it allow to save
JOTHI KUMAR Member 10918227 24-Jul-15 5:48am    
i improve my question.kindly see that i deploy all my script using
You can aggregate your functions inside wrapper function and make them all self executing, like the script example below:

[^]

HTML
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">

    <script type='text/javascript'>
      function go()
      {
        var result;

        console.log('in');

        // first call
        (function go1(){
           console.log('go 1');
        })();

        // second call, with return
        (function go2(){
           console.log('go 2');

           //set your return value here
           result = false;
        })();

        // just to view the result
        document.getElementById('result').innerHTML = 'Result: '+result;

        //returned value
        return result;
      }
    </script>
  </head>
  <body>
    <a href="http://example.com" onclick="return go();">Link</a>

    <label id="result">
  </body>
</html>


Output:
"in"
"go 1"
"go 2"
Result: false

if you change result to true, it will navigate away so the return value is working
 
Share this answer
 
v2
Comments
JOTHI KUMAR Member 10918227 24-Jul-15 5:48am    
i improve my question.kindly see that i deploy all my script using

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900