TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Michell Turley
NA
9
3.3k
Access data from dynamically created controls
Mar 8 2011 3:53 PM
I'm working on a report management system. Each report has its own parameters. Parameter names and saved values are kept in the database.
When the user selects a report from a GridView, the parameter names and values are dynamically created like so. Note that during development, I'm just adding a Label for the Parameter Name and a Textbox for the Parameter Value. In its final version, there will be several different types of Parameter Value controls (e.g., DropDownList, ListBox, Calendar).
DataSet ds = dal.GetSavedParameters(reportID);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
DataRow dr = ds.Tables[0].Rows[i];
Label lbl = new Label();
Literal ltl = new Literal();
ltl.ID = "ltl" + i.ToString();
lbl.ID = "lbl" + i.ToString();
lbl.Text = dr["ParameterName"].ToString();
TextBox txt = new TextBox();
txt.ID = "parm" + i.ToString(); // any control will have the ID "parm" + i, not just TextBoxes
txt.Text = dr["ParameterValue"].ToString();
pnlParameters.Controls.Add(lbl); // an asp:Panel
pnlParameters.Controls.Add(txt);
pnlParameters.Controls.Add(ltl); // to add a line break between parameter name/value pairs
}
This will create dynamic controls named "lbl0", "ltl0", "parm0", "lbl1", "ltl1", "parm1", etc.
Now when it's time to Update the Parameters (i.e., the user has decided to store different values for this report). I can't find the Labels or Textboxes. I've used some of the following:
DataSet ds = dal.GetSavedParameters(reportID); // get the stored values for update later
DataTable dt = ds.Tables[0];
foreach(DataRow row in dt.Rows)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
string labelName = "lbl" + i.ToString();
string valueName = "parm" + i.ToString();
Label lblName = new Label();
lblName = (Label)pnlParameters.FindControl(labelName);
...
}
lblName keeps coming back "null". By the way, if I use the name of a "real" label (e.g., "lblTest"), this word just fine.
I've also tried a helper method:
private static Control Find(Control C, string ControlName)
{
if (C.ID == ControlName) return C;
foreach(Control c in C.Controls)
{
Control cntrl = Find(c, ControlName);
if (cntrl != null) return c;
}
return null
}
This is called as:
Label lblName = (Label)Find(pnlParameters, labelName);
Label still comes back null.
Any ideas?
TIA,
Michell
Reply
Answers (
9
)
how to import csv data into Oracle using c#
Passing function as argument