Monday, July 7, 2008

inside ajax :- Textbox on Key up hit Servercode

js:-

function SetKeyUp(e)
{
document.onkeyup = AssignKeyPress.interceptKeypress;
}


AssignKeyPress = {
interval : 200,
lastKeypress : null,
interceptKeypress : function(e) {
var key;
if (window.event) {
key = window.event.keyCode;
} else if (e) {
key = e.which;
}
var text = String.fromCharCode(key);
var format = /^([A-Za-z0-9\s])+$/;
//alert(AssignKeyPress.interval);
if(text.match(format))
{
AssignKeyPress.lastKeypress = new Date().getTime();
var that = AssignKeyPress;
setTimeout(function() {
var currentTime = new Date().getTime();
if(currentTime - that.lastKeypress > that.interval) {
that.sendRequest();
}
}, that.interval + 100);
}
else if(key == 8 key == 46)
{
AssignKeyPress.lastKeypress = new Date().getTime();
var that = AssignKeyPress;
setTimeout(function() {
var currentTime = new Date().getTime();
if(currentTime - that.lastKeypress > that.interval) {
that.sendRequest();
}
}, that.interval + 100);
}
},
sendRequest : function() {
__doPostBack('ctl00$SiteContentPlaceHolder$searchTextBox','SearchTextBoxKeyPress');
}

C#:-


if (this.IsPostBack)
{
string eventArgument = (this.Request["__EVENTARGUMENT"] == null) ? string.Empty : this.Request["__EVENTARGUMENT"];
if (eventArgument.Trim() == "SearchTextBoxKeyPress")
{
_setFocus = true;
SearchTextBoxKeyPress(selectTabHiddenField.Value);
}
else
{
_setFocus = false;
}
}

if (httpBrowserCapabilities.Browser == "Firefox")
{
searchTextBox.Attributes.Add("OnKeyUp", "SetKeyUp(this);");
}
else
{
searchTextBox.Attributes.Add("OnKeyUp", "AssignKeyPress.interceptKeypress(this);");
}

No comments: