owl

Monday, August 31, 2009

Friday, August 28, 2009

Javascript click() does not cause a Response.Redirect in asp.net

JavaScript click() does not cause a Response.Redirect in asp.net

when Page has 2 text box and button pair(ie)one for search and the other for lo gin.

The main problem that's encountered here is when the user enters the user name and password and presses the enter key the lo gin button click event is not called as the search button on top of the page has the default focus.

In HTML there is an option to set default focus and default button.
Defaultbutton as the search button(btnsearch)
and DefaultFocus as search textbox(txtsearch)

in some scenario this may not help.so we go for key press event to capture the "enter key" press
TextMode="Password" TabIndex="1" onkeypress="KeyCheck();" >



//This function will be invoked when the enter key is pressed after keying in user name followed by the password
//if the key pressed is an Enter Key the we are calling the server side button click event..this can be achieved with
//the help of JavaScript "click()" method..

//Click() method calls the button click event in the server side and after performing the operation the control returns back

//to the JavaScript key press event call..in case if we like to redirect to an other page on this click event, we will have

//to cancel the click event in order to redirect.to achieve that " window.event.return Value = false;" this method has to be called

function KeyCheck()
{
if (event.keyCode ==13)
{
document.getElementById('ctl00_ContentPlaceHolder1_btnLogin').click();
window.event.returnValue = false;
}

function KeysCheck(e)
{
var key=e.keyCode? e.keyCode : e.charCode
if (key==13)
{
alert("ascx");
document.getElementById('ctl00_ContentPlaceHolder1_BasicSearch1_btnSearch').click();
window.event.returnValue = false;
}
}
}


Following Code will check the type of browser and execute accordingly.

function KeyCheck(e)
{
var browserName="";
var ua=navigator.userAgent.toLowerCase();
if ( ua.indexOf( "firefox" ) != -1 )
{
browserName = "firefox";
}
else if ( ua.indexOf( "msie" ) != -1 )
{
browserName = "msie";
}

if(browserName=="firefox")
{
var key=e.keyCode? e.keyCode : e.charCode
if (key==13)
{
document.getElementById('ctl00_ContentPlaceHolder1_btnLogin').click();
e.preventDefault();
}
}
else if(browserName=="msie")
{
var key=e.keyCode? e.keyCode : e.charCode
if (key==13)
{
document.getElementById('ctl00_ContentPlaceHolder1_btnLogin').click();
window.event.returnValue = false;
}
}
}