在asp.net的html表中添加下拉列表
Posted
技术标签:
【中文标题】在asp.net的html表中添加下拉列表【英文标题】:Adding dropdown list in html table in asp.net 【发布时间】:2018-07-31 14:26:52 【问题描述】:我正在从数据库中填充 ID 和值并绑定到下拉列表。我想将下拉列表添加到后面代码中为第 2 列的每一行生成的 html 表中。我为下拉列表提供了不同的 id,但它似乎不起作用,只有最后一行获取下拉列表。
如果我不填充数据而只使用 id 属性,则下拉列表会出现在表格的每一行中。同样,如果我将项目随机添加到下拉列表中,那么事情就会再次起作用。但它不适用于绑定数据。
下面是我的代码:
.cs
protected void Page_Load(object sender, EventArgs e)
if(!Page.IsPostBack)
// Generate tabel for employer
PopulateDropDown();
Displaytable(numOfRows);
private DropDownList PopulateDropDown()
try
DC.dbConnection = Database.getInstance();
DC.dbConnection.Open();
DC.dbCommand = new SqlCommand("SP_Employee", DC.dbConnection);
DC.dbCommand.CommandType = System.Data.CommandType.StoredProcedure;
DC.dbCommand.Parameters.AddWithValue("@Action", "SelectEmployeeList");
lst.DataSource = DC.dbCommand.ExecuteReader();
lst.DataTextField = "Name";
lst.DataValueField = "Id";
lst.DataBind();
DC.dbConnection.Close();
catch(Exception ex)
return lst;
private void Displaytable(int rowsCount)
try
Table tbl = new Table();
tbl.ID = "PendingTable";
tbl.Width = Unit.Percentage(100);
tbl.Rows.Add(new TableRow());
tbl.Rows[tbl.Rows.Count - 1].Cells.Add(new TableHeaderCell());
tbl.Rows[tbl.Rows.Count - 1].Cells[tbl.Rows[tbl.Rows.Count - 1].Cells.Count - 1].Text = "First Name";
tbl.Rows[tbl.Rows.Count - 1].Cells.Add(new TableHeaderCell());
tbl.Rows[tbl.Rows.Count - 1].Cells[tbl.Rows[tbl.Rows.Count - 1].Cells.Count - 1].Text = "Last Name";
tbl.Rows[tbl.Rows.Count - 1].Cells.Add(new TableHeaderCell());
tbl.Rows[tbl.Rows.Count - 1].Cells[tbl.Rows[tbl.Rows.Count - 1].Cells.Count - 1].Text = "Employer";
tbl.Rows[tbl.Rows.Count - 1].Cells.Add(new TableHeaderCell());
tbl.Rows[tbl.Rows.Count - 1].Cells[tbl.Rows[tbl.Rows.Count - 1].Cells.Count - 1].Text = "Email Address";
const int colsCount = 4;
for (int i = 0; i < rowsCount; i++)
TableRow row = new TableRow();
for (int j = 0; j < colsCount; j++)
TableCell cell = new TableCell();
TextBox tb = new TextBox();
// Set a unique ID for each TextBox added
tb.ID = "TextBoxRow_" + i + "Col_" + j;
if (j == 2)
DropDownList newlst = new DropDownList();
newlst = PopulateDropDown();
newlst.ID = Guid.NewGuid().ToString();
cell.Controls.Add(newlst);
row.Cells.Add(cell);
else
// Add the control to the TableCell
cell.Controls.Add(tb);
row.Cells.Add(cell);
tbl.Rows.Add(row);
Panel1.Controls.Add(tbl);
catch (Exception ex)
【问题讨论】:
【参考方案1】:好吧,我只是通过添加项目而不是添加 datatextfield 和 datavaluefield 以某种方式进行管理。所以下面是我如何填充下拉项目并在每一行中显示。
private DropDownList PopulateDropDown()
DropDownList lst = new DropDownList();
try
DC.dbConnection = Database.getInstance();
DC.dbConnection.Open();
DC.dbCommand = new SqlCommand("SP_Employee", DC.dbConnection);
DC.dbCommand.CommandType = System.Data.CommandType.StoredProcedure;
DC.dbCommand.Parameters.AddWithValue("@Action", "SelectEmployeeList");
DC.dbReader = DC.dbCommand.ExecuteReader();
while(DC.dbReader.Read())
lst.Items.Add(DC.dbReader["Name"].ToString());
DC.dbConnection.Close();
catch(Exception ex)
return lst;
【讨论】:
以上是关于在asp.net的html表中添加下拉列表的主要内容,如果未能解决你的问题,请参考以下文章