如何确定哪个文本框连接到Repeater Control中的哪个记录?

时间:2014-03-23 00:50:04

标签: c# asp.net repeater

我想出了如何从转发器控件中获取值,但是如何获取包含文本框的行的记录ID。 我的转发器被数据绑定到包含剩余缺货订单的采购订单的数据集。这是我的转发器的标记

<asp:Repeater ID="Repeater1" runat="server" EnableViewState="false">
            <HeaderTemplate>
                <table style="width: 100%; text-align: center">
                    <tr>
                        <td style="width: 124px">Purchase Order :</td>
                        <td style="width: 124px"><asp:Label ID="lblPONumber" runat="server" Text="Label"></asp:Label></td>
                    </tr>
                    <tr>
                        <td>
                            Product
                        </td>
                        <td>
                            BackOrder
                        </td>
                        <td>
                            Received
                        </td>
                    </tr>
                </table>
            </HeaderTemplate>
            <ItemTemplate>
                <table style="width: 100%; text-align: center">
                    <tr>
                        <td style="width: 124px"><%# Eval("ProductName")%></td>
                        <td style="width: 124px"><%# Eval("OnBackOrderAmount") %></td>
                        <td style="width: 124px"><asp:TextBox ID="txtQty" runat="server"></asp:TextBox></td>
                    </tr>
                </table>
            </ItemTemplate>
            <SeparatorTemplate>
                <hr />
            </SeparatorTemplate>
        </asp:Repeater>

我在转发器外面有一个按钮,它从文本框中获取值,所以我可以更新数据库,那个代码是

foreach (RepeaterItem rptItem in Repeater1.Items)
        {
            TextBox txtQty = (TextBox)rptItem.FindControl("txtQty");
            if(txtQty != null)
            { 
                Response.Write(txtQty.Text); 
            }
        }

response.write就是这样,我可以看到它正在工作而不是单步执行代码。正如您在转发器中看到的那样,每一行都有一个产品名称,延期交货的数量,以及一个要在收到的数量中输入的文本框来更新数据库。这就是问题所在,我如何知道哪个文本框适用于哪条记录?所以我可以更新数据库

修改

我改变了一些代码并想出了这个

标记

<td><asp:Label ID="hdnField" runat="server"><%# Eval("ProductID") %></asp:Label></td>

CodeBehind,添加到foreach

Label hdn = (Label)rptItem.FindControl("hdnField");
            if(txtQty != null)
            {
                Response.Write(txtQty.Text + "    ProductID: " + hdn.Text.ToString() + "<br>"); 
            }

单击运行该语句的按钮时,我可以获取文本框值,但不能获取具有ID的标签文本。

1 个答案:

答案 0 :(得分:0)

您可以在HiddenField中添加ItemTemplate,其中包含数据库中所需的ID。 当您获得数量(在foreach循环中)时,获取HiddenField值并进行数据库调用。