开发者

Calling a function / database update using Ajax via Jquery

开发者 https://www.devze.com 2023-03-28 17:17 出处:网络
Im creating a simple \"Was this useful?\" form withYes and No objects- Using ASP.net webforms. I need the submission to be done via ajax using jquery, to prevent a user from 开发者_如何学编程voting m

Im creating a simple "Was this useful?" form with Yes and No objects- Using ASP.net webforms.

I need the submission to be done via ajax using jquery, to prevent a user from 开发者_如何学编程voting multiple times on the same page.. currently i have two methods Like_Click and Dislike_click in the C# code behind the page in question.

Can anyone give me some pointers on or a link to any suitable walkthroughs for simple ajax via jquery (I'm new to ajax!)

Ive looked at using the [WebMethod] identifier on each of the methods but do not really understand this method fully.

thanks


You are probably looking for jQuery's post function. Check out the examples. You'll want to do something along the lines of:

$('.myForm').submit(function(){ //define a handler for the submit event of the form
    $.post($(this).attr('action'), {useful: true}); //send data via ajax
    return false; //prevents the form from submitting via a normal web request
});


You can try something like below

 <script type="text/javascript">
    $(function () {
        $('#btnSubmit').click(function () {
            var like = $('#Like').val();
            var dislike = $('#Dislike').val();
            if (name != '' && email != '') {
                $.ajax
                    ({
                        type: 'POST',
                        url: 'Home.aspx/UpdateDB',     //UpdateDB is declared as WebMethod 
                        async: false,
                        data: "{'like':'" + like + "','dislike':'" + dislike + "'}",
                        contentType: 'application/json; charset =utf-8',
                        success: function (data) {
                            var obj = data.d;
                            if (obj == 'true') {
                                $('#Like').val('');
                                $('#Dislike').val('');
                                alert("Data Saved Successfully");
                            }
                        },
                        error: function (result) {
                            alert("Error Occured, Try Again");
                        }
                    });
            }
        })
    });
</script> 

Webmethod is shown below

[WebMethod] 
public static string UpdateDB(string like, string dislike) 
{ 
    //Add your stuff
}

take a look more details here Call WebMethod from jquery in ASP.NET

0

精彩评论

暂无评论...
验证码 换一张
取 消