为什么在每个文本框上输入事件运行第一个按钮

时间:2019-03-01 23:59:02

标签: asp.net

我正在使用asp.net和c#,在他的情况下,我有两个文本框和两个imgbutton,但是都运行 当我确实要在任何文本框中输入密钥时,首先单击按钮 执行任何按钮单击取决于我在任何文本框中执行的输入

我正在使用此代码

<form id="form1" runat="server">
<asp:TextBox ID="txtCodCliente" runat="server" Height="25px" Width="89px"> 
</asp:TextBox>
<asp:ImageButton ID="imbtnConsultaCliente" src="Images/Search.png" 
runat="server" Height="16px" OnClick="ImageButton1_Click1" Width="17px" 
ImageAlign="AbsMiddle" /> 


<asp:TextBox ID="txtMaterial" onkeypress="continues()" autofocus="autofocus"   
runat="server" Width="64px"  ></asp:TextBox>

<asp:ImageButton ID="btnBuscaSku"  src="Images/Search.png" runat="server" 
Height="16px" Width="17px" OnClick="btnBuscaSku_Click"/>
</form>


protected void ImageButton1_Click1(object sender, ImageClickEventArgs e)
{
//Action
}

protected void btnBuscaSku_Click(object sender, ImageClickEventArgs e)
{
//Action
}

1 个答案:

答案 0 :(得分:0)

这是因为DefaultButton只有一个form,因此,每当您按Enter键时,都会触发默认的按钮单击事件。为了进行相关的按钮调用,请将标记包装在asp:panel控件内,并为各个面板设置DefaultButton。 您的标记应如下所示:

<form id="form1" runat="server">
    <asp:Panel runat="server" DefaultButton="imbtnConsultaCliente">
        <asp:TextBox ID="txtCodCliente" runat="server" Height="25px" Width="89px"> 
        </asp:TextBox>
        <asp:ImageButton ID="imbtnConsultaCliente" src="Images/Search.png"
            runat="server" Height="16px" OnClick="ImageButton1_Click1" Width="17px"
            ImageAlign="AbsMiddle" />

    </asp:Panel>

    <asp:Panel runat="server" DefaultButton="btnBuscaSku">
        <asp:TextBox ID="txtMaterial" onkeypress="continues()" autofocus="autofocus"
            runat="server" Width="64px"></asp:TextBox>
        <asp:ImageButton ID="btnBuscaSku" src="Images/Search.png" runat="server"
            Height="16px" Width="17px" OnClick="btnBuscaSku_Click" />
    </asp:Panel>
</form>