0

我有这段代码,它将在一行中列出所有内容。但是,我想将其格式化为: 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);
}
}
4

1 回答 1

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; }
    }
于 2012-09-21T14:39:31.750 回答