我想使用“网格系统”在网页上容纳我动态创建的控件(有条件地创建,基于用户选择)。我正在创建一个 html 表格、控件,然后将控件添加到 html 表格行单元格中。
问题是我希望(类似于 WPF 中的“跨度”计数)能够跨越多个单元格的某些控件,例如长字符串。
这段代码:
protected override void CreateChildControls()
{
base.CreateChildControls();
LiteralControl message = new LiteralControl();
message.Text = DisplayMessage;
Controls.Add(message);
int selectionMadeOnEditor = 0; // TODO: Make this dynamic; this is just for testing
HtmlTable tbl = null;
switch (selectionMadeOnEditor)
{
case 0:
tbl = GeneratePlatypoisons();
break;
case 1:
//Console.WriteLine(5);
break;
default:
tbl = GenerateMorayEelLikeElectricity();
break;
}
this.Controls.Add(tbl);
. . .
private HtmlTable GeneratePlatypoisons()
{
HtmlTable dynamicTable = new HtmlTable();
// Create Row 1
var row = new HtmlTableRow();
var cell1 = new HtmlTableCell();
var cell2 = new HtmlTableCell();
var cell3 = new HtmlTableCell();
var cell4 = new HtmlTableCell();
var cell5 = new HtmlTableCell();
var cell6 = new HtmlTableCell();
row.Cells.Add(cell1);
row.Cells.Add(cell2);
row.Cells.Add(cell3);
row.Cells.Add(cell4);
row.Cells.Add(cell5);
row.Cells.Add(cell6);
dynamicTable.Rows.Add(row);
// Populate row 1
LiteralControl section1Hdr = new LiteralControl("<h2>Section 1: Platypus Information</h2>");
LiteralControl section2Hdr = new LiteralControl("<h2>Section 2: PoisonToe Information</h2>");
cell1.Controls.Add(section1Hdr);
cell5.Controls.Add(section2Hdr);
// Create Row 2
var row2 = new HtmlTableRow();
var cell2_1 = new HtmlTableCell();
var cell2_2 = new HtmlTableCell();
var cell2_3 = new HtmlTableCell();
var cell2_4 = new HtmlTableCell();
var cell2_5 = new HtmlTableCell();
var cell2_6 = new HtmlTableCell();
row2.Cells.Add(cell2_1);
row2.Cells.Add(cell2_2);
row2.Cells.Add(cell2_3);
row2.Cells.Add(cell2_4);
row2.Cells.Add(cell2_5);
row2.Cells.Add(cell2_6);
dynamicTable.Rows.Add(row2);
// Populate Row 2
LiteralControl reqDateStr = new LiteralControl("PoisonToe Date:");
cell2_1.Controls.Add(reqDateStr);
boxRequestDate = new TextBox();
boxRequestDate.Text = DateTime.Today.ToShortDateString();
cell2_2.Controls.Add(boxRequestDate);
LiteralControl payAmtStr = new LiteralControl("Platypus Amount:");
cell2_3.Controls.Add(payAmtStr);
boxPaymentAmount = new TextBox();
cell2_4.Controls.Add(boxPaymentAmount);
LiteralControl reqNameStr = new LiteralControl("PoisonToe Name:");
cell2_5.Controls.Add(reqNameStr);
boxRequestorName = new TextBox();
cell2_6.Controls.Add(boxRequestorName);
// Populate row 3
// Populate row 4
// Populate row 5
// Populate row 6
// Populate row 7
return dynamicTable;
}
我将文字控件/字符串添加到 cell1 和 cell5:
cell1.Controls.Add(section1Hdr);
cell5.Controls.Add(section2Hdr);
...希望第一个跨越单元格 1 到 4,第二个跨越单元格 5 和 6,但不行,如您在此处看到的:
我需要做些什么才能让这些琴弦水平伸展,而不是把自己塞进痉挛状态,导致我发脾气?
更新
使用 Malachi 接受的答案,我能够简化我的第 1 行代码(第 2 行保持不变):
// Create Row 1
var row = new HtmlTableRow();
var cell1 = new HtmlTableCell();
var cell2 = new HtmlTableCell();
row.Cells.Add(cell1);
row.Cells.Add(cell2);
dynamicTable.Rows.Add(row);
// Populate row 1
LiteralControl section1Hdr = new LiteralControl("<h2>Section 1: Platypus Information</h2>");
LiteralControl section2Hdr = new LiteralControl("<h2>Section 2: PoisonToe Information</h2>");
cell1.ColSpan = 4;
cell2.ColSpan = 2;
cell1.Controls.Add(section1Hdr);
cell2.Controls.Add(section2Hdr);