我有一个文本框。在它的输入我想保存其数据,如粘滞注意我的意思是在asp.net中自动保存文本框。
我的前端代码是:
<textarea id="txtClientArea" runat="server" class="scfMultipleLineTextBox" cols="20" rows="4" onchange="onTextChange" >
我的javascript Web方法是:
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);
}
});
}
代码隐藏方法是:
[System.Web.Services.WebMethod]
public static void SaveData(string data)
{
User currentUser = Sitecore.Context.User;
if (currentUser != null)
{
currentUser.Profile.SetCustomProperty("StickyNote", data);
}
}
如何自动保存文本框数据?
答案 0 :(得分:1)
您可以尝试以下方法:
$('#<%=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);
}
});
});