By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,318 Members | 2,116 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,318 IT Pros & Developers. It's quick & easy.

getting dynamically created control's value as null

P: 29
Dear All,
I am mess with one situation let me explain the scenario.
I am making search form where I display 15 field in checkboxlist and user select one or two or three or any no to all field. Depending on his selection (he tick the field and click filter button ) Now in filter button click I create label and dropdownlist control(populated from database) and add it to placeholder control. Now I select value and click the search button. Now in search button I need to access those control and its selected value(ie label and droplistbox ).
I have made function to create label and dropdownlistbox which is

private void createcontrols()
{
foreach (ListItem li in CheckBoxListmst.Items)
{
if (li.Selected == true)
{

//code added to fetch dropdownlist
setting = ConfigurationManager.ConnectionStrings["StyleSearchConnectionString"];
if (setting != null)
{
Code to access data for populating dropdownlistbox
try
{
// added controls
Label label = new Label();
DropDownList dropdownlist = new DropDownList();
label.ID = "label" + i.ToString();
dropdownlist.ID = "dropdownlist" + .ToString();


i++;
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Items for dropdownlist


}
PlaceHolder1.Controls.Add(label);
PlaceHolder1.Controls.Add(dropdownlist);



}

catch (SqlException ex)
{

}

Now this create function I call in one in filter button click event and second as

protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
createcontrols();
}

Now in seach button click event when I am trying to access
Code is

Label objlbl;
DropDownList objdrop;
objlbl = this.Page.FindControl("label1") as Label;
objdrop = this.Page.FindControl("dropdownlist1") as DropDownList;
if ((objlbl != null) && (objdrop != null))
{
cmd.Parameters.Add("@para1", SqlDbType.VarChar).Value = objlbl.Text; //Page.FindControl("label1").ToString() ;
cmd.Parameters.Add("@value1", SqlDbType.VarChar).Value = objdrop.SelectedValue;
}
I am getting objlbl and objdrop as null.


Please help


thanks
Jul 26 '07 #1
Share this Question
Share on Google+
1 Reply


Plater
Expert 5K+
P: 7,872
You will need to search for them with PlaceHolder1.FindControl("label1") since they are children of PlaceHolder1.
I believe there is also an overload to .FindControl() that allows searching of children.

In the following scenario (PlaceHolder1 contains the Labels and Page contains it and mytextbox):
Page
mytextbox
PlaceHolder1
Label1
Label2
(etc)

Page.FindControl() would let you find "PlaceHolder1" and "mytextbox", but to find the labels you would need to use the overload of FindControl() to search children's children, or do the search starting from PlaceHolder1
Jul 26 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.