0

有什么方法可以将文本框文本保存到会话变量而不必回发文本更改?

4

1 回答 1

2

您需要分 4 个阶段执行此操作。

1)添加一个onkeydown="textChanged(this.ID);"到你的文本框

2)使用一些Javascript来捕获文本

3) 触发对 Web 方法的 Ajax 调用

4)使用web方法存储session变量。

所以是这样的:

在您的文本框中,进行设置,Autopostback=false这样您就不会在打字时回帖。

为自己创建一个小的 Javascript 函数,当用户键入时会触发该函数。

此函数将首先清除附加到文本框的任何计时器。然后它将创建另一个在 1 秒后触发另一个函数的函数。这将确保您不会过于频繁地尝试触发 end 函数。

 function textChanged(id) {
        var txtBox = document.getElementById(id);
        clearTimeout(txtBox.timer);
        txtBox.timer = setTimeout('SaveVariable()', 1000);
    };

1 秒后,您的方法将调用如下内容:

function SaveVariable(){
       // Do an ajax call in here and send your textbox value to a web method
        var t = // GetYourTextboxTextHere
        if (t != "") {
            $.ajax({
                type: "POST",
                url: "yourPage.aspx/SaveMyTextVar",
                data: "{'textToSave ': '" + escape(t) + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    if (!msg.d) {
                        // it didn't save
                    } else {
                        // it saved just fine and dandy
                    };
                }
            });
        };
}

最后,在你的代码后面用一个小的 web 方法来捕获文本

<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> <Services.WebMethod()> _
Public Shared Function SaveMyTextVar(ByVal textToSave As String) As Boolean
         '' Save your variable to session here.
         '' return true or false
         '' you can capture the returning result in the AJAX that calls this method
End Function
于 2013-05-14T14:34:47.983 回答