KyLim KyLim - 2 months ago 24
ASP.NET (C#) Question

Asp.net define enter button action

I have 2 buttons in my webpage.When i press "Enter",it auto perform "close_topic",but i want my enter button to perform "button_click".

<asp:Button ID="btnLogout" Text="Close Topic" OnClick="close_topic" runat="server" />

<asp:Button ID="ButtonTT" Text="Click" runat="server" OnClick="button_click" />

protected void button_Click(object sender, EventArgs e)
{

}

protected void close_topic(object sender, EventArgs e)
{

}


tried to use
<asp:Panel ID="Panel1" runat="server" DefaultButton="ButtonTT">
,but not working.

Answer

Did you wrap your panel around the 2 buttons? As follows:

<asp:Panel ID="Panel1" runat="server" DefaultButton="ButtonTT">
    <asp:Button ID="btnLogout" Text="Close Topic" OnClick="close_topic" runat="server" />
    <asp:Button ID="ButtonTT" Text="Click" runat="server" OnClick="button_click" />
</asp:Panel>

Edit

Based on how to set a default 'enter' on a certain button, you can do it using code also:

Me.Form.DefaultButton = Me.btn.UniqueID;

or

Me.Page.Form.DefaultButton = = Me.btn.UniqueID;

Replacing the Me.Page with whatever your page name is.

Comments