我在我的项目中使用asp.net C#我们在面板中动态生成文本框我想获取文本框值,所以我使用foreach循环来获取文本框但是我没有得到文本框
这里我生成了控件
Table table = new Table();
table.ID = "table1";
table.Width = new Unit("100%");
table.BorderWidth = new Unit("1px");
table.CssClass = "tbl";
string Query = "SELECT * FROM XXCUS.MASTER_VERIFICATION";
OracleDataAdapter da = new OracleDataAdapter(Query, obj_Conn);
DataTable dt = new DataTable();
da.Fill(dt);
var Count = dt.Rows.Count;
if (Count > 0)
{
TableHeaderCell cellheader = new TableHeaderCell();
Label lblHeader = new Label();
lblHeader.Text = "Select";
Label lblDesc = new Label();
TableHeaderRow thr = new TableHeaderRow();
TableHeaderCell thc = new TableHeaderCell();
TableHeaderCell thcode = new TableHeaderCell();
TableHeaderCell thcReason = new TableHeaderCell();
thc.Text = "Select";
thcode.Text = "Description";
thcReason.Text = "Reason";
thr.Cells.Add(thc);
thr.Cells.Add(thcode);
//thr.CssClass = "pdlbl";
thc.Width = new Unit("5px");
thcode.Width = new Unit("75%");
thcode.CssClass = "thcode";
thcReason.Width = new Unit("20%");
thcReason.CssClass = "thcReason";
thr.Cells.Add(thcReason);
table.Rows.Add(thr);
for (int i = 0; i < Count; i++)
{
TableRow row = new TableRow();
TableCell cellchk = new TableCell();
TableCell celltxt = new TableCell();
TableCell celldesc = new TableCell();
TextBox txt = new TextBox();
CheckBox chk = new CheckBox();
txt.ID = "txt" + i.ToString();
txt.MaxLength = 250;
//txt.Width = new Unit("84%");
//txt.CssClass = "txtCl";
chk.ID = "chk" + i.ToString();
txt.TextMode = TextBoxMode.MultiLine;
cellchk.ID = "cellchk" + i.ToString();
celltxt.ID = "celltxt" + i.ToString();
cellchk.Controls.Add(chk);
//chk.Attributes.Add("onClick", "alert('" + chk.ID + "')");
Label lblDescription = new Label();
lblDescription.Text = dt.Rows[i]["DESCRIPTION"].ToString();
celldesc.Controls.Add(lblDescription);
cellheader.Controls.Add(lblHeader);
cellchk.Width = new Unit("5%");
//cellchk.CssClass = "pd";
celldesc.Width = new Unit("75%");
//celldesc.CssClass = "pdlbl";
celltxt.Width = new Unit("20%");
celltxt.Controls.Add(txt);
row.Cells.Add(cellchk);
row.Cells.Add(celldesc);
row.Cells.Add(celltxt);
table.Rows.Add(row);
}
dvGenerateCntrl.Controls.Add(table);
}
我正在使用面板添加控件
<asp:Panel ID="dvGenerateCntrl" runat="server">
</asp:Panel>
答案 0 :(得分:0)
我没有看到您的上述代码有任何问题,所以可能问题是如何将文本框控件添加到dvGenerateCntrol。
您是否也可以显示此代码。
下面是一些示例代码,用于向表中的单元格添加控件,然后查找迭代行和单元格以查找文本框。
Table t = new Table();
TableRow tr = new TableRow();
TableCell tc = new TableCell();
System.Web.UI.WebControls.TextBox tb = new System.Web.UI.WebControls.TextBox() { Text = "Testing" };
tc.Controls.Add(tb); //Add Textbox to TableCell Control Collection
tr.Cells.Add(tc); // Add TableCell to TableRow
t.Rows.Add(tr); //Add TableRow to Table
System.Web.UI.WebControls.Panel panel = new System.Web.UI.WebControls.Panel(); //This is your panel
panel.Controls.Add(t); // Add Table to Panel
Table tbl = panel.Controls.OfType<Table>().FirstOrDefault(); //Now to find the TextBox in the Table, in the Row, in the cell
foreach (TableRow row in tbl.Rows) //Loop through each TableRow
{
foreach (TableCell cell in row.Cells) //Loop through each TableCell
{
IEnumerable<System.Web.UI.WebControls.TextBox> txtBoxes = cell.Controls.OfType<System.Web.UI.WebControls.TextBox>(); //Search TableCell.Controls Collection for all TextBoxes
}
}