-1

我以前从某人那里得到了代码,并根据自己的喜好对其进行了修改。但是,我可能误解了他们的代码,但是在第三次单击按钮时,表格变得异常。我正在制作一个带有表格的动态文本框,并在每次单击“单击此处添加事故”后将其保存到会话中。

第二次点击:

第二次点击

第三次点击:

第三次点击

我已经尝试过调试它,但它为什么会这样做是没有意义的。我相信我的代码是正确的,我不确定出了什么问题。

public partial class employment_driversapplication_History : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{


}

protected int TotalNumberAdded
{
    get { return (int)(ViewState["TotalNumberAdded"] ?? 0); }
    set { ViewState["TotalNumberAdded"] = value; }


}



protected void AccidentButton_Click(object sender, EventArgs e)
{
    TotalNumberAdded++;
   // AddControls(TotalNumberAdded);
    BuildControls();


}
protected void PrevPage_Click(object sender, EventArgs e)
{

    Response.Redirect("employment_driversapplication_personalinfo.aspx");

}

private IList<TextBox> AddedControls = new List<TextBox>();
private IList<Label> AddedControlsLabel = new List<Label>();
protected override void CreateChildControls()
{
    BuildControls();
    base.CreateChildControls();
}

private void BuildControls()
{






    AccidentPlaceHolder.Controls.Add(new LiteralControl("<table><tr>"));
    AccidentPlaceHolder.Controls.Add(new LiteralControl("<br />"));

    for (var Nl = 0; Nl < TotalNumberAdded; Nl++)
    {
        var idNL = String.Format("NatureLabel{0}", Nl);


        //Check if control was already added 
        //only create controls that are new for this postback
        if (AccidentPlaceHolder.FindControl(idNL) == null)
        {
            var NLabel = new Label() { ID = idNL };
            NLabel.Text = "Nature Of Accident: ";
            AccidentPlaceHolder.Controls.Add(new LiteralControl("<td class='title-text'>"));
            AccidentPlaceHolder.Controls.Add(NLabel);

            AddedControlsLabel.Add(NLabel);

        }
    }




    for (var x = 0; x < TotalNumberAdded; x++)
    {
        var idN = String.Format("NatureTextBox{0}", x);

        //Check if control was already added 
        //only create controls that are new for this postback
        if (AccidentPlaceHolder.FindControl(idN) == null)
        {
            var NtextBox = new TextBox() { ID = idN };

            AccidentPlaceHolder.Controls.Add(new LiteralControl("</td><td class='title-text'width='180px'>"));
            AccidentPlaceHolder.Controls.Add(NtextBox);

            AddedControls.Add(NtextBox);

        }
    }

    for (var DL = 0; DL < TotalNumberAdded; DL++)
    {
        var idDL = String.Format("DateLabel{0}", DL);


        //Check if control was already added 
        //only create controls that are new for this postback
        if (AccidentPlaceHolder.FindControl(idDL) == null)
        {
            var DLabel = new Label() { ID = idDL };
            DLabel.Text = "Date: ";
            AccidentPlaceHolder.Controls.Add(new LiteralControl("</td><td class='title-text'>"));
            AccidentPlaceHolder.Controls.Add(DLabel);

            AddedControlsLabel.Add(DLabel);

        }
    }


    for (var d = 0; d < TotalNumberAdded; d++)
    {
        var idD = String.Format("DateTextBox{0}", d);
        if (AccidentPlaceHolder.FindControl(idD) == null)
        {

            var DtextBox = new TextBox() { ID = idD };

            AccidentPlaceHolder.Controls.Add(new LiteralControl("</td><td class='title-text'width='180px'>"));
            AccidentPlaceHolder.Controls.Add(DtextBox);
            AddedControls.Add(DtextBox);
            AccidentPlaceHolder.Controls.Add(new LiteralControl("</td></tr>"));

        }


    }

    for (var FL = 0; FL < TotalNumberAdded; FL++)
    {
        var idFL = String.Format("FatalLabel{0}", FL);


        //Check if control was already added 
        //only create controls that are new for this postback
        if (AccidentPlaceHolder.FindControl(idFL) == null)
        {
            var FLabel = new Label() { ID = idFL };
            FLabel.Text = "Fatalities: ";
            AccidentPlaceHolder.Controls.Add(new LiteralControl("<tr><td class='title-text'>"));
            AccidentPlaceHolder.Controls.Add(FLabel);

            AddedControlsLabel.Add(FLabel);

        }
    }
    for (var f = 0; f < TotalNumberAdded; f++)
    {
        var idF = String.Format("FatalTextBox{0}", f);
        if (AccidentPlaceHolder.FindControl(idF) == null)
        {

            var FtextBox = new TextBox() { ID = idF };
            AccidentPlaceHolder.Controls.Add(new LiteralControl("</td><td class='title-text'width='180px'>"));
            AccidentPlaceHolder.Controls.Add(FtextBox);
            AddedControls.Add(FtextBox);


        }


    }

    for (var IL = 0; IL < TotalNumberAdded; IL++)
    {
        var idIL = String.Format("InjuryLabel{0}", IL);


        //Check if control was already added 
        //only create controls that are new for this postback
        if (AccidentPlaceHolder.FindControl(idIL) == null)
        {
            var ILabel = new Label() { ID = idIL };
            ILabel.Text = "Injuries: ";
            AccidentPlaceHolder.Controls.Add(new LiteralControl("</td><td class='title-text'>"));
            AccidentPlaceHolder.Controls.Add(ILabel);

            AddedControlsLabel.Add(ILabel);

        }
    }
    for (var i = 0; i < TotalNumberAdded; i++)
    {
        var idI = String.Format("InjuryTextBox{0}", i);
        if (AccidentPlaceHolder.FindControl(idI) == null)
        {

            var ItextBox = new TextBox() { ID = idI };

            AccidentPlaceHolder.Controls.Add(new LiteralControl("</td><td class='title-text'width='180px'>"));
            AccidentPlaceHolder.Controls.Add(ItextBox);
            AddedControls.Add(ItextBox);
            AccidentPlaceHolder.Controls.Add(new LiteralControl("</td></tr>"));

        }


    }
    AccidentPlaceHolder.Controls.Add(new LiteralControl("</table>"));
}

protected override void OnPreRender(EventArgs e)
{
    foreach (var ctrl in AddedControls)
    {
        var key = ctrl.ID.Replace("TextBox", String.Empty);
        Session[key] = ctrl.Text;
    }

    foreach (string session in Session.Keys)
    {
        System.Diagnostics.Debug.WriteLine(String.Format("{0} = {1}", session, Session[session]));
    }
    base.OnPreRender(e);
}

}

任何帮助将不胜感激......我一直在寻找和调试它几个小时,但没有任何修复它。

4

1 回答 1

1

Have you tried implementing a repeater for this? It would be a lot easier that adding literal controls, as it can quickly get out of hand and makes it more difficult to debug.

One of the uses of a repeater control is to repeat an output of html for each data item. In your case you would repeat the output of the table for each accident.

For example, this would be your code on the front-end:

<asp:Repeater ID="rptAccidents" runat="server">
    <ItemTemplate>
        <table>
            <tr>
                <td class="title-text">
                    Nature Of Accident
                </td>
                <td class='title-text' width='180px'>
                    <asp:TextBox ID="txtNature" runat="server" />
                </td>
                <td class="title-text">
                    Date
                </td>
                <td class='title-text' width='180px'>
                    <asp:TextBox ID="txtDate" runat="server" />
                </td>
            </tr>
            ....NEXT ROWS
        </table>
    </ItemTemplate>
    <SeparatorTemplate>
        <br />
    </SeparatorTemplate>
</asp:Repeater>

And this would be in your code-behind:

    //HERE YOU ARE ATTACHING TO THE REPEATER'S ITEMDATABOUND EVENT
    //THIS ALLOWS YOU TO CONTROL THE OUTPUT FOR EACH ACCIDENT
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        this.rptAccidents.ItemDataBound += rptAccidents_ItemDataBound;
    }

    //THIS IS THE CODE THAT WILL RUN FOR EACH DATA ITEM IN THE REPEATER'S DATA SOURCE
    void rptAccidents_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        //in this case, you only care about the item templates
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            //cast your dataitem here
            Accident data = (Accident)data;
            //find the appropriate textbox (or any other control that is runat="server", and set the value.
            ((TextBox)e.Item.FindControl("txtNature")).Text = data.Nature;
            ((TextBox)e.Item.FindControl("txtDate")).Text = data.Date;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            this.rptAccidents.DataSource = //I AM NOT SURE WHERE YOU ARE STORING THE DATA. BUT HERE IS WHERE YOU WOULD SET THE REPEATERS DATA SOURCE
            this.rptAccidents.DataBind(); //THIS LINE CAUSES THE REPEAT TO BIND ITSELF TO YOUR DATA SOURCE
        }
    }
于 2012-10-04T16:54:39.927 回答