locked
Send value to webform code behind using ajax RRS feed

  • Question

  • User444756084 posted

    I have a webform and am attempting to send a value to a function in my code behind on insert. It is not working. I am getting error: No overload for 'ListView3_ItemInserting' matches delegate 'EventHandler<ListViewInsertEventArgs>'. How can I get the value to the code behind?

    Jquery code:

    $('#ContentPlaceHolder1_Wizard1_ListView3_InsertButton').click(function () {
    
                    var sum = 0;
                    var sumIt = $('.getHrsVal').text();
    
                    $(sumIt).each(function () {
                        sum += +$(this).val();
                    });
    
                    alert(sumIt);
    
                    $.ajax({
                        url: "Default.aspx/ListView3_ItemInserting",
                        type: "POST",
                        data: "{'sumIT':'" + sumIt +"'}",
                        dataType: "json",
                        contentType: "application/json; charset=utf-8",
                       
                       // data: JSON.stringify(sumIt),
                        success: function (data) {
                            //success data if any 
                        },
                        error: function (msg) { //handle error here 
                        }
                    });
    });

    Code behind:

        protected void ListView3_ItemInserting(object sender, ListViewInsertEventArgs e, string sumIt)
        {
    
            NEED VALUE HERE
    
    
        }

    Sunday, September 1, 2019 9:28 PM

All replies

  • User-719153870 posted

    Hi gordon1221,

    As far as i kown, if you want to use ajax to send a parameter to the back code and get some data from that, you won't need to use a method with EvenArgs.

    A simple demo using ajax and a static method might help you understand what does that mean:

    ASPX:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script src="Scripts/jquery-3.3.1.min.js"></script>
        <script>
            $(function () {
                $('#btninsert').click(function () {
                    var sumIt = "6";
                    $.ajax({
                        type: "POST",
                        url: "AjaxInsertDemo.aspx/ItemInserting",
                        data: "{'sumIT':'" + sumIt + "'}",
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (data) {
                            alert('success');
                        },
                        error: function (msg) {
                            alert('fail');
                        }
                    });
                });
            })
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <input type="button" id="btninsert" value="insert" />
            </div>
        </form>
    </body>
    </html>

    .CS:

    [WebMethod]
            public static void ItemInserting(string sumIT)
            {
                SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=DatabaseTestPool;Integrated Security=True");
                con.Open();
                try
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = con;
                    cmd.CommandText = "Insert into Options values('" + sumIT + "','D1')";
                    cmd.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    throw (ex);
                }
            }

    For more information about using ajax and webmethod, please refer to this artical.

    Best Regard,

    Yang Shen

    Monday, September 2, 2019 5:09 AM