我遇到的问题是动态生成的Button控件只出现在最后一行。我看过这篇文章
但它建议使用ListView,我不认为我可以在这种情况下使用。我使用Panel控件和SQL数据生成一个HTML表格,在我看来它非常具体。 这是我的代码背后:
//declare a dynamic button
Button btn_continue = new Button();
btn_continue.Click += Btn_continue_Click;
btn_continue.CssClass = "controlButtons";
btn_continue.Font.Size = 20;
btn_continue.Text = "Continue this job";
//formatting for the rows
int oddNumber = 1;
foreach (DataRow row in dtGetIncompleteJobs.Rows)
{
//grab the date and make it shorter
DateTime date = Convert.ToDateTime(row[3].ToString());
pnl_jobList.Controls.Add(new LiteralControl("<tr>"));
if (centralFunctions.OddNumberCheck(oddNumber) == true)
{
pnl_jobList.Controls.Add(new LiteralControl("<td class='col-1 col-1-m lightRow'>" + row[5].ToString() + "</td>"));
pnl_jobList.Controls.Add(new LiteralControl("<td class='col-2 col-2-m lightRow'>" + date.ToShortDateString() + "</td>"));
pnl_jobList.Controls.Add(new LiteralControl("<td class='col-2 col-2-m lightRow'>" + row[0].ToString() + "</td>"));
pnl_jobList.Controls.Add(new LiteralControl("<td class='col-2 col-2-m lightRow'>" + row[1].ToString() + "</td>"));
pnl_jobList.Controls.Add(new LiteralControl("<td class='col-2 col-2-m lightRow'>" + row[2].ToString() + "</td>"));
pnl_jobList.Controls.Add(new LiteralControl("<td class='col-1 col-1-m lightRow'>" + row[4].ToString() + "</td>"));
pnl_jobList.Controls.Add(new LiteralControl("<td class='col-2 col-2-m'>"));
btn_continue.ID = row[5].ToString();
pnl_jobList.Controls.Add(btn_continue);
pnl_jobList.Controls.Add(new LiteralControl("</td>"));
oddNumber++;
}
else
{
pnl_jobList.Controls.Add(new LiteralControl("<td class='col-1 col-1-m darkRow'>" + row[5].ToString() + "</td>"));
pnl_jobList.Controls.Add(new LiteralControl("<td class='col-2 col-2-m darkRow'>" + row[3].ToString() + "</td>"));
pnl_jobList.Controls.Add(new LiteralControl("<td class='col-2 col-2-m darkRow'>" + row[0].ToString() + "</td>"));
pnl_jobList.Controls.Add(new LiteralControl("<td class='col-2 col-2-m darkRow'>" + row[2].ToString() + "</td>"));
pnl_jobList.Controls.Add(new LiteralControl("<td class='col-2 col-2-m darkRow'>" + row[2].ToString() + "</td>"));
pnl_jobList.Controls.Add(new LiteralControl("<td class='col-1 col-1-m darkRow'>" + row[4].ToString() + "</td>"));
pnl_jobList.Controls.Add(new LiteralControl("<td class='col-2 col-2-m'>"));
btn_continue.ID = row[5].ToString();
pnl_jobList.Controls.Add(btn_continue);
pnl_jobList.Controls.Add(new LiteralControl("</td>"));
oddNumber++;
}
pnl_jobList.Controls.Add(new LiteralControl("</tr>"));
}
这就是它的样子: html output
是否有可能按照我正在进行的方式对每行进行Button控制?我有一种感觉,我要么缺少一些简单的东西,要么走错了路。 如果我遗漏任何细节,请告诉我。谢谢您的帮助。
答案 0 :(得分:0)
看起来您需要将btn_continue的创建代码移动到foreach循环
//formatting for the rows
int oddNumber = 1;
//declate the button
Button btn_continue
foreach (DataRow row in dtGetIncompleteJobs.Rows)
{
//grab the date and make it shorter
DateTime date = Convert.ToDateTime(row[3].ToString());
//create the button
btn_continue = new Button();
btn_continue.Click += Btn_continue_Click;
btn_continue.CssClass = "controlButtons";
btn_continue.Font.Size = 20;
btn_continue.Text = "Continue this job";
pnl_jobList.Controls.Add(new LiteralControl("<tr>"));
if (centralFunctions.OddNumberCheck(oddNumber) == true)
{
pnl_jobList.Controls.Add(new LiteralControl("<td class='col-1 col-1-m lightRow'>" + row[5].ToString() + "</td>"));
pnl_jobList.Controls.Add(new LiteralControl("<td class='col-2 col-2-m lightRow'>" + date.ToShortDateString() + "</td>"));
pnl_jobList.Controls.Add(new LiteralControl("<td class='col-2 col-2-m lightRow'>" + row[0].ToString() + "</td>"));
pnl_jobList.Controls.Add(new LiteralControl("<td class='col-2 col-2-m lightRow'>" + row[1].ToString() + "</td>"));
pnl_jobList.Controls.Add(new LiteralControl("<td class='col-2 col-2-m lightRow'>" + row[2].ToString() + "</td>"));
pnl_jobList.Controls.Add(new LiteralControl("<td class='col-1 col-1-m lightRow'>" + row[4].ToString() + "</td>"));
pnl_jobList.Controls.Add(new LiteralControl("<td class='col-2 col-2-m'>"));
btn_continue.ID = row[5].ToString();
pnl_jobList.Controls.Add(btn_continue);
pnl_jobList.Controls.Add(new LiteralControl("</td>"));
oddNumber++;
}
else
{
pnl_jobList.Controls.Add(new LiteralControl("<td class='col-1 col-1-m darkRow'>" + row[5].ToString() + "</td>"));
pnl_jobList.Controls.Add(new LiteralControl("<td class='col-2 col-2-m darkRow'>" + row[3].ToString() + "</td>"));
pnl_jobList.Controls.Add(new LiteralControl("<td class='col-2 col-2-m darkRow'>" + row[0].ToString() + "</td>"));
pnl_jobList.Controls.Add(new LiteralControl("<td class='col-2 col-2-m darkRow'>" + row[2].ToString() + "</td>"));
pnl_jobList.Controls.Add(new LiteralControl("<td class='col-2 col-2-m darkRow'>" + row[2].ToString() + "</td>"));
pnl_jobList.Controls.Add(new LiteralControl("<td class='col-1 col-1-m darkRow'>" + row[4].ToString() + "</td>"));
pnl_jobList.Controls.Add(new LiteralControl("<td class='col-2 col-2-m'>"));
btn_continue.ID = row[5].ToString();
pnl_jobList.Controls.Add(btn_continue);
pnl_jobList.Controls.Add(new LiteralControl("</td>"));
oddNumber++;
}
pnl_jobList.Controls.Add(new LiteralControl("</tr>"));
}