Getting values from textbox in item template

3 posts, 1 answers
  1. Henry Franzoni
    Henry  Franzoni avatar
    2 posts
    Member since:
    Nov 2009

    Posted 28 Aug 2015 Link to this post

    In the ListBox ItemTemplate Demo, a numeric textbox is included to allow users to enter order Quantity. No problem creating a similar item template. No problem passing item.Value to SqlParameter and inserting to database. But I cannot figure out how to grab a value from a textbox and pass that as another parameter.

    Code below passed an empty string or NULL to @Quantity: 


     foreach (RadListBoxItem item in collection)
                        cmd4.Parameters.AddWithValue("@Product_ID", item.Value);
                        cmd4.Parameters.AddWithValue("@Quantity", Convert.ToInt32(item.FindControl("RadNumericTextBox1.Text") as TextBox));

    A few articles I found suggest textbox values are lost during postback? Anyone else run into this problem? Advice appreciated.



  2. Henry Franzoni
    Henry  Franzoni avatar
    2 posts
    Member since:
    Nov 2009

    Posted 28 Aug 2015 in reply to Henry Franzoni Link to this post

    PS - my item template:

                              <asp:Table runat="server">
                                      <asp:TableCell Width ="300px">
                                        <%# DataBinder.Eval(Container, "Text")%>
                                          <telerik:RadNumericTextBox runat="server" ID="QuantityTextBox" Width="60px" MinValue="0"
                                        MaxValue="1000" ShowSpinButtons="true" Value="0" NumberFormat-DecimalDigits="0">


  3. Answer
    Aneliya Petkova
    Aneliya Petkova avatar
    207 posts

    Posted 31 Aug 2015 Link to this post

    Hi Henry,

    Please note that .FindControl method takes the searched control's ID as an argument. So in your case, there is no control with RadNumericTextBox1 ID in the ItemTemplate of the RadListBox. The following line of code:
    cmd4.Parameters.AddWithValue("@Quantity", Convert.ToInt32(item.FindControl("RadNumericTextBox1.Text") as TextBox));

    should be changed with something like this:
    cmd4.Parameters.AddWithValue("@Quantity", Convert.ToInt32(((RadNumericTextBox)item.FindControl("QuantityTextBox")).Text));

    Hope this will be helpful.

    Aneliya Petkova
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top