Asma Siddiqua

Asma Siddiqua

  • NA
  • 23
  • 2.4k

create controls dynamically in master's child pages in C#?

Jun 1 2015 4:47 AM
I have creating controls dynamically successfully but when i applied master page then throwing an error "Object reference not set to an instance of an object." below is the my code.
 
protected void GenerateTableOnDelete(int rowsCount, int deleteID) 
{             
rowsCount--; 
Table table = new Table();             
table.ID = "Table1"; 
PlaceHolder1.Controls.Add(table); 
const int colsCount = 6; 
TableHeaderRow header = new TableHeaderRow(); 
TableHeaderCell headerTableCell1 = new TableHeaderCell();             
headerTableCell1.Text = "Item Name";            
 header.Cells.Add(headerTableCell1);                             
table.Rows.Add(header); 
for (int i = 0, k = 0; i < rowsCount; i++) 
{ 
if (i < deleteID)                    
 k = i; else                    
 k = i - 1; 
if (deleteID != i) 
{ 
TableRow row = new TableRow();                    
 row.ID = "Row_" + k; 
for (int j = 0; j < colsCount; j++)
 { 
if (j == colsCount - 6) 
{ 
TableCell cell = new TableCell(); 
DropDownList ddl = new DropDownList();                                                        
ds = fetchStates();                            
 ddl.DataSource = ds.Tables[0];                            
 ddl.DataValueField = "EmpId";                            
 ddl.DataTextField = "EmpName";                             
ddl.DataBind();                            
 ddl.ID = "DropDownListRow_" + k + "Col_" + j;                             
cell.Controls.Add(ddl);                             
row.Cells.Add(cell); 
} }                     
table.Rows.Add(row); 
} } 
SetPreviousDataOnDelete(rowsCount, colsCount, deleteID); 
ViewState["RowsCount"] = rowsCount; 
}
 at this line getting error Line 49: Table table = (Table)this.Page.FindControl("PlaceHolder1").FindControl("Table1"); // **** if (table != null) {
 
private void SetPreviousData(int rowsCount, int colsCount) 
{ 
Table table = (Table)this.Page.FindControl("ContentPlaceHolder1").FindControl("Table1");
for (int i = 0; i < rowsCount; i++) 
{ 
for (int j = 0; j < colsCount; j++) 
{ 
if (j == colsCount - 6) 
{ 
 DropDownList ddl = (DropDownList)table.Rows[i+1].Cells[j].FindControl("DropDownListRow_" + i + "Col_" + j); 
 ddl.Text = Request.Form["DropDownListRow_" + i + "Col_" + j];
 } } } }
 

Answers (3)