0

I don't know what is the problem..

        <script type="text/javascript">
        $(document).ready(function () {
            $("#SendCommentBtn").click(function () {
                $.ajax({

                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: "Pages/PageOne.aspx/SendComment",
                    data: "{'name':'" + $('#nameTxt').val() + "','comment':'" + $('#commentTxt').val() + "'}",
                    async: false,
                    success: function (response) {
                        $("#nameTxt").val("");
                        $("#commentTxt").val("");
                        alert("OK");
                    },
                    error: function () {
                        alert("ERROR");
                    }
                });
            });
        });
</script>

In my code behind

    [WebMethod]
    public static void SendComment(string name, string comment)
    {
        using (SqlConnection cnn = new SqlConnection("CONNECTIONSTRING"))
        {
            cnn.Open();

            int CodArt = Convert.ToInt32(HttpContext.Current.Request.QueryString["CodArt"].ToString());

            string query = @"INSERT INTO Comment VALUES(@Param1,@Param2,@Param3)";
            SqlCommand cmd = new SqlCommand(query, cnn);

            cmd.Parameters.AddWithValue("@Param1", CodArt);
            cmd.Parameters.AddWithValue("@Param2", comment);
            cmd.Parameters.AddWithValue("@Param3", name);

            cmd.ExecuteNonQuery();

        }
    }

Where is my problem? I can't find it. I'm working with a master page.. is the same that a web form or not? this code is in the master page.. if put a breakpoint in my code behind (SendComment method), the web doesn't stop. it's like that never arrive on it.

4
  • Are you sure about the URL? stackoverflow.com/questions/348689/… Commented Jan 6, 2014 at 3:46
  • @Dilish hi, mm the problem is that i used a master page and the method SendComment is in code behind of the master page.. how i do to call this method from webforms? Commented Jan 6, 2014 at 4:08
  • can't you move the SendComment webmethod to the page 'PageOne.aspx' itself? also, you can use $('#<%= controID.ClientID %>') where controID is the id of the element, to get a control through jquery, if you're using a masterpage. Commented Jan 6, 2014 at 6:14
  • I don't think it will work. See here stackoverflow.com/questions/8577539/… Commented Jan 7, 2014 at 1:11

2 Answers 2

0

Try this may this help you solve your problem

var name = $.trim($('#<%=nameTxt.ClientID %>').val());
var comment= $.trim($('#<%=nameTxt.ClientID %>').val());
$.ajax({

                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "Pages/PageOne.aspx/SendComment",
                data: "{'name':'" + name + "','comment':'" + comment + "'}",
                async: false,
                success: function (response) {
                    $("#nameTxt").val("");
                    $("#commentTxt").val("");
                    alert("OK");
                },
                error: function () {
                    alert("ERROR");
                }
            });
Sign up to request clarification or add additional context in comments.

Comments

0

i have attached the sample code for your reference..

  <script type="text/javascript">
    $(document).ready(function () {
        $("#SendCommentBtn").click(function (e) {
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "/Pages/PageOne.aspx/SendComment",
                data: "{'name':'" + $('#<%= nameTxt.ClientID %>').val() + "','comment':'" + $('#<%= commentTxt.ClientID %>').val() + "'}",
                //async: false,
                success: function (response) {
                    $('#<%= nameTxt.ClientID %>').val("");
                    $('#<%= commentTxt.ClientID %>').val("");
                    alert("OK");
                },
                error: function () {
                    alert("ERROR");
                }
            });
            e.preventDefault();
        });
    });
</script>

check it let me know the feedback Example Source code

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.