我以前从某人那里得到了代码,并根据自己的喜好对其进行了修改。但是,我可能误解了他们的代码,但是在第三次单击按钮时,表格变得异常。我正在制作一个带有表格的动态文本框,并在每次单击“单击此处添加事故”后将其保存到会话中。
第二次点击:
第三次点击:
我已经尝试过调试它,但它为什么会这样做是没有意义的。我相信我的代码是正确的,我不确定出了什么问题。
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);
}
}
任何帮助将不胜感激......我一直在寻找和调试它几个小时,但没有任何修复它。