如何在asp.net转发器侧面的按钮单击上检索TextBox值

时间:2017-04-21 00:22:26

标签: c# sql asp.net

 <div class="row">
        <asp:Repeater ID="rptrProducts" runat="server" OnItemDataBound="rptrProducts_ItemDataBound">
            <ItemTemplate>
                <div class="col-sm-4 col-md-4 col-lg-3 ">
                    <div class="thumbnail" style="background-color: white; width: 100%;">
                        <img src="images/<%# Eval("image")%>" class=" img" style="width: 300px; height: 300px;" />
                        <div class="caption">
                            <asp:TextBox CssClass="hidden" ID="pid" runat="server" Text='<%#Eval("productsID")%>'></asp:TextBox>
                            <div class="ProductName" style="font-family:monospace; font-weight:bold;">Title: <%#Eval("name")%></div>
                            <div class="ProductPrice" style="font-family:monospace; font-weight:bold;">Price: £<%# Eval("price", "{0:F2}")%></div>
                            <asp:Button ID="addItem" CommandName="addItem" CommandArgument='<%#Eval("productsID")%>' CssClass="btn btn-success btn-sm" runat="server" Visible="false" Text="Add" OnClick="addItem_Click" />
                            <asp:TextBox ID="quan" runat="server" Text=""></asp:TextBox>
                        </div>
                    </div>
                </div>
            </ItemTemplate>
        </asp:Repeater>
    </div>
</div>

我有这个代码显示我的产品。我现在正试图找出如何挑出正确的文本框,例如知道用户输入文本的文本框。如果这真的很模糊,我道歉,如有必要,请询问更多细节。我还提供了一张图片,使其更容易理解。谢谢。

enter image description here

1 个答案:

答案 0 :(得分:0)

您需要做的事情很少:

首先,将其包含在您的代码中:

void rptrProducts_ItemCommand(Object Sender, RepeaterCommandEventArgs e) {        
      var quantityTextBox = (TextBox)e.Item.FindControl("quan");
      int quantity = 0;
      if (!Int32.TryParse(quantityTextBox.Text, out quantity))
                //what do you want to do if the value is not an int? like if someone passes a text? up to you

      //from here on, the int variable quantity has the value collected from the textbox
}

可以找到参考here

然后,在你的html中:

<asp:Repeater ID="rptrProducts" runat="server" OnItemDataBound="rptrProducts_ItemDataBound" **OnItemCommand="rptrProducts_ItemCommand"**>