我得到了这个代码生成的表(我知道它看起来很糟糕,但它只是作为测试):
protected void Gen_Load(object sender, EventArgs e)
{
List<string> ParamList = new List<string>();
ParamList.Add("Perforation");
ParamList.Add("Top of perforation");
ParamList.Add("Bottom of perforation");
ParamList.Add("Well radius");
for (int i = 0; i < 4; i++)
{
TableRow row1=new TableRow();
TableCell cell1=new TableCell();
cell1.BorderColor=System.Drawing.Color.DarkGray;
cell1.BorderWidth=2;
cell1.Font.Size = 11;
cell1.Text=ParamList.ElementAt(i);
TableCell cell2 = new TableCell();
cell2.BorderColor=System.Drawing.Color.DarkGray;
cell2.BorderWidth=2;
cell2.Width = 200;
TextBox text1 = new TextBox();
text1.ID = "txtb_1" + i;
text1.Width = cell2.Width;
text1.Height = cell2.Height;
cell2.Controls.Add(text1);
TableCell cell3 = new TableCell();
cell3.BorderColor=System.Drawing.Color.DarkGray;
cell3.BorderWidth=2;
cell3.Width = 200;
DropDownList dlist = new DropDownList();
dlist.ID = "dlist_1" + i;
ListItem li1 = new ListItem("m");
ListItem li2 = new ListItem("ft");
// if (i != 0)
// {
dlist.Items.Add(li1);
dlist.Items.Add(li2);
// }
dlist.Width = cell3.Width;
dlist.Height = cell3.Height;
cell3.Controls.Add(dlist);
row1.Cells.Add(cell1);
row1.Cells.Add(cell2);
row1.Cells.Add(cell3);
row1.ID = "id" + i;
ParamTable.Rows.Add(row1);
}
}
那么如何从dropdownlist
和textbox
元素中获取数据呢?我假设我可以以某种方式使用元素ID
来调用它们,但我找不到任何示例。
UPD: aspx
<asp:Table CssClass="span9" style="margin-top:2px; margin-bottom:5px;" ID="ParamTable" runat="server">
<asp:TableHeaderRow >
<asp:TableHeaderCell Font-Size="Medium" BorderColor="DarkGray" BorderWidth="2px" >Parameters</asp:TableHeaderCell>
<asp:TableHeaderCell Font-Size="Medium" BorderColor="DarkGray" BorderWidth="2px" >Value</asp:TableHeaderCell>
<asp:TableHeaderCell Font-Size="Medium" BorderColor="DarkGray" BorderWidth="2px" >Units</asp:TableHeaderCell>
</asp:TableHeaderRow>
<asp:TableRow >
</asp:TableRow>
</asp:Table>
答案 0 :(得分:1)
请参阅以下代码以获取每行的值:
for (int i = 0; i < ParamTable.Rows.Count; i++)
{
var textBoxValue = ((TextBox)ParamTable.Rows[i].Cells[1].FindControl("txtb_1" + i)).Text;
var dropDownValue = ((DropDownList)ParamTable.Rows[i].Cells[2].FindControl("dlist_1" + i)).SelectedItem.Text;
}
注意:我不会在任何列表中存储值。您可以根据您的要求使用POCO / Property类。
<强>更新强>
从aspx中删除<asp:TableRow ></asp:TableRow>
并更新forloop,如下所述:
for (int i = 0; i < ParamTable.Rows.Count - 1; i++)
{
var textBoxValue = ((TextBox)ParamTable.Rows[i].Cells[1].FindControl("txtb_1" + i)).Text;
var dropDownValue = ((DropDownList)ParamTable.Rows[i].Cells[2].FindControl("dlist_1" + i)).SelectedItem.Text;
}