我想使用“网格系统”将我的动态创建的控件(基于用户选择的条件创建)存放在网页上。我正在创建一个html表,即控件,然后将这些控件添加到html表行单元格中。
问题是,我希望(类似于WPF中的" span“计数)能够跨越多个单元格的一些控件,例如长字符串。
此代码:
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);...in的希望是,第一个跨单元格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);发布于 2015-04-06 19:41:25
在HTML表中,您实际上可以编写如下代码:
<table>
<tr>
<td colspan="4">
Platypus Information
</td>
<td colspan="2">
PoisonToe Information
</td>
</tr>
<tr>
<td>Poison Toe Date</td>
<td><textBox /></td>
<td>Platypus Amount</td>
<td><textBox/></td>
<td>Poison Toe Name</td>
<td><textBox /></td>
</tr>
</table>有一个属性你可以给你的细胞
var cell1 = new HtmlTableCell();
cell1.ColSpan = 4;
row.Cells.Add(cell1);ColSpan属性在服务器呈现HTML时分配属性。
https://stackoverflow.com/questions/29477886
复制相似问题