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=开发者_StackOverflow社区"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>
Set the defaultbutton
attribute on the containing panel
:
<asp:panel defaultbutton=“btnSave”>
Taken from this blogpost.
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>
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 + "')");
we can set the "defaultbutton" property in the form tag
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);");
精彩评论