Find Dynamically Created Table in RadWindow

2 posts, 0 answers
  1. Eric Klein
    Eric Klein avatar
    183 posts
    Member since:
    Aug 2009

    Posted 01 Jun 2011 Link to this post

    I have some code that works fine in an aspx page but I would like to use it in a radwindow.
    On the page I have numerous input controls but one of the things is a table that has a button that allowas the user to add another row of inputs.  When they hit the button it creates another row in the dynamically created table, but since the rows are created each time I need to reload the old data into the past rows.

    private void SetPreviousAccountData(int rowsCount)
           {
               if (Page.IsPostBack)
               {
                   Table table = (Table)Page.FindControl("Table1");
                   if (table != null)
                   {
                       for (int i = 0; i < rowsCount; i++)
                       {
                           //Extracting the Dynamic Controls from the Table
                           DropDownList ddAccount = (DropDownList)table.Rows[i].Cells[1].FindControl("ddAccount" + i + "Col_1");
                           //Use Request objects for getting the previous data of the dynamic textbox
                           ddAccount.Text = Request.Form["ddAccount" + i + "Col_1"];
                           TextBox txtProxy = (TextBox)table.Rows[i].Cells[3].FindControl("txtProxy" + i + "Col_3");
                           //Use Request objects for getting the previous data of the dynamic textbox
                           txtProxy.Text = Request.Form["txtProxy" + i + "Col_3"];
                       }
                   }
               }
           }
           private void AddAccount(int rowsCount, bool addNewRow)
           {
               //Sore the current Rows Count in ViewState
               if (addNewRow)
               {
                   rowsCount++;
                   ViewState["AccountRowCount"] = rowsCount;
               }
               //Creat the Table and Add it to the Page
               Table table = new Table();
               table.ID = "Table1";
               AccountSpan.Controls.Add(table);
               for (int i = 0; i < rowsCount; i++)
               {
                   TableRow tRow = new TableRow();
                   TableCell lblAccountCell = new TableCell();
                   TableCell tcAccountCell = new TableCell();
                   TableCell lblProxyCell = new TableCell();
                   TableCell tcProxyCell = new TableCell();
                   DropDownList ddAccount = new DropDownList();
                   TextBox txtProxy = new TextBox();
                   lblAccountCell.Text = "Account Number:";
                   lblProxyCell.Text = "Proxy:";
                   ddAccount.ID = "ddAccount" + i + "Col_1";
                   txtProxy.ID = "txtProxy" + i + "Col_3";
                   if (ddClientID.SelectedIndex > 0)
                   {
                       ClientDataContext db = new ClientDataContext();
                       var query = from p in db.ProxyAccounts
                                   where p.ClientID == Int32.Parse(ddClientID.SelectedValue)
                                   select new
                                   {
                                       p.ProxyAccountID,
                                       Account = p.NameOnAccount + ": " + p.AccountNumber
                                   };
                       ddAccount.DataSource = query.ToList();
                       ddAccount.DataTextField = "Account";
                       ddAccount.DataValueField = "ProxyAccountID";
                       ddAccount.DataBind();
                       ddAccount.Items.Insert(0, "");
                   }
                   tcAccountCell.Controls.Add(ddAccount);
                   tcProxyCell.Controls.Add(txtProxy);
                   tRow.Cells.Add(lblAccountCell);
                   tRow.Cells.Add(tcAccountCell);
                   tRow.Cells.Add(lblProxyCell);
                   tRow.Cells.Add(tcProxyCell);
                   table.Rows.Add(tRow);
               }
               SetPreviousAccountData(rowsCount);
           }


    The problem is the line  Table table = (Table)Page.FindControl("Table1");  does not find the table.

    I guess if I could find a way to simply just add a new row to the table on the button push this issue would be solved but have not be able to figure that out yet. 

  2. Marin Bratanov
    Admin
    Marin Bratanov avatar
    3595 posts

    Posted 03 Jun 2011 Link to this post

    Hi Eric,

    You can use the RadWindow's ContentContainer property as the parent element in which to find the table. The FindControl() method finds only direct descendants of the said parent and this is the reason why you cannot find the table inside the RadWindow's INaming container when you search within Page.

    For your convenience I created and attached a sample page based on the provided code that achieves the desired functionality. Please note that I have removed the part that extracts the data from the database since I do not have it. I also hardcoded an initial value in the ViewState for demonstration purposes.



    All the best,
    Marin
    the Telerik team

    Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top