如何在c#中的master的子页面中动态创建控件? [英] How to create controls dynamically in master's child pages in c#?
本文介绍了如何在c#中的master的子页面中动态创建控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已成功动态创建控件但是当我应用母版页然后抛出错误对象引用未设置为对象的实例。下面是我的代码。
protected void GenerateTableOnDelete(int rowsCount,int deleteID)
{
rowsCount-- ;
表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;
}
此行获取错误第49行:表table =(表)this.Page.FindControl(PlaceHolder1 ).FindControl( 表1\" ); // **** if(table!= null){
private void SetPreviousData(int rowsCount,int colsCount)
{
Table table =(Table)this.Page.FindControl(ContentPlaceHolder1)。FindControl(Table1); // **** if(table!= null){
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);
//使用Request对象获取动态文本框的先前数据
ddl.Text = Request.Form [DropDownListRow_+ i +Col_+ j]; // ***** }
}
}
}
}
解决方案
1.问题是由第一个FindControl(ContentPlaceHolder1)
调用生成的,无法找到您的控件,可能是因为您使用了错误的控件ID。
2.您的占位符是一个服务器对象,您应该像在GenerateTableOnDelete
方法中那样直接访问它。所以你应该用这样的问题改变你的代码:
Table table =(Table)PlaceHolder1.Controls.FindConntrol( Table1);
if (table!= null )
{
// 您的代码。
// ...
}
得到答案..有待访问下面。
private void SetPreviousData(int rowsCount,int colsCount)
{
ContentPlaceHolder content =(ContentPlaceHolder)this.Master.FindControl(ContentPlaceHolder1 );
PlaceHolder plz =(PlaceHolder)content.FindControl(PlaceHolder1);
表table =(表)plz.FindControl(Table1);
/ * --------------
------------- * /
}
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"); // **** if (table != null) { for (int i = 0; i < rowsCount; i++) { for (int j = 0; j < colsCount; j++) { if (j == colsCount - 6) { //Extracting the Dynamic Controls from the Table DropDownList ddl = (DropDownList)table.Rows[i+1].Cells[j].FindControl("DropDownListRow_" + i + "Col_" + j); //Use Request object for getting the previous data of the dynamic textbox ddl.Text = Request.Form["DropDownListRow_" + i + "Col_" + j];//***** } } } } }
解决方案
1.The problem is generated by the firstFindControl("ContentPlaceHolder1")
call, that cannot find your control, maybe because you are using a wrong control ID.
2.Your placeholder is a server object and you should access it directly like you did in yourGenerateTableOnDelete
method. So you should change your code with problem like this:
Table table = (Table)PlaceHolder1.Controls.FindConntrol("Table1"); if(table != null) { // Your code. //... }
got the answer..have to access like below.
private void SetPreviousData(int rowsCount, int colsCount) { ContentPlaceHolder content = (ContentPlaceHolder)this.Master.FindControl("ContentPlaceHolder1"); PlaceHolder plz = (PlaceHolder)content.FindControl("PlaceHolder1"); Table table = (Table)plz.FindControl("Table1"); /*-------------- -------------*/ }
这篇关于如何在c#中的master的子页面中动态创建控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文