I have few textboxes on a page which are loaded dynamically on change of text in dropdownlist. The user enters values in these textboxes and clicks on a addnewitem button. In the code behind I want to capture the values entered in these textboxes. But I get the value as empty.
On Pageload mthd there is nothing which is referencing to createdynamiccontrols method.Then I have
protected override void CreateChildControls()
{
base.CreateChildControls();
CreateDynamicControls();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
}
protected override object SaveViewState()
{
return new Pair(base.SaveViewState(), null);
}
protected override void LoadViewState(object savedState)
{
base.LoadViewState(((Pair)savedState).First);
EnsureChildControls();
}
public void CreateDynamicControls()
{
Student= ddlStudentschool.SelectedItem.Text.ToString();
studentcollege= ddlstudentcollege.SelectedValue.ToString();
int rowPos = 0;
DynPanel.BorderStyle = BorderStyle.Solid;
Based on the selected of studentschool and studentcollege, the following textboxes & fieldnames will be created
Label Mynewlabel = new Label();
Mynewlabel.ID = "lbl" + FldLabel;
Mynewlabel.Text = FldLabel;
Mynewlabel.Visible = true;
Mynewlabel.Font.Bold = true;
DynPanel.Controls.Add(new LiteralControl("<td bgcolor=#333>"));
DynPanel.Controls.Add(Mynewlabel);
DynPanel.Controls.Add(new LiteralControl("</td>"));
if (ControlType == "TextBox")
{
int textBoxLength;
TextBox MynewTextBox = new TextBox();
MynewTextBox.ID = "txt" + Fldname;
MynewTextBox.Width = 100;
MynewTextBox.EnableViewState = true;
MynewTextBox.Enabled = true;
did not paste the whole code
}
protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
{
CreateDynamicControls();
}
protected void btnAddItem_Click(object sender, EventArgs e)
{
CreateDynamicControls();
if (DynPanel.Controls != null)
{
foreach (Control ctrl in DynPanel.Controls)
{
if (ctrl is TextBox)
{
TextBox _txt = DynPanel.FindControl(ctrl.ID) as TextBox;
int count = DynPanel.Controls.Count;
// txtresult.ID = ctrl.ID;
txtresult.Text = ((TextBox)ctrl).Text;
TextBox txtPath = (TextBox)ctrl.FindControl("");
string result = txtresult.Text;
}
Firstly i am getting object reference not set to an instance of an object when the createchildcontrols method is called the second time and also if I dont have the createdynamiccontrols method in additem click event, controls are no longer tehre...