我有一个文本框。在其输入上,我想像粘滞便笺一样保存其数据,我的意思是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);
}
}
如何让文本框数据自动保存?
最佳答案
您可以尝试以下方法:
$('#<%=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);
}
});
});
关于javascript - 如何在asp.net中调用文本框更改事件的Web方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28023814/