hisanth nithi

hisanth nithi

  • NA
  • 30
  • 9k

FindControl() returns null when searching for dynamic contro

May 4 2018 12:27 AM
In my requirement I am creating dynamic controls by two events
  1. Dropdownlist OnSelectedIndexChanged event
  2. Onclick button event
In OnSelectedIndexChanged event i have created number of dynamic textbox.
 
Here I have create two dynamic textboxes.
 
I am getting all enquiry product list based on enquiry number from the dynamic textboxes on button click event
 
 
All are dynamic controls
 
While saving the productlist the checkbox control return null value
  1. protected void ddlenqChaged(object sender, EventArgs e)    
  2. {    
  3.     getenqTextbox();    
  4. }    
  5. protected void btnSearchClick(object sender, EventArgs e)    
  6. {    
  7.     tblbill.Visible = true;    
  8.     
  9.     if (Convert.ToString(ViewState["Generated"]) != "true")    
  10.     {    
  11.         CreateDynamicControls();    
  12.         ViewState["Generated"] = "true";    
  13.     }    
  14. }
  15. protected void getenqTextbox()    
  16. {    
  17.     enqPanel.Controls.Clear();    
  18.     if (Convert.ToInt32(ddlNoofEnq.SelectedValue) > 0)    
  19.     {    
  20.         for (int i = 1; i <= Convert.ToInt32(ddlNoofEnq.SelectedValue); i++)    
  21.         {    
  22.             TextBox _txtCode = new TextBox();    
  23.             _txtCode.ID = "txtEnqqNo" + i;    
  24.             _txtCode.Attributes.Add("ClientIDMode""Static");    
  25.             _txtCode.Width = 75;    
  26.             _txtCode.CssClass = "AtCompleteByEnq";    
  27.             enqPanel.Controls.Add(_txtCode);    
  28.         }    
  29.     }    
  30. } 
  31. protected void CreateDynamicControls()    
  32. {
  33.     int m = 1;    
  34.     //int encount = click.buttonclick;    
  35.     
  36.     if (Convert.ToInt32(ddlNoofEnq.SelectedValue) > 0)    
  37.     {    
  38.         Table tbldynamic = new Table();
  39.         for (int k = 1; k <= Convert.ToInt32(ddlNoofEnq.SelectedValue); k++)    
  40.         {    
  41.             Panel1.Controls.Clear();    
  42.             TextBox tb = (TextBox)enqPanel.FindControl("txtEnqqNo" + k);    
  43.             if (tb != null)    
  44.             {    
  45.                 if (!String.IsNullOrEmpty(tb.Text))    
  46.                 {    
  47.                     List<Enquiry_ProductListBLL> objlst = Enquiry_ProductListBLL.GetEnquiry_ProductListBLLs(tb.Text);    
  48.     
  49.                     foreach (Enquiry_ProductListBLL item in objlst)    
  50.                     {    
  51.                         TableRow tr = new TableRow();    
  52.     
  53.                         TableCell tc = new TableCell();    
  54.                         TableCell tc1 = new TableCell();    
  55.                         TableCell tc2 = new TableCell();    
  56.                         TableCell tc3 = new TableCell();    
  57.                         TableCell tc4 = new TableCell();    
  58.                         TableCell tc5 = new TableCell();
  59.                         TableCell tc6 = new TableCell();
  60.                         Master_ProductBLL objpdt = Master_ProductBLL.GetMaster_ProductBLL(item.ProductId);    
  61.     
  62.                         CheckBox _chkRowNo = new CheckBox();    
  63.                         _chkRowNo.ID = "chkRowNo" + m;    
  64.                         _chkRowNo.Text = m.ToString();    
  65.                         _chkRowNo.Attributes.Add("ClientIDMode""Static");    
  66.                        _chkRowNo.Attributes.Add("onclick""getEnable(this);");    
  67.                         _chkRowNo.Checked = true;
  68.                         TextBox _txtCode = new TextBox();    
  69.                         _txtCode.ID = "txtCodeRow" + m;    
  70.                         _txtCode.Attributes.Add("ClientIDMode""Static");    
  71.                         _txtCode.Enabled = false;    
  72.                         _txtCode.Width = 75;    
  73.                         _txtCode.Attributes.Add("autocomplete""off");    
  74.                         _txtCode.Text = objpdt.Code;    
  75.     
  76.                         HiddenField _hdnPdtId = new HiddenField();    
  77.                         _hdnPdtId.ID = "hdnPdtId" + m;    
  78.                         _hdnPdtId.Value = item.ProductId.ToString();    
  79.     
  80.                         HiddenField _hdEnqNo = new HiddenField();    
  81.                         _hdEnqNo.ID = "hdEnqNo" + m;    
  82.                         _hdEnqNo.Value = item.EnqNo;    
  83.     
  84.                         _hdnPdtId.Value = item.ProductId.ToString();    
  85.                         TextBox _txtPdtName = new TextBox();    
  86.                         _txtPdtName.ID = "txtPdtNameRow" + m;    
  87.                         _txtPdtName.Attributes.Add("ClientIDMode""Static");    
  88.                         _txtPdtName.Enabled = false;    
  89.                         _txtPdtName.Width = 150;    
  90.                         _txtPdtName.CssClass = "AtCompleteByName";    
  91.                         _txtPdtName.Text = objpdt.Name;
  92.                         TextBox _txtQty = new TextBox();    
  93.                         _txtQty.ID = "txtQtyRow" + m;    
  94.                         _txtQty.Width = 80;    
  95.                         _txtQty.MaxLength = 8;    
  96.                         _txtQty.Attributes.Add("ClientIDMode""Static"); 
  97.                         _txtQty.Text = item.Count.ToString();    
  98.     
  99.                         DropDownList ddlUnits = new DropDownList();    
  100.                         ddlUnits.ID = "ddlUnits" + m;    
  101.                         ddlUnits.CssClass = "dropdowncss";    
  102.                         ddlUnits.Width = 100;    
  103.                         Bindings.BindUnitName(ddlUnits);    
  104.                         ddlUnits.Items.FindByText(item.PP).Selected = true;    
  105.     
  106.                         tc.Controls.Add(_chkRowNo);    
  107.                         tc1.Controls.Add(_txtCode);    
  108.                         tc2.Controls.Add(_txtPdtName);    
  109.                         tc2.Controls.Add(_hdnPdtId);    
  110.                         tc2.Controls.Add(_hdEnqNo);    
  111.                         tc3.Controls.Add(_txtBrand);    
  112.                         tc4.Controls.Add(_txtThickness);    
  113.                         tc5.Controls.Add(ddlUnits);    
  114.                         tc6.Controls.Add(_txtQty);    
  115.     
  116.                         tc.Attributes.Add("Width""50px");    
  117.                         tr.Attributes.Add("id""Rowid" + m);    
  118.                         tr.Attributes.Add("class""paidRw");    
  119.     
  120.                         tr.Cells.Add(tc);    
  121.                         tr.Cells.Add(tc1);    
  122.                         tr.Cells.Add(tc2);    
  123.                         tr.Cells.Add(tc3);    
  124.                         tr.Cells.Add(tc4);    
  125.                         tr.Cells.Add(tc5);    
  126.                         tr.Cells.Add(tc6);
  127.                         tbldynamic.Rows.Add(tr);    
  128.                         m++;    
  129.                     }    
  130.                 }    
  131.             }    
  132.        }
  133.         Panel1.Controls.Add(tbldynamic);    
  134.     }    
  135. }    
Here I have override OnLoad event
  1. protected override void OnLoad(EventArgs e)    
  2. {    
  3.     base.OnLoad(e);    
  4.     CreateDynamicControls();    
  5.     getenqTextbox();    
  6.     
  7. }  
My problem is i cannot find controls from Panel1
  1. CheckBox chk = (CheckBox)Panel1.FindControl(chkId);    
  2. if (chk != null)    
  3. {    
  4.     if (chk.Checked == true)    
  5.     {    
  6.         //my process    
  7.     }    
  8. }  
FindControl returns null value.
 
Here I have create dynamic controls two times.
 
I cannot have any problem with enquiry textbox, it was created first, but the second time button click event controls are return null.

Answers (4)