locked
Insert and update query on HTML Button RRS feed

  • Question

  • User-1578974752 posted

    How can i insert and update the sql table (using sql query )on html button click event in vb.net. Thanks

    Wednesday, February 20, 2019 4:00 AM

Answers

All replies

  • User409696431 posted

    You can turn an HTML button into a server control by adding runat="server" to it.  You can handle its click in code-behind by assigning an onserverclick event to it.

    E.g.:

    <button id="button1" runat="server" onserverclick="button1_click" >Submit</button>

    and in code behind:

    protected void button1_click(object sender, EventArgs e)
     {
       //write your code to update your database here
     }

    Wednesday, February 20, 2019 7:38 AM
  • User-1578974752 posted

    Using html button for avoiding postback. Hence I want exactly the html button. Otherwise jQuery cloned panel are disappearing on button click. Appreciate the help

    Wednesday, February 20, 2019 8:24 AM
  • User61956409 posted

    Hi shsu,

    shsu

    Using html button for avoiding postback. Hence I want exactly the html button.

    You can use jQuery AJAX to make request(s) to a WebMethod/backend service to perform insert/update operation on your database. You can refer to the following code snippet.

    var obj = { "name": "fei han", "age": 28 };
    $.ajax({
        type: "POST",
        url: "Test.aspx/OperateDB",
        data: JSON.stringify(obj),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (r) {
            alert(r.d);
        }
    });
    <System.Web.Services.WebMethod()>
    Public Shared Function OperateDB(name As String, age As Integer) As String
        'in your code logic, you can use ado.net technology
        'connect database
        'insert/update record
        Return "success"
    End Function

    With Regards,

    Fei Han

    Wednesday, February 20, 2019 8:45 AM
  • User-1578974752 posted

     I am completely new to jquery,ajax.. So can please inform the script for button click call.I placed button in the function that you shown as  above. Is this the correct way? Thanks

    <script src="../Scripts/jquery-3.3.1.js"></script>

    <script>

    $(function () {

    $("#Button1").click(function () {

    var obj = { "name": "fei han", "age": 28 };

    $.ajax({ type: "POST", url: "Test.aspx/OperateDB", data: JSON.stringify(obj),

    contentType: "application/json; charset=utf-8",

    dataType: "json",

    success: function (r) {

    alert(r.d);

    }

    });

    </script>

    </head>

    <body>

    <form id="form1" runat="server">

    <input id="Button1" type="button" value="button" />

    <System.Web.Services.WebMethod()>

    Wednesday, February 20, 2019 10:02 AM
  • User61956409 posted

    Hi shsu,

    I am completely new to jquery,ajax.. So can please inform the script for button click call.I placed button in the function that you shown as  above. Is this the correct way?

    It is ok to bind an event handler to the "click" of button and make jQuery AJAX request in event handler. You can check documentation of jQuery.ajax() to know more details, or refer to the following article that explained with an example, how to send (pass) parameters to Web Method in jQuery AJAX POST call.

    https://www.aspsnippets.com/Articles/jQuery-AJAX-call-with-parameters-example-Send-parameters-to-WebMethod-in-jQuery-AJAX-POST-call.aspx 

    With Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 21, 2019 1:32 AM