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

Access RadTextBox in Table

2 Answers 131 Views
Input
This is a migrated thread and some comments may be shown as answers.
Jon
Top achievements
Rank 1
Jon asked on 21 Jul 2011, 07:05 PM
Group,
Not sure there is an easy way to explain this, but basically I have an HTML table containing 4 columns with a bunch of RadTextBox controls.  What I am trying to do is iterate through each row of the HTML table and grab the the value of the RadTextBox based off of the cell index.

Below is a snippet of the code which appears to work for a regular asp textbox but not a radtextbox.  I get the error:
Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'Telerik.Web.UI.RadTextBox'.

foreach (HtmlTableRow row in uc.RecommendationTable.Rows)
{
 var rec1 = (RadTextBox)row.Cells[1].Controls[0];
}

 

i basically want to avoid having to use any find controls or anything because there are like 20 rows of 4 columns.  I want to iterate through each row and give me the value of the control located in each column of the HTML table.

2 Answers, 1 is accepted

Sort by
0
Accepted
Vasil
Telerik team
answered on 26 Jul 2011, 10:00 AM
Hi Jon,

It seems that the first control inside the row.Cells[1] is not RadTextBox but a LiteralControl and this is the reason that you are unable to cast it. Make sure that the table really contains RadTextBoxes.
Here is an example:
ASPX:
<asp:Table runat="server" ID="table1">
  <asp:TableRow>
    <asp:TableCell>
    <telerik:RadTextBox runat="server"></telerik:RadTextBox>
    </asp:TableCell>
    <asp:TableCell>
      <telerik:RadTextBox runat="server">
      </telerik:RadTextBox>
    </asp:TableCell>
  </asp:TableRow>
</asp:Table>

C#:
protected void Page_Load(object sender, EventArgs e)
{
    foreach (TableRow row in table1.Rows)
    {
        RadTextBox radTextBox1 = row.Cells[0].Controls[0] as RadTextBox;
        radTextBox1.Text = "textbox in first column";
        RadTextBox radTextBox2 = row.Cells[1].Controls[0] as RadTextBox;
        radTextBox2.Text = "textbox in second column";
    }
}


Kind regards,
Vasil
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

0
Jon
Top achievements
Rank 1
answered on 01 Aug 2011, 07:42 PM
Works great thanks!
Tags
Input
Asked by
Jon
Top achievements
Rank 1
Answers by
Vasil
Telerik team
Jon
Top achievements
Rank 1
Share this question
or