Search content within the blog

Wednesday, March 25, 2009

Triggering button event when enter is clicked in textboxt

In javascripts add the following script.....

function fnTrapKD1(btn, event){
if (document.all){
if (event.keyCode == 13){
event.returnValue=false;
event.cancel = true;
btn.click();
}
}
else if (document.getElementById){
if (event.which == 13){
event.returnValue=false;
event.cancel = true;
btn.click();
}
}
else if(document.layers){
if(event.which == 13){
event.returnValue=false;
event.cancel = true;
btn.click();
}
}
}

In page load add the following.....

protected void Page_Load(object sender, EventArgs e)
{
// here txtSrch is the textbox id and btnSubmit is the button id
SetDefaultButton(this.txtSrch, this.btnSubmit);
}

private void SetDefaultButton(System.Web.UI.WebControls.TextBox txt, System.Web.UI.WebControls.Button defaultButton)
{

txt.Attributes.Add("onkeydown", "fnTrapKD1(" + defaultButton.ClientID + ",event)");
}

No comments:

Post a Comment