I am adding dynamic textbox and label. I want to create dynamic control with label and textbox. After that I need to get the value on from submit, but I am not able to get the value I added my control into table.
if (!string.IsNullOrEmpty(dynamicFieldstring))
{
string[] groupControl = dynamicFieldstring.Split('|');
for (int i = 0; i < groupControl.Length; )
{
TableRow row = new TableRow();
for (int j = 0; j < 2; j++)
{
if (i >= groupControl.Length)
break;
string[] singleControl = groupControl[i].Split('=');
Label label = new Label();
TextBox textbox = new TextBox();
string textboxId = string.Empty;
string labelId = string.Empty;
TableCell cell = new TableCell();
label.Text = singleControl[0];
labelId = "lbl" + i + j;
label.ID = textboxId;
label.Attributes.Add("runat", "server");
cell.Controls.Add(label);
TableCell cell1 = new TableCell();
textbox.Text = singleControl[1];
textboxId = "txt" + i + j;
textbox.ID = textboxId;
textbox.Attributes.Add("runat", "server");
cell1.Controls.Add(textbox);
_dynamicControlId.Add(labelId, textboxId);
Session["controllIds"] = _dynamicControlId;
// Add the TableCell to the TableRow
row.Cells.Add(cell);
row.Cells.Add(cell1);
//}
i = i + 1;
}
extraAttr.Rows.Add(row);
}
Value added successfully, but when I am trying to get value from the code. I am getting null value. This is my code for get the value:
string value = string.Empty;
_dynamicControlId = (Dictionary<string, string>)Session["controllIds"];
if (_dynamicControlId != null && _dynamicControlId.Count > 0)
{
TextBox textbox;
for (int i = 0; i < this.Controls.Count; i++)
{
var control= this.Controls[i];
}
foreach (var item in _dynamicControlId)
{
Label label = (Label)this.FindControl(item.Key);
//value = Request.Form("something_TextoxFirstName");
value = label.Text + "=";
textbox = (TextBox)this.FindControl(item.Value);
value += textbox.Text + "|";
}
}
return value;