带有文本框和按钮的Repeater中的DefaultButton

本文关键字:Repeater 中的 DefaultButton 按钮 文本 | 更新日期: 2023-09-27 18:19:25

我有一个中继器,它代表一个购物篮。这个购物篮有一个"更新数量"和一个"删除项目"按钮。

当你编辑数量并点击进入时,我需要它使用更新数量按钮(不像现在它使用删除项目按钮)。

在代码中,我试图通过添加一个"QuantityPanel"与DefaultButton来修复这个问题,但这并不能解决我的问题!

任何想法?

我代码:

<asp:Repeater ID="ProductBasketRepeater" runat="server" 
                onitemcommand="ProductBasketRepeater_ItemCommand" 
                onitemdatabound="ProductBasketRepeater_ItemDataBound1">
        <ItemTemplate>
            <tr class="BasketEntryItem">
                <td class="ImageCol">
                    <asp:Image ID="ProductImageBox" runat="server" Width="50" />
                </td>
                <td class="NameCol">
                    <asp:HyperLink ID="ProductNameHyperlink" runat="server"></asp:HyperLink>             
                </td>
                <td class="PriceCol">
                    <asp:Label ID="PriceLabel" runat="server"></asp:Label>
                </td>
                <td class="QuanCol">
                    <asp:Panel ID="QuantityPanel" runat="server" DefaultButton="UpdateQuantityBtn">
                        <asp:TextBox ID="QuantityBox" runat="server" Width="30px"></asp:TextBox>
                        <asp:LinkButton ID="UpdateQuantityBtn" runat="server" Text="Opdater" CommandName="UpdateQuantity"></asp:LinkButton>
                    </asp:Panel>
                </td>
                <td class="TotalCol">
                    <asp:Label ID="TotalPriceLabel" runat="server"></asp:Label><br />
                    <asp:Button ID="DeleteProductBtn" runat="server" Text="Slet" CommandName="Delete"  />
                </td>
            </tr>
        </ItemTemplate>
        <SeparatorTemplate>
        </SeparatorTemplate>
    </asp:Repeater>

带有文本框和按钮的Repeater中的DefaultButton

在无法使用jQuery的情况下,您可以使用简单的JavaScript设置为任何文本框设置默认按钮:

JS代码:

function clickButton(e, buttonid){
      var evt = e ? e : window.event;
      var bt = document.getElementById(buttonid);
      if (bt){
          if (evt.keyCode == 13){
                bt.click();
                return false;
          }
      }
}

ASPX页面或任何简单的"输入"文本框:

<input name="TextBox1" type="text" id="TextBox1" onkeypress="return clickButton(event,'Button1')"  />

c#开头代码:

TextBox1.Attributes.Add("onkeypress", "return clickButton(event,'" + Button1.ClientID + "')");

在中继器中应用此代码需要帮助吗?

使用jQuery,这段代码将在文本框中按enter键时发出一个程序性点击:

$('input[id$=QuantityBox]').keypress(function(e) {
    if (e.keyCode == 13) {
        $(this).next('input[id$=UpdateQuantityBtn]').click();
    }
});

注意:我看到有点晚了,你没有特别要求jquery,但在你做,这应该做的伎俩。