Hisanth

Hisanth

  • NA
  • 1
  • 814

Re: How to get values from dynamically created controls

Jun 22 2014 1:08 AM
Hi,
   I have Create Dynamic Controls on dropdown SelectedIndexChanged event
 
protected void ddlcount_SelectedIndexChanged(object sender, EventArgs e)
{
Table tbldynamic = new Table();
for (int i = 1; i <= count; i++)
{

tr = new TableRow();


tc1 = new TableCell();
tc2 = new TableCell();
tc3 = new TableCell();
tc4 = new TableCell();

TextBox _txtQty = new TextBox();

_txtQty.ID = "txtQtyRow" + i;
_txtQty.Width = 50;

TextBox _txtRate = new TextBox();

_txtRate.ID = "txtRateRow" + i;
_txtRate.Width = 50;

TextBox _txtAmount = new TextBox();

_txtAmount.ID = "txtAmountRow" + i;
_txtAmount.Width = 50;

CheckBox _chkRowNo = new CheckBox();
_chkRowNo.ID = "chkRowNo" + i;
_chkRowNo.Text = i.ToString();



tc1.Controls.Add(_txtQty);
tc2.Controls.Add(_txtRate);
tc3.Controls.Add(_txtAmount);
tc4.Controls.Add(_chkRowNo);

tr.Cells.Add(tc4);
tr.Cells.Add(tc1);
tr.Cells.Add(tc2);
tr.Cells.Add(tc3);

tbldynamic.Rows.Add(tr);

}
}
 
 
 I need to get values from dynamically generated  Textbox on Button Click Event.
 
  I am trying below,
 
protected void btnSave_Click(object sender, EventArgs e)
{
 
CheckBox chk = (CheckBox)Page.FindControl("MainContent").FindControl("MultiviewId").FindControl("view1").FindControl("Panel1").FindControl("chkRowNo1");
 
}
 
 Here

MainContent = ContentPlaceHolderID,

MultiViewId = MultiView,

ViewId = view1,

PanelId = Panel1,

chkRowNo1 = Dynamically generated checkbox

I have also tried
CheckBox chk = (CheckBox)Page.FindControl("chkRowNo1");
 
CheckBox chk = (CheckBox)Panel1.FindControl("chkRowNo1");
 
But no use. It shows an object reference error. While debugging it shows null.
 
 Some one could you help me...