asp.net - update session variable on textbox_change without postback -
is there way save textbox text session variable without having postback on text change?
you need in 4 stages.
1) add onkeydown="textchanged(this.id);"
textbox
2) use javascript capture text
3) fire ajax call web method
4) use web method store session variable.
so this:
on textbox, set autopostback=false
don't post on typing.
create small javascript function fired when user types.
this function first clear timer attached textbox. create 1 fire function after 1 second. ensure you're not trying fire end function often.
function textchanged(id) { var txtbox = document.getelementbyid(id); cleartimeout(txtbox.timer); txtbox.timer = settimeout('savevariable()', 1000); };
after 1 second, method call this:
function savevariable(){ // ajax call in here , send textbox value 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) { // didn't save } else { // saved fine , dandy }; } }); }; }
finally, small web method on code behind capture text
<scriptmethod(responseformat:=responseformat.json)> <services.webmethod()> _ public shared function savemytextvar(byval texttosave string) boolean '' save variable session here. '' return true or false '' can capture returning result in ajax calls method end function
Comments
Post a Comment