Object (usercontrol) reference problem
Hi!
I have a aspx page where I by default show 3 usercontrols. One of these controls is just a header, the second one (lets call it "ucForm") is a form with textboxes and the bottom one has a submit button for saving the information in the textboxes.
Also, there is a button for adding more "ucForm"'s. In other words, dynamically adding of user control.
The code I use to do this:
for (int i=1; i<=Nr; i++)
{
Control oControl = LoadControl("EmpRecord.ascx");
oControl.ID = "oEmpRecord" + i.ToString();
oPlcHldr.Controls.Add(oControl);
}
My problem is that I don't know how to create a reference to the dynamically created usercontrols. I keep getting a reference error...... I tried the following code in the usercontrol with the submit button:
EmpRecord oERC = (EmpRecord)LoadControl("EmpRecord.ascx");
oERC = (EmpRecord)this.Parent.FindControl("sControlName");
When I debug it, oERC is null.
I also have set the textboxes in the ucForm usercontrol to public and I have get/set properties for it. But that doesn't do me much good if I can't "reach" the controls drawn on the page!!
Can anyone please help me out!?!? I'm stuck and I need to finish this project soon!
Thanks!
Answers (4)
1
Hi,
I think in the above code, the for loop will skip first row. Try giving value of "i" in for loop as -1;
for (int i = -1; i < dataGridView2.Rows.Count - 1; i++)
{
Accepted 0
Midhun T P
Got it, thank you very much!
0
Nilesh Sawardekar
Good suggestion but not working. Changed to 2 my header gone, change to 0 it throw an error (Exception on HRESULT: 0x800A03EC). Any other suggestion?
0
int cellRowIndex = 1;
change value of it and check.