Click here to Skip to main content
15,891,847 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Scenario description:
1) Enter password value into an asp.TextBox.
2) Press the RETURN button to act like the asp.Button: btnSubmit.
ASP.NET
<asp:TextBox ID="txtPasswordNew" runat="server" Font-Size="Small" 
	AutoPostBack="true" TabIndex="2" 
        ToolTip="6 - 30 valid ASCII characters; Case Sensitive"
        onkeydown="submit(event);">
</asp:TextBox>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" Font-Size="Small"
	onclick="btnSubmit_Click" />

<script>
   function submit(event) {
        if (event.keyCode == 13) {      // RETURN key
           // implement btnSubmit_Click()
        }
   }
</script>
.....
// -- Code behind --
protected void btnSubmit_Click(object sender, EventArgs e) {
	// Validation code
}

My Q here is in JS submit(), how a call can be made to implement the code in btnSubmit_Click()? Thanks for your help.
Posted
Comments
F-ES Sitecore 27-Mar-15 10:17am    
Just call the click method of the button in javascript and that will cause the event to be called. If you're looking to do this via ajax, ie with no page refresh, then put the controls inside an updatepanel.

The client side code (here js) can prevent execution of server side by return true/false.

You can use OnClientClick="return func1();"
C#
<asp:button id="btnSubmit" runat="server" text="Submit" font-size="Small" xmlns:asp="#unknown">
	onclick="btnSubmit_Click" OnClientClick="return func1();" />
</asp:button>


Javascript
JavaScript
function func1(event) {
     if (event.keyCode == 13) {      // RETURN key
        return true; // this will allow to execute server side code
     }
     //serverside code will never execute
     //you can put your validation alert here
     return false;
}
 
Share this answer
 
Comments
s yu 27-Mar-15 13:24pm    
Sundip: I have no problem to fire the JS. The problem I have is to call a code-behind method in the JS. Any hint? Thanks.
Sandip.Nascar 27-Mar-15 13:35pm    
validating client side you can use onclientclick. check the code snippet sent above.

Calling server side code from client side, there are several ways-
-Use $(ajax)
-use __doPost Back
But still you don't need to call server side forcefully per your needs in the above e.g.

Thanks
s yu 27-Mar-15 13:40pm    
Thanks. I will search how to do it.
s yu 27-Mar-15 13:46pm    
Wonderful. It works per your feedback by using a similar method: document.getElementById("btnSubmit").click();
Thanks.
Basically, you want to find out how to call code behind function from JavaScript, check this out: Different ways for calling a code behind function from Javascript[^]
 
Share this answer
 
Comments
s yu 27-Mar-15 13:00pm    
Peter: Thanks for your response. I reviewed the post at http://taraksworld.blogspot.sg/2012/05/different-ways-for-calling-code-behind.html. Then I dragged and dropped asp:ScriptManager control onto my page. But under the control, it shows: Element 'ScriptManager' is not known element. I know I might miss something. For this problem, I checked with http://geekswithblogs.net/ranganh/archive/2007/08/06/114452.aspx but that is about AJAX 2 only. Could you advise me how to solve this problem? Thanks again.

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