Click here to Skip to main content
16,020,622 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I Want call web method from json,but web method does not call from json,when using update panel in form.I have to use update panel.

Here is my code of client side

HTML
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript" src="http://cdn.jsdelivr.net/json2/0.1/json2.js"></script>

HTML
<script type="text/javascript">
        $(function () {
            $("[id*=btnSave]").bind("click", function () {
                
                var event1 = {};
                event1.EventDate = $("[id*=txtEventDt]").val();
                event1.Description = $("[id*=txtEventDesc]").val();
                $.ajax({
                    type: "POST",
                    url: "Default3.aspx/SaveEvent",
                    data: '{event1: ' + JSON.stringify(event1) + '}',
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (response) {
                        $find("mpe").hide();
                        clear();
                        __doPostBack('hdText', 'JavaScript');
                    }
                });
                return false;
            });
        });
        //==== Method to clear input fields
        function clear() {
            $("#txtEventDt").val("");
            $("#txtEventDesc").val("");                      
        }
    </script>



Server Side Code

C#
[WebMethod]
   [ScriptMethod]
   public static void SaveEvent(Event event1)
   {
       try
       {
           string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
           using (SqlConnection con = new SqlConnection(constr))
           {
               using (SqlCommand cmd = new SqlCommand("INSERT INTO Event VALUES(@EventDate, @Description)"))
               {
                   cmd.CommandType = CommandType.Text;
                   Default3 obj = new Default3();
                   string eventdate=obj.changeYYYYMMDD(event1.EventDate.ToString());
                   cmd.Parameters.AddWithValue("@EventDate", eventdate);
                   cmd.Parameters.AddWithValue("@Description", event1.Description);
                   cmd.Connection = con;
                   con.Open();
                   cmd.ExecuteNonQuery();
                   con.Close();

               }
           }
       }
       catch (Exception ex)
       {
       }
   }



Now the postback part

C#
protected override void Render(System.Web.UI.HtmlTextWriter writer)
    {

        ClientScript.RegisterForEventValidation("hdText", "JavaScript");

        base.Render(writer);

    }
    protected void hdText_Click(object sender, EventArgs e)
    {
    }


What I have tried:

I have tried by using postback from client side.

Any help will be very appreciated.
Please.... have a reply
Posted
Updated 22-Nov-16 20:35pm
v2
Comments
Did you debug? Does it hit the method with debugger?
priya231 22-Nov-16 23:51pm    
Actually the button click event is not firing.I have tried it by changing id of button as inspected in firefox.



Instead of $("[id*=btnSave]").bind("click", function () {

I am Using $("#MainContent_btnSave").click(function () {

but does not succed.Any help from your side will be highly appreciated
Check my answer.
F-ES Sitecore 23-Nov-16 5:01am    
You haven't posted enough to give any specific help, where is the save button and where does the updatepanel come in? You might also be using master\content pages which also changes everything.

Javascript runs on the clink so view the source of your page and ensure there is an element that matches the selector for your click event as .net will change the ids of your elements, and ensure you're not trying to attach the click event before the button appears in the markup, ie ensure your script tag is at the bottom of the page.
F-ES Sitecore 23-Nov-16 6:32am    
You should update the original question with the code and ensure it is properly formatted. Can't really tell for sure but if the issue is that the click doesn't work after you do the update then look at this.

1 solution

Try like below...
JavaScript
$("[id*=btnSave]").click(function () {
    // Your logic here. 
});
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900