【问题标题】:Getting the databound value from repeater control in asp.net从 asp.net 中的转发器控件获取数据绑定值
【发布时间】:2011-08-15 14:26:16
【问题描述】:

作为我的问题Looping through a repeater control to get values of Textbox in asp.net的后续行动 如果用户在文本框中输入了一个数量,我想从转发器控件中获取相应的 ProductID 值:

<asp:Repeater ID="rptRequestForm" runat="server">
                    <HeaderTemplate>
                            <table border="0" width="100%">
                                <tr>
                                    <td style="width:50%" class="TextFontBold"><asp:Label runat="server" ID="Label1" Text="Supply Name"></asp:Label></td>
                                    <td style="width:25%" class="TextFontBold"><asp:Label runat="server" ID="Label2" Text="Quantity"></asp:Label></td>
                                    <td style="width:25%" class="TextFontBold"><asp:Label runat="server" ID="Label3" Text="Price (ea.)"></asp:Label></td>
                                </tr>
                            </table>
                    </HeaderTemplate>
                        <ItemTemplate>
                            <table border="0" width="100%">
                                <tr>
                                    <td style="width:50%" class="TextFont"><asp:Label ID="lblProductName" Text="<%#Trim(Eval("Product_Title"))%>" ><%#Trim(Eval("Supplie_title"))%></asp:Label></td>
                                    <td style="width:25%"><asp:TextBox ID="txtBox" runat="server" Width="30%" onblur="Javascript:numberonly(this)"></asp:TextBox></td>
                                    <td style="width:25%" class="TextFont"><span><%#Trim(FormatCurrency(Eval("Price")))%></span></td>
                                </tr>
                            </table>
                        </ItemTemplate>
                    </asp:Repeater> 

我的代码隐藏是:

For Each item As RepeaterItem In rptRequestForm.Items
                txtField = item.FindControl("txtBox")
                If Not IsNothing(txtField) Then
                    j += 1
                    strText = strText & ", " & txtField.Text
                End If
Next

我正在使用 FindControl 循环遍历文本框(查看用户是否输入了有效数量)。如何获取对应ProductID的值?

【问题讨论】:

    标签: asp.net vb.net repeater


    【解决方案1】:

    为您的 ItemTemplate 添加标签,如下所示:

    <td style="width:50%" class="TextFont"><asp:Label ID="lblProductName" runat="server"><%#Trim(Eval("Product_Title"))%></asp:Label></td>
    

    在你后面的代码中,像这样找到它:

    foreach (RepeaterItem item in Repeater1.Items)
    {
        TextBox txtField = (TextBox)item.FindControl("txtBox");
    
        //extract value from textbox
        int Quantity = Convert.ToInt32(((TextBox)item.FindControl("txtBox").Text);
    
        //extract the value from the label
        string productName = ((Label)item.FindControl("lblProductName")).Text;
    }
    

    【讨论】:

    • 你的建议会给我所有的 productID 值(即使是用户没有输入数量的值),对吗?
    • 怎么样?您将从单个项目 (Repeater1.Items[0]) 中获取产品名称标签的值。
    • 我用我的代码隐藏更新了帖子。我正在对“在 rptRequestForm.Items 中作为 RepeaterItem 的项目”进行 For Each 循环。
    • 在您的行:Label lblProdTitle = (Label)item.FindControl("lblProductName"); .....我收到错误消息“'System.Web.UI.Control' 类型的值无法转换为 'String'”
    • 您没有将 runat="server" 添加到 lblProductName。您可能还需要将标签的文本属性用单引号而不是双引号括起来,因为您的 Eval() 函数需要双引号。
    【解决方案2】:

    您担心在没有数量的情况下获得产品名称,然后在分配之前进行评估。例如(基于@James 的代码),您只需在添加IF Quantity &gt; 0 THEN ... 之前添加string productName = ...

    不过,我会对该代码进行一些更改...

    1) 在尝试将其转换为文本框之前检查对象是否存在。例如,IF NOT item.FindControl("txtBox") is NOTHING...

    2) 在尝试convert.toint32() 之前检查空条件。例如,IF NOT string.isnullorempty(txtField)...

    3) 当您进行 Quantity 变量赋值时,您不需要 FindControl,因为您已经找到它(并将其分配给您的 txtField 变量)。所以就int Quantity = Convert.ToInt32(txtField.Text);

    【讨论】:

      【解决方案3】:

      您可以将ProductId 存储在转发器ItemTemplate 中的hidden field 中。然后您可以使用相同的方式访问产品 ID,就像您使用 FindControl 访问数量一样

      【讨论】:

      • 使用 FindControl 将返回所有 ProductID 值,即使是那些没有有效数量的值,对吗?
      猜你喜欢
      • 1970-01-01
      • 2018-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多