Click here to Skip to main content
16,011,988 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
Hi,
I have an asp .net submit button. On clicking that button the values in the text box and drop down list are inserted to the sql database, it works fine. But the problem is that on clicking submit button the page refreshes and the values are inserted. To avoid page refresh I have used onclientclick="return false", at this time the page do not refreshes but the value insertion could not been done.
Posted

Practically it has many ways, but let me show you 1 way to do so. Here i am going to use Jquery.
First of all create JS function or Jquery function, which required to call on "OnClientClick". Than you have to call the Code Behind method for doing the database operations(If you have such method else create such).
Remember that the method should be
C#
"[WebMethod]"
.
Let consider function name "JqueryFunc" for better understanding.

JavaScript
<script type="text/javascript">
function JqueryFunc(){
    // Validate your controls if required.
    // Now you need to call the code behind method such like below & pass required parameters
    // consider parameters as other controls data which you required to store in DB.
    PageMethods.SetAllValues(parameter1,parameter2);
}
</script>


Now consider that you have created method in code behind which takes parameters and do required operation in database.
C#
 [WebMethod]
//Consider parameters as per your requirement
 public static void SetAllValues(int parameter1, int parameter2)
{
  //perform all required database operations here.
}


And now just call the JS function from the button.
C#
// OnClientCode will have the JS function and return false.
<asp:button runat="server" id="btnFunc" text="Func" onclientclick="JqueryFunc(); return false;"></asp:button>


Hope this will help you.
 
Share this answer
 
v3
 
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