0

I have a text box. On its input I want to save its data like sticky Note I mean Auto Save textbox in asp.net.

My front end code is :

<textarea id="txtClientArea" runat="server" class="scfMultipleLineTextBox" cols="20" rows="4" onchange="onTextChange" >

My javascript Web method is:

function onTextChange(data) {
        debugger;
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "StickyNote.aspx/SaveData",
            data: JSON.stringify({ "data": data }),
            datatype: "json",
            async: false,
            success: function (response) {
                alert("C# method calling : " + response.d);
            },
            error: function (err) {
                alert(err.responseText);
            }
        });
    }

Code behind method is :

[System.Web.Services.WebMethod]
public static void SaveData(string data)
{
    User currentUser = Sitecore.Context.User;
    if (currentUser != null)
    {
        currentUser.Profile.SetCustomProperty("StickyNote", data);
    }
}

How do I make the textbox data auto save?

4
  • This exemple above works? Whant's the issue? Commented Jan 19, 2015 at 11:41
  • it is not working when i put some values in textarea javascript method is not calling Commented Jan 19, 2015 at 11:43
  • 1
    Refer the following link stackoverflow.com/questions/3889004/… Commented Jan 19, 2015 at 11:43
  • @neenumariya mostly i want something like this but it send request after each 5 seconds but i want to send this request after any change not after each 5 seconds Commented Jan 19, 2015 at 11:58

1 Answer 1

1

You could try the following:

$('#<%=txtClientArea.ClientID%>').on('change',function () {
    var data = encodeURIComponent($(this).text());                
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "StickyNote.aspx/SaveData",
        data: JSON.stringify({ "data": data }),
        datatype: "json",
        async: false,
        success: function (response) {
            alert("C# method calling : " + response.d);
        },
        error: function (err) {
            alert(err.responseText);
        }
    });
});
Sign up to request clarification or add additional context in comments.

4 Comments

it is throwing an error : TypeError: $(...).live is not a function error source line: $('textarea').live('change', function () {
Which jQuery version are you using? You should note that as of jQuery 1.7, the .live() method is deprecated. The alternative is the .on() to attach event handlers.
i'm using : //ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
I've updated my answer to use the correct method .on(), give that a try, should work.

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.