Thursday, December 30, 2004

How to make a default button for ASP.Net Page

Dear All.. This is really a big problem. if you have couple of buttons in your form ,, and couple of input boxes or one,, how to make one button default. Lets make a scenrio, like if you have a form taking search input in a textbox, and you have one button for search, one button for sign up. You want the search button to behave like a default button,, but how.... after googling.. i found 2 methods, one is very easy just paste the code in your page load event Page.RegisterHiddenField("__EVENTTARGET", "btnSearch") This makes your search button a default button.. This is cool. But i dont know somehow it doesnt solve one of friend problem, i mean he was using ,, couple of cms templates, and all those templates have couple of button, so on his case it doesnt work ,, so there is one more solution for that,, and that is with javascript Public Shared Sub SetDefaultButton(ByVal page As System.Web.UI.Page, ByVal button As System.Web.UI.WebControls.Button) SetDefaultButton(page, button.ClientID) End Sub Public Shared Sub SetDefaultButton(ByVal page As System.Web.UI.Page, ByVal clientID As String) page.RegisterClientScriptBlock("MyFocus", " var defaultButton=null;function document.onkeypress(){if (defaultButton==null window.event.keyCode!=13) return; defaultButton.click();window.event.returnValue = false;return false;}") page.RegisterStartupScript("setDefault", "defaultButton = document.all['" + clientID + "'];") End Sub ' Blog doesnt allow to write script tag so thats why i write ssss within the code ,, change it to script. And paste the following code in your page load event SetDefaultButton(Me.Page, btnSearch.ClientID) This works everywhere, ..................................., Wajahat Abbas


  • Hi, Need to know about telecommunications story or update, so visit my website **Telecom** site/blog. It pretty much covers Telecommunications Updates related stuff.

    Have a nice day.

    By Anonymous Anonymous, at 10/21/2005 07:04:00 AM  

Post a Comment

<< Home