This is a migrated thread and some comments may be shown as answers.

Getting values from textbox in item template

2 Answers 31 Views
ListBox
This is a migrated thread and some comments may be shown as answers.
Henry Franzoni
Top achievements
Rank 1
Henry Franzoni asked on 28 Aug 2015, 10:52 PM

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.Clear();
                    cmd4.Parameters.AddWithValue("@Product_ID", item.Value);
                    cmd4.Parameters.AddWithValue("@Quantity", Convert.ToInt32(item.FindControl("RadNumericTextBox1.Text") as TextBox));
                    cmd4.ExecuteNonQuery();
                }​

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

 

                              

2 Answers, 1 is accepted

Sort by
0
Henry Franzoni
Top achievements
Rank 1
answered on 28 Aug 2015, 11:02 PM

PS - my item template:

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

                          </asp:Table>
                         
                       </ItemTemplate>​

0
Accepted
Aneliya Petkova
Telerik team
answered on 31 Aug 2015, 09:59 AM
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.

Regards,
Aneliya Petkova
Telerik
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
Tags
ListBox
Asked by
Henry Franzoni
Top achievements
Rank 1
Answers by
Henry Franzoni
Top achievements
Rank 1
Aneliya Petkova
Telerik team
Share this question
or