Arindam Rudra
Arindam Rudra

Reputation: 624

Default button setting

I have an .aspx page that contains two buttons one is "btnCancel" and another one is "btnSave".

<asp:Button ID="btnCancel" runat="server" Text="Cancel" CssClass="Button" OnClick="btnCancel_Click" />
<asp:Button ID="btnSave" runat="server" Text="Save" CssClass="Button" OnClick="btnSave_Click"
OnClientClick="tinyMCE.triggerSave(false,true);" ValidationGroup="grp"/>

Now the problem is after filling up some of the textboxes which are present in that page, if I press the "Enter button" the "btnCancel_Click" event is firing instead of "btnSave_Click".

Can any one please suggest me how to set the "btnSave_Click" button as the default one. So that if any one press the "enter button" it will fire the "btnSave_Click" event.

Any help please.

Updated Question:

    <asp:Panel DefaultButton="btnSave" runat="server" ID="pnlTest">
    <asp:Button ID="btnCancel" runat="server" Text="Cancel" CssClass="Button" OnClick="btnCancel_Click" />
    <asp:Button ID="btnSave" runat="server" Text="Save" CssClass="Button" OnClick="btnSave_Click"
                                            OnClientClick="tinyMCE.triggerSave(false,true);" ValidationGroup="grp" />
   </asp:Panel>

Upvotes: 2

Views: 2432

Answers (5)

Viral Sarvaiya
Viral Sarvaiya

Reputation: 781

set this javascript to header

function checkKey(b1, e) {
if (e.keyCode == 13) {
    //alert(e.keyCode + 'validation.js');
    document.getElementById(b1).click();
    return false;
} 

}

and set attributes to control

txtboxname.Attributes.Add("onkeypress", "javascript:return checkKey('" + buttonName.ClientID + "',event);");

Upvotes: 0

Muhammad Akhtar
Muhammad Akhtar

Reputation: 52241

you can simply use javascript

<script language="javascript" type="text/javascript" > 
function button_click(objTextBox,objBtnID)
{
    if(window.event.keyCode == 13)
    {
        document.getElementById(objBtnID).focus();
        document.getElementById(objBtnID).click();
    }
}
</script>

add this in your page load event

this.TextBox1.Attributes.Add("onkeypress", "button_click(this,'" + this.btnSave.ClientID + "')");

Upvotes: 1

Ahmed Magdy
Ahmed Magdy

Reputation: 6030

put your form controls inside asp.net Panel and set DefaultButton to your button id Example:

<asp:Panel DefaultButton="btnSave">    
<asp:Button ID="btnCancel" runat="server" Text="Cancel" CssClass="Button" OnClick="btnCancel_Click" />
<asp:Button ID="btnSave" runat="server" Text="Save" CssClass="Button" OnClick="btnSave_Click"OnClientClick="tinyMCE.triggerSave(false,true);" ValidationGroup="grp"/>
</asp:Panel>

Upvotes: 2

Mahesh KP
Mahesh KP

Reputation: 6446

we can set the "defaultbutton" property in the form tag

Upvotes: 0

Mizipzor
Mizipzor

Reputation: 52321

Set the defaultbutton attribute on the containing panel:

<asp:panel defaultbutton=“btnSave”>

Taken from this blogpost.

Upvotes: 6

Related Questions