Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a num pad,whenever I press any button it post back the page so how could I stop this ?? can anyone help me please ....send me code if you could so.
Posted
Comments
NAPorwal(8015059) 6-Nov-12 2:09am    
Is this a <asp:Button> ?
fahad.1 6-Nov-12 2:10am    
yes
NAPorwal(8015059) 6-Nov-12 2:16am    
<asp:button id="btnID" runat="server" text="Click Me" onclientclick="return false;" />
manognya kota 6-Nov-12 2:11am    
What do you mean by a numpad? Is it inside a panel or something?Also, any reason why you want to stop the post back?
fahad.1 6-Nov-12 2:14am    
I have tried it to put it inside the update panel but when I put this inside the oanel it does not type the numbers on text box..

On the aspx code, for the concerned button write
JavaScript
OnClientClick="return false;"

This will stop posting back the page. If you need to do some function call, like validation etc, you can do that too.
JavaScript
OnClientClick="MyFunction(); return false;"

Define a function which stores value in some variable
JavaScript
MyFunction()
{
myVariable = myVariable + "number you got from button";
return false;
}

If you need to do conditional stopping of postback you can write like
JavaScript
OnClientClick="MyFunction();"

And define function like
JavaScript
function MyFunction() 
{
if (condition == true)
return false;
else
return true;
}

In this case, if condition is true, it will NOT post back else it will post back.

Hope that helps. If it does, mark it as solution/upvote.

Thanks
Milind
 
Share this answer
 
v2
Comments
fahad.1 6-Nov-12 2:21am    
same problem here when I am using onclientclick="return false;" text box is not get any value.
karthik mushyam 23-Sep-13 2:10am    
thanks
fahad.1 6-Nov-12 2:30am    
because I have a numeric pad from where I ask the user to enter the password by clicking the numbers from the numpad so,on every click page gets postback which delays the response so I waant to stop this..I have used a textbox as well in which numbers can be entered and on code behind page every button event is declares.Lets say if I click on button 1 it takes 1 as an input and so as 2 for button click 2.but I want to stop the post back.how???
MT_ 6-Nov-12 2:46am    
Thats what I said in the psot Fahad. You call a function rather than simply saying return false. In the function, you save the value in some variable, keep concatenating that variable for each button. But return the function with "Return false". So it will NOT post-back...Okay?
Milind
MT_ 6-Nov-12 4:18am    
Does my explanation helped?
Hope it'll help u


C#
<asp:button id="btnID" runat="server" text="Click Me" onclientclick="return false;"  />
 
Share this answer
 
v3
Comments
fahad.1 6-Nov-12 2:21am    
same problem here when I am using onclientclick="return false;" text box is not get any value.
NAPorwal(8015059) 6-Nov-12 2:28am    
fahad, in so for as I understand,

u are fetching value of button and putting it in text box just like a calculator , isn't it if u r doing the same then where u have written the code for getting button value and putting in textbox in codebehind page or in javascript?
fahad.1 6-Nov-12 2:37am    
exactly the same thing I am trying to do :)
NAPorwal(8015059) 6-Nov-12 2:42am    
so where u r writting the logic in code behind or in javascript ?

1: if u r doing in code behind page then definitively i t'll need to get post back fired
2: if u r doing it in javascript then have to manage this problem in javascript only let me me where u putting ur logic
fahad.1 6-Nov-12 4:50am    
on code behide...
but if u could give me the code of javascript so plz send me
Hi,

As per my understanding, if your requirement is to stop the asp button click event not to be called, then you can use return false on your Onclientclick of the button.

<asp:button runat="server" ...="" onclientclick="return false"  />


Let me know if my understanding is wrong.
 
Share this answer
 
v2
Comments
manognya kota 6-Nov-12 2:25am    
Are you populating something on click of that button.?
fahad.1 6-Nov-12 2:30am    
because I have a numeric pad from where I ask the user to enter the password by clicking the numbers from the numpad so,on every click page gets postback which delays the response so I waant to stop this..I have used a textbox as well in which numbers can be entered and on code behind page every button event is declares.Lets say if I click on button 1 it takes 1 as an input and so as 2 for button click 2.but I want to stop the post back.how???
Step 1: in aspx page

a) in Head tag
----------------------------------------------------------------------------------

XML
<script src="Other/JScript.js" type="text/javascript"></script>

    <script src="JSTemplate/jquery-latest.min.js" type="text/javascript"></script>

    <style>
        .btn
        {
            border: solid 1px gray;
            border-radius: 3px;
            padding: 5px;
            vertical-align: middle;
            text-align: center;
            text-decoration: none;
            color: #000;
        }
    </style>




----------------------------------------------------------------------------------

b) in body tag
---------------------------------------------------------------------------------

 <div>
        <br />
        Operation to be performed:  <asp:textbox id="txtOperation" runat="server" class="btn" xmlns:asp="#unknown">
            Width="20" ReadOnly="true"></asp:textbox>
        <br />
        <asp:textbox id="txtBoxInput" runat="server" width="200" height="25" xmlns:asp="#unknown"></asp:textbox>
        <br />
        <br />
        <a href="javascript:TraceClick('1', 'idA1');" id="idA1" class="btn">1</a> <a href="javascript:TraceClick('2', 'idA2');">
            id="idA2" class="btn">2</a> <a href="javascript:TraceClick('3', 'idA3');" id="idA3">
                class="btn">3</a> <a href="javascript:TraceClick('4', 'idA4');" id="idA4" class="btn">
                    4</a> <a href="javascript:TraceOperator('+', 'idAPlus');" id="idAPlus" class="btn">+</a>
        <a href="javascript:TraceOperator('-', 'idA4Minus');" id="idA4Minus" class="btn">-</a>
        <br />
        <br />
        <br />
        <asp:button runat="server" id="btnFetchValueinCodebehind" text="Get Value In code behind" xmlns:asp="#unknown">
            OnClick="btnFetchValueinCodebehind_Click" />
        <br />
    </asp:button></div>

---------------------------------------------------------------------------------

Step 2: in JScript file
-----------------------------------------------------------------------------

<pre lang="cs">function TraceClick(new_Value, objID) {

      document.getElementById(objID).style.color = 'red';
      var finalVal='';

      if($("#txtBoxInput").val().length>0)
      {
         var newVal=parseInt(new_Value);
         var  vInput=parseInt($("#txtBoxInput").val());

         if($("#txtOperation").val()=="+")
         {

             finalVal = vInput + newVal;
             $("#txtBoxInput").val(finalVal);

         }
         else if($("#txtOperation").val()=="-")
         {

             finalVal = vInput - newVal;
             $("#txtBoxInput").val(finalVal);

         }
        else
        {

        $("#txtBoxInput").val($("#txtBoxInput").val().concat(new_Value));

        }


      }
      else
      {
         $("#txtBoxInput").val(new_Value);
      }

}
function TraceOperator(new_Value, objID) {
   document.getElementById(objID).style.color = 'red';
     $("#txtOperation").val(new_Value);

}



------------------------------------------------------------------------
step 3: in code behind page
------------------------------------------------------------------------
C#
protected void btnFetchValueinCodebehind_Click(object sender, EventArgs e)
  {
      string strval = txtBoxInput.Text.ToString();
      Response.Write("here u get value in code behind page: "+strval);
  }





Let me know if this concept could help u..
best luck
 
Share this answer
 

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