adding controls at runtime

K

kalaivanan

hi,
i am trying to add a datagrid control to a web page dynamically.
i am able to do it while i used the following code in the page load
event of the form in which i am going to add the control.

DataGrid dG = new DataGrid();
Control frm = FindControl("frmSnAInformation");
frm.Controls.Add(dG);
DataSet ds = nam_Db.dbFunctions.funFetchDataSet("fetchSuInfo_all");
dG.DataSource = ds.Tables[0];
dG.DataBind();


since i need to do the same process for many forms i have written the
code in a class file and call the function when needed. also i am
passing the form name as parameter. the class file function is as
follows:

public void funGrid(string frmNam)
{
DataGrid dG = new DataGrid();
Control frm = FindControl(frmANam);
frm.Controls.Add(dG);
DataSet ds = nam_Db.dbFunctions.funFetchDataSet("fetchSuInfo_all");
dG.DataSource = ds.Tables[0];
dG.DataBind();
}
but when i try to add control i am getting the following error:

System.NullReferenceException: Object reference not set to an instance
of an object.

at the line: frm.Controls.Add(dG);


what could be the reason and the work around for this problem.

regards,
kalaivanan
 
G

Guest

Hi there,

This line is likely the problem:
Control frm = FindControl(frmANam);

The problem is that you are trying to get a reference to the htmlform with
name passed by frmName parameter, but such form does not exist on the page.
try to review your code and see if you're passing correct ID of the control.
 
K

kalaivanan

Hi there,

This line is likely the problem:
Control frm = FindControl(frmANam);

The problem is that you are trying to get a reference to the htmlform with
name passed by frmName parameter, but such form does not exist on the page.
try to review your code and see if you're passing correct ID of the control.
--
Milosz



kalaivanan said:
hi,
i am trying to add a datagrid control to a web page dynamically.
i am able to do it while i used the following code in the page load
event of the form in which i am going to add the control.
DataGrid dG = new DataGrid();
Control frm = FindControl("frmSnAInformation");
frm.Controls.Add(dG);
DataSet ds = nam_Db.dbFunctions.funFetchDataSet("fetchSuInfo_all");
dG.DataSource = ds.Tables[0];
dG.DataBind();
since i need to do the same process for many forms i have written the
code in a class file and call the function when needed. also i am
passing the form name as parameter. the class file function is as
follows:
public void funGrid(string frmNam)
{
DataGrid dG = new DataGrid();
Control frm = FindControl(frmANam);
frm.Controls.Add(dG);
DataSet ds = nam_Db.dbFunctions.funFetchDataSet("fetchSuInfo_all");
dG.DataSource = ds.Tables[0];
dG.DataBind();
}
but when i try to add control i am getting the following error:
System.NullReferenceException: Object reference not set to an instance
of an object.
at the line: frm.Controls.Add(dG);
what could be the reason and the work around for this problem.
regards,
kalaivanan- Hide quoted text -

- Show quoted text -

you are absolutely right man.
i changed my code as given below;
since the code is executed in the class file the frm is assigned null.

Control frm = FindControl(frmANam);

hence i added one more parameter to the function.

public void funGrid(string frmANam, System.Web.UI.Page pg)
{
DataGrid dG = new DataGrid();
Control frm = pg.FindControl(frmANam);
frm.Controls.Add(dG);

DataSet ds = nam_Db.dbFunctions.funFetchDataSet("fetchSuInfo_all");
dG.DataSource = ds.Tables[0];
dG.DataBind();
}

I passed 'this' keyword as the second argument from the form in which
i call this funtion.
now 'frm' is not null and working too.

regards,
kalaivanan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top