3

As a follow up to my question Looping through a repeater control to get values of Textbox in asp.net If user has entered a a quantity in the textbox, I want to get the corresponding ProductID value from the repeater control:

<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> 

My codebehind is:

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

I am using FindControl to loop through the textbox (to see if user has entered a valid quantity). How can I get the value of the corresponding ProductID ?

Community
  • 1
  • 1
Frank
  • 2,015
  • 10
  • 36
  • 57

3 Answers3

2

Add a label to your ItemTemplate, like this:

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

And in your code behind, find it like this:

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;
}
James Johnson
  • 45,496
  • 8
  • 73
  • 110
  • your suggestion would get me ALL the productID values (even the ones where the user has not enetered a quantity), correct? – Frank Aug 15 '11 at 14:50
  • How so? You would be grabbing the value of the product name label from a single item (Repeater1.Items[0]). – James Johnson Aug 15 '11 at 14:58
  • I updated the posting with my codebehind. I am doing a For Each loop on "item As RepeaterItem In rptRequestForm.Items". – Frank Aug 15 '11 at 15:04
  • on your line: Label lblProdTitle = (Label)item.FindControl("lblProductName"); ..... I get error message "Value of type 'System.Web.UI.Control' cannot be converted to 'String'" – Frank Aug 15 '11 at 15:15
  • if I change strProduct = item.FindControl("lblProductTitle") to be strProduct = item.FindControl("lblProductTitle").ToString() .......then I get an error message on "productName = lblProdTitle.Text" saying "'text' is not a member of 'String'" – Frank Aug 15 '11 at 15:18
  • Yea, because you need to cast it as a label. Look at my edited answer. – James Johnson Aug 15 '11 at 15:21
  • does not work. I get an error: Object reference not set to an instance of an object. – Frank Aug 15 '11 at 15:29
  • In your example, you are doing item.FindControl("lblProductTitle"), but my example is item.FindControl("lblProductName"). Please make sure that the IDs match up and try again. – James Johnson Aug 15 '11 at 15:34
  • Did you add the product name label in your item template, like in my example? This definitely works, so there's something missing from your code. – James Johnson Aug 15 '11 at 15:38
  • do you mean ID="lblProductName" ? If so, yes – Frank Aug 15 '11 at 15:44
  • Can you edit your question and post the code that you're using now? – James Johnson Aug 15 '11 at 15:45
  • 1
    You didn't add the runat="server" to lblProductName. You might also need to wrap the text property of the label in single quotes instead of double quotes, since your Eval() function needs double quotes. – James Johnson Aug 15 '11 at 15:54
1

You can store the ProductId in a hidden field in the repeater ItemTemplate. Then You can access the Product Id in the same way, as you are accessing the quantity using FindControl

Off The Gold
  • 1,228
  • 15
  • 28
Muhammad Akhtar
  • 51,913
  • 37
  • 138
  • 191
  • using FindControl will return all the ProductID values, even the ones without a valid quantity, correct? – Frank Aug 15 '11 at 14:59
1

You're worried about getting product names where there is no quantity, then just evaluate before you assign. For example (based on @James' code), you just add IF Quantity > 0 THEN ... before you do string productName = ...

A couple of changes I'd make to that code, though...

1) check for the object to exist before you attempting to convert it to a textbox. For example, IF NOT item.FindControl("txtBox") is NOTHING...

2) check for a null condition before you attempt convert.toint32(). for example, IF NOT string.isnullorempty(txtField)...

3) When you do the Quantity variable assignment, you don't need FindControl, because you already found it (and assigned it to your txtField variable). So just int Quantity = Convert.ToInt32(txtField.Text);

Chains
  • 12,541
  • 8
  • 45
  • 62