从代码背后查找使用Repeater创建的标记中的控件值

本文关键字:控件 创建 Repeater 背后 代码 查找 | 更新日期: 2023-09-27 18:27:42

我想我可能在应该使用其他东西的时候使用了中继器,所以我准备把它记为设计,但我想在更改之前与开发社区核实一下。

我还应该提前说,我正在aspx页面的自定义用户控件中使用此中继器控件。

我的情况是,当用户选择一个项目时,我必须动态地显示一个附加部分的列表。这类似于你有时在网上结账时看到的"你可能也感兴趣"列表。

因此,用户从下拉列表中选择要订购的项目,最多可以选择添加4个附加部件。

目前,我将在数据对象的通用列表中带回该可选部件列表,并将其绑定到中继器控件及其文本框。文本框基本上在一个框中列出了零件描述,用户可以在另一个文本框中键入他们想要的数量。

这一切都很好。

因此,需要明确的是,在中继器控件加载所有内容并呈现表单后,用户可以在数量文本框中键入值。

由于这一切都在用户控件中,我正在编写一个方法来收集所有这些信息,填充业务对象并将其返回给任何调用它的对象

我很难找到自动生成的文本框,以便检索它们的值。

我偷偷地怀疑我在设计中做了一些明显错误的事情。所以我想通过蜂箱思维来观察其他人的想法:)

这是中继器控件生成的标记。

<div class="base-container-controls-75pct">         
  <div class="base-container-controls-98pct">
    <div class="base-container-controls-75pct">
       <input name="DownLoadItem1$UxAdditionalParts$ctl01$UxItemNumber" type="text" id="DownLoadItem1_UxAdditionalParts_ctl01_UxItemNumber" class="textbox-readonly-xl" />
    </div>
    <div class="base-container-controls-10pct">
        <input name="DownLoadItem1$UxAdditionalParts$ctl01$UxQuantity" type="text" value="3" id="DownLoadItem1_UxAdditionalParts_ctl01_UxQuantity" class="textbox-md" />
    </div>  
  </div>
  ... more repeating code here, basically the 98pct div above is repeated for each "row" ...
</div>

从代码背后查找使用Repeater创建的标记中的控件值

我基本上是在把一些东西拼凑在一起,阅读了其他各种帖子后才明白这一点的。

我缺少的是,我需要将[EnableViewState="True"]添加到中继器的标记中。所以我的标记如下:

<asp:Repeater ID="UxAdditionalParts" runat="server" OnItemDataBound="UxAdditionalPartsItemDataBound" EnableViewState="True">
    <HeaderTemplate>
    </HeaderTemplate>
    <ItemTemplate>
    <div class="base-container-controls-98pct">
        <div class="base-container-controls-75pct">
            <asp:TextBox ID="UxItemNumber" runat="server" CssClass="textbox-readonly-xl"></asp:TextBox>
        </div>
        <div class="base-container-controls-10pct">
                <asp:TextBox ID="UxQuantity" runat="server" CssClass="textbox-md"></asp:TextBox>
        </div>  
    </div>
    </ItemTemplate>
    <FooterTemplate>
    </FooterTemplate>
</asp:Repeater>

一旦完成了这项工作,我就可以像预期的那样循环浏览控件,并获取它们的值,从中构建对象列表。结果如下:

// Find all textboxes, looping through them to build an object list.  Generally there is a maximum of 4 parts
// associated here but there could be more in the future so this should expand too.
for (int j = 0; j <= UxAdditionalParts.Items.Count - 1; j++)
    {
        if (UxAdditionalParts.Items[j].ItemType == ListItemType.Item || UxAdditionalParts.Items[j].ItemType == ListItemType.AlternatingItem)
            {
                TextBox txtItm = (TextBox)UxAdditionalParts.Items[j].FindControl("UxItemNumber");
                TextBox txtQty = (TextBox)UxAdditionalParts.Items[j].FindControl("UxQuantity");
                if (txtItm != null & txtQty != null)
                {
                    // Create a new part and add it to our list.
                    AdditionalPart objAdditionalPart = new AdditionalPart();
                    objAdditionalPart.ItemNumber = txtItm.Text;
                    objAdditionalPart.Quantity = Convert.ToInt32(txtQty.Text) ;
                    loAdditionalParts.Add(objAdditionalPart);
                }
            }
        } 

谜团解开了!