Access RadTextBox in Table

3 posts, 1 answers
  1. Jon
    Jon avatar
    12 posts
    Member since:
    Jul 2010

    Posted 21 Jul 2011 Link to this post

    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. Answer
    Vasil
    Admin
    Vasil avatar
    1638 posts

    Posted 26 Jul 2011 Link to this post

    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!

  3. Jon
    Jon avatar
    12 posts
    Member since:
    Jul 2010

    Posted 01 Aug 2011 Link to this post

    Works great thanks!
Back to Top