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

Find Dynamically Created Table in RadWindow

1 Answer 104 Views
Window
This is a migrated thread and some comments may be shown as answers.
Eric Klein
Top achievements
Rank 1
Eric Klein asked on 01 Jun 2011, 04:09 PM
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. 

1 Answer, 1 is accepted

Sort by
0
Marin Bratanov
Telerik team
answered on 03 Jun 2011, 02:39 PM

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.

Tags
Window
Asked by
Eric Klein
Top achievements
Rank 1
Answers by
Marin Bratanov
Telerik team
Share this question
or