Unwanted calling onClick event on button after pressed enter
net web page I have textBox and button.
I added onClick for button.
Problem is that if I click enter in textBox it also call onClick event for button. I understand why.
My code is here:
TextBox is in view:
<asp:View ID="BlView" runat="server">
<p class="tabHead"> BLACK LIGHT LABEL DATA </p>
<table class="tab">
<tr>
<td>
<asp:Label ID="Label1" runat="server" class="label" Text="BARCODE" />
</td>
<td>
<asp:TextBox ID="bl_tb_barcode" runat="server" BorderWidth="1px"
class="textBox" Width="700"/>
</td>
</tr>
</table>
</asp:View>
Button code i开发者_运维百科s here:
<div class="divPrinter">
<asp:DropDownList runat="server" class="dropDownList" Width="250" Height="35"
ID="cbPrinters" style="margin-top:80px"/>
<asp:Button ID="btnPrint" runat="server" class="btnPrint" style="margin-top:80px"
Width="120" Height="35" Text="Print" onclick="BtnPrintClick" />
</div>
I take it you don't want the Enter
key to have any effect? If so, add this tag to your textboxes:
onKeyDown = "return (event.keyCode != 13)"
so it would be like this:
<asp:TextBox ID="bl_tb_barcode" runat="server" BorderWidth="1px"
class="textBox" Width="700" onKeyDown="return (event.keyCode != 13)"/>
As a side note, onKeyDown
is a Javascript event, so it won't execute on the server. The code will return false
so a postback will not occur.
精彩评论