我有这个代码,它会在一行中列出来。但是,我想格式化它:A。按钮位于texbox的底部,标签为B.每个标签和texbox组之间有一个表格单元格。
现在它是如何出现的:
这就是我需要的:
public partial class Testing : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Add any controls that have been previously added dynamically
Response.Write("<tr><td class='table-text'>");
for (int i = 0; i < TotalNumberAdded; ++i)
{
AddControls(i + 1);
}
Response.Write("</td></tr>");
// Attach the event handler to the button
//Button1.Click += new EventHandler(Button1_Click);
}
protected void Button1_Click(object sender, EventArgs e)
{
// Increase the number added and add the new label and textbox
TotalNumberAdded++;
AddControls(TotalNumberAdded);
}
private void AddControls(int controlNumber)
{
var newPanel = new Panel();
var natureLabel = new Label();
var dateLabel = new Label();
var fatalLabel = new Label();
var injurLabel = new Label();
var natureTextbox = new TextBox();
var dateTextbox = new TextBox();
var fatalTextbox = new TextBox();
var injurTextbox = new TextBox();
// textbox needs a unique id to maintain state information
natureTextbox.ID = "NatureTextBox_" + controlNumber;
dateTextbox.ID = "DateTextbox_" + controlNumber;
fatalTextbox.ID = "fatalTextbox_" + controlNumber;
injurTextbox.ID = "injurTextbox_" + controlNumber;
natureLabel.Text = "Nature Of Accident";
dateLabel.Text = "Date:";
atalLabel.Text = "Fatalities:";
njurLabel.Text = "Injuries:";
// add the label and textbox to the panel, then add the panel to the form
newPanel.Controls.Add(natureLabel);
newPanel.Controls.Add(natureTextbox);
newPanel.Controls.Add(dateLabel);
newPanel.Controls.Add(dateTextbox);
newPanel.Controls.Add(fatalLabel);
newPanel.Controls.Add(fatalTextbox);
newPanel.Controls.Add(injurLabel);
newPanel.Controls.Add(injurTextbox);
form1.Controls.Add(newPanel);
}
protected int TotalNumberAdded
{
get { return (int)(ViewState["TotalNumberAdded"] ?? 0); }
set { ViewState["TotalNumberAdded"] = value; }
}
}
我在这里发现了一些类似于我所做的其他代码,但我似乎无法将这两个代码与面板结合起来:
protected void Button2_Click(object sender, EventArgs e)
{
TableRow row;
TableCell cell;
for (int i = 0; i < 3; ++i)
{
TextBox txt = new TextBox();
txt.Text = i.ToString();
row = new TableRow();
cell = new TableCell();
cell.Controls.Add(txt);
row.Controls.Add(cell);
Table1.Controls.Add(row);
}
}
答案 0 :(得分:0)
我自己想出来的是正确的代码(添加文字来做HTML):
private void AddControls(int controlNumber)
{
var newPanel = new Panel();
var natureLabel = new Label();
var dateLabel = new Label();
var fatalLabel = new Label();
var injurLabel = new Label();
var natureTextbox = new TextBox();
var dateTextbox = new TextBox();
var fatalTextbox = new TextBox();
var injurTextbox = new TextBox();
// textbox needs a unique id to maintain state information
natureTextbox.ID = "NatureTextBox_" + controlNumber;
dateTextbox.ID = "DateTextbox_" + controlNumber;
fatalTextbox.ID = "fatalTextbox_" + controlNumber;
injurTextbox.ID = "injurTextbox_" + controlNumber;
natureLabel.Text = "Nature Of Accident: ";
dateLabel.Text = "Date: ";
fatalLabel.Text = "Fatalities: ";
injurLabel.Text = "Injuries: ";
// add the label and textbox to the panel, then add the panel to the form
newPanel.Controls.Add(new LiteralControl("<table><tr>"));
newPanel.Controls.Add(new LiteralControl("<br />"));
newPanel.Controls.Add(new LiteralControl("<td class='title-text' >"));
newPanel.Controls.Add(natureLabel);
newPanel.Controls.Add(new LiteralControl("</td><td class='title-text'width='180px'>"));
newPanel.Controls.Add(natureTextbox);
newPanel.Controls.Add(new LiteralControl("</td><td class='title-text' >"));
newPanel.Controls.Add(dateLabel);
newPanel.Controls.Add(new LiteralControl("</td><td class='title-text'>"));
newPanel.Controls.Add(dateTextbox);
newPanel.Controls.Add(new LiteralControl("</td></tr>"));
newPanel.Controls.Add(new LiteralControl("<tr><td class='title-text'>"));
newPanel.Controls.Add(fatalLabel);
newPanel.Controls.Add(new LiteralControl("</td><td class='title-text'>"));
newPanel.Controls.Add(fatalTextbox);
newPanel.Controls.Add(new LiteralControl("</td><td class='title-text'>"));
newPanel.Controls.Add(injurLabel);
newPanel.Controls.Add(new LiteralControl("</td><td class='title-text'>"));
newPanel.Controls.Add(injurTextbox);
newPanel.Controls.Add(new LiteralControl("</td></tr></table>"));
form1.Controls.Add(newPanel);
}
protected int TotalNumberAdded
{
get { return (int)(ViewState["TotalNumberAdded"] ?? 0); }
set { ViewState["TotalNumberAdded"] = value; }
}