Friday, March 16, 2012

Make a button default to being active

My page that has three buttons. By default the first button is always the "active" one. How can I make the third button be the default active button?

By active I mean that if the user hits the enter key, the action for this button will occur. IE 6.0 also adds a slightly thicker border to the active button.

Use a Panel:

<asp:Panel ID="Panel1" DefaultButton="btnSubmit" runat="server">

... Content ...

</asp:Panel>

-- LZ --


or you can use javascript to achieve this, insert the below code inside the <head> tag of your webform

<script language=javascript>

function DefaultButton()

{

if(window.event.keyCode == 13)

document.getElementById('<%=Button3.ClientID%>').click();

}

</script>

then call this function onload event of the page

<body onkeypress="DefaultButton()">

HC


Try the following...

protected void Page_Load(object sender, EventArgs e){this.Form.DefaultButton = button1.UniqueID;}
Replace "button1" with the ID of your button.

Thanks guys. All 3 techniques worked great. I used the panel method because it was the easiest to implement with my application.

0 comments:

Post a Comment