Set Default button in Asp.Net

Monday, October 5, 2009

Sometime it happen, that you enter text in textbox and you press enter and some other submit button get pressed, so here is the solution below, using below code you can set your default button.

HTML code

enter your table inside div tag and put onkeypress event in div tag

<div id="pnlSearch" onkeypress="javascript:return WebForm_FireDefaultButton(event, 'btnSearch')">

<table width="50%" cellpadding="0" cellspacing="0">



Search :



<input name="txtSerach" type="text" id="txtSerach" />


<td colspan="2">

<input type="submit" name="btnSearch" value="Search" id="btnSearch" />





Put this fuction inside script tag in your web page

function WebForm_FireDefaultButton(event, target) {

if (event.keyCode == 13) {

var src = event.srcElement ||;

if (!src || (src.tagName.toLowerCase() != "textarea")) {

var defaultButton;

if (__nonMSDOMBrowser) {

defaultButton = document.getElementById(target);


else {

defaultButton = document.all[target];


if (defaultButton && typeof( != "undefined") {;

event.cancelBubble = true;

if (event.stopPropagation) event.stopPropagation();

return false;




return true;


Hope this post will help you,
if yes please put comment below of this page,
Rajesh Singh,
Asp.Net Developer
Indianic Infotech Ltd (India)

No comments :