[英]Specified argument was out of the range of valid values. Parameter name: index on dynamically generated Control
i'm creating dynamic checkbox and textbox Control on page Load and i want to get the values of control on button click but we are not able to find out 我在“加载页面”上创建动态复选框和文本框“控件”,我想在单击按钮时获取控件的值,但我们无法找出
here is my Dynamic control Code 这是我的动态控制代码
DataTable dt = new DataTable();
da.Fill(dt);
var Count = dt.Rows.Count;
if (Count > 0)
{
TableHeaderRow thr = new TableHeaderRow();
TableHeaderCell cellheader = new TableHeaderCell();
TableHeaderCell thc = new TableHeaderCell();
TableHeaderCell thcode = new TableHeaderCell();
TableHeaderCell thcReason = new TableHeaderCell();
thc.Text = "Select";
thc.CssClass = "pd";
thcode.Text = "Description";
thcode.CssClass = "pdlbl";
thcReason.Text = "Reason";
thcReason.CssClass = "thcReason";
thr.Cells.Add(thc);
thr.Cells.Add(thcode);
thr.Cells.Add(thcReason);
tbl.Rows.Add(thr);
for (int i = 0; i < Count; i++)
{
TableRow tr = new TableRow();
TableCell tc = new TableCell();
TableCell tc1 = new TableCell();
TableCell tc2 = new TableCell();
CheckBox chk = new CheckBox();
Label lbl = new Label();
TextBox txtBox = new TextBox();
txtBox.ID = "txt" + i.ToString();
txtBox.TextMode = TextBoxMode.MultiLine;
chk.ID = "chk" + i.ToString();
lbl.ID = "lbl" + i.ToString();
lbl.Text = dt.Rows[i]["DESCRIPTION"].ToString();
tc.Controls.Add(chk);
tc.Width = new Unit("5px");
tc.CssClass = "chkcntrl";
tc1.Controls.Add(lbl);
tc1.Width = new Unit("75%");
tc2.Controls.Add(txtBox);
tc2.Width = new Unit("20%");
tr.Cells.Add(tc);
tr.Cells.Add(tc1);
tr.Cells.Add(tc2);
tbl.Rows.Add(tr);
}
tbl.EnableViewState = true;
ViewState["tbl"] = true;
}
after create the control i want to find out the values 创建控件后,我想找出值
my code are below 我的代码在下面
foreach (TableRow tr in tbl.Controls)
{
string str_Confirmed = string.Empty;
string str_Description = string.Empty;
string str_Reason = string.Empty;
foreach (TableCell tc in tr.Controls)
{
if (tc.Controls[0] is CheckBox)
{
if (((CheckBox)tc.Controls[0]).Checked == true)
{
str_Confirmed = "Y";
}
else
{
str_Confirmed = "N";
}
}
if (tc.Controls[1] is Label)
{
string txt = string.Empty;
str_Description = ((Label)tc.Controls[1]).Text;
}
if (tc.Controls[2] is TextBox)
{
str_Reason = ((TextBox)tc.Controls[2]).Text;
//Response.Write(((TextBox)tc.Controls[0]).Text);
}
} }
but when i will execute this code we are getting the Specified argument was out of the range of valid values. Parameter name: index
但是当我执行此代码时,我们得到的Specified argument was out of the range of valid values. Parameter name: index
Specified argument was out of the range of valid values. Parameter name: index
please any one can tell me where am wrong Specified argument was out of the range of valid values. Parameter name: index
任何人都可以告诉我哪里错了
You have different table cells for each control you are adding. 您要添加的每个控件都有不同的表格单元格。
tc has the checkbox control tc1 has the label control tc具有复选框控件tc1具有标签控件
EDIT: A few other changes were needed to your code. 编辑:您的代码需要一些其他更改。
Hope this helps 希望这可以帮助
foreach (TableRow tr in tbl.Rows)
{
string str_Confirmed = string.Empty;
string str_Description = string.Empty;
string str_Reason = string.Empty;
foreach (TableCell tc in tr.Cells)
{
if (tc.Controls.Count > 0)
{
if (tc.Controls[0] is CheckBox)
{
if (((CheckBox)tc.Controls[0]).Checked == true)
{
str_Confirmed = "Y";
}
else
{
str_Confirmed = "N";
}
}
else if (tc.Controls[0] is Label)
{
string txt = string.Empty;
str_Description = ((Label)tc.Controls[1]).Text;
}
else if (tc.Controls[0] is TextBox)
{
str_Reason = ((TextBox)tc.Controls[2]).Text;
//Response.Write(((TextBox)tc.Controls[0]).Text);
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.