好的,所以我需要将文本框中的每个输入保存在会话变量中。问题是这是动态的,这意味着 texbox ID 是 NatureTextbox_1、NatureTexbox_2 等。由于可用的 texbox 数量无限,因此很难保存每个会话变量。我一直把头撞在墙上,试图弄清楚这一点,我想成为一个菜鸟,向你们征求意见。如果您能给我任何有关该做什么的信息,我将不胜感激。
这是 C# 代码(记住文本框是动态的,意味着无限):
protected void Page_Load(object sender, EventArgs e)
{
// Add any controls that have been previously added dynamically
for (int i = 0; i < TotalNumberAdded; ++i)
{
AddControls(i + 1);
}
}
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();
//Validations
var dateRegex = new RegularExpressionValidator();
//*****CURRENT IDEA THAT ISNT WORKING***********************************
Session["Nature" + (TotalNumberAdded - 1)] = natureTextbox.Text.ToString();
Session["Date" + (TotalNumberAdded - 1)] = dateTextbox.Text.ToString();
Session["Fatal" + (TotalNumberAdded - 1)] = fatalTextbox.Text.ToString();
Session["injury" + (controlNumber - 1)] = injurTextbox.Text.ToString();
//**********************************************************************
// 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: ";
dateRegex.ID = "DateRegex_" + controlNumber;
dateRegex.Text = "Please enter in format MM/DD/YYY";
dateRegex.ValidationExpression = @"^(((0?[1-9]|1[012])/(0?[1-9]|1\d|2[0-8])|(0?[13456789]|1[012])/(29|30)|(0?[13578]|1[02])/31)/(19|[2-9]\d)\d{2}|0?2/29/((19|[2-9]\d)(0[48]|[2468][048]|[13579][26])|(([2468][048]|[3579][26])00)))$";
dateRegex.ControlToValidate = dateTextbox.ID;
// 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("<br />"));
newPanel.Controls.Add(dateRegex);
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><br /><hr />"));
AccidentPlaceHolder.Controls.Add(newPanel);
}
protected int TotalNumberAdded
{
get { return (int)(ViewState["TotalNumberAdded"] ?? 0); }
set { ViewState["TotalNumberAdded"] = value; }
}
protected void AccidentButton_Click(object sender, EventArgs e)
{
TotalNumberAdded++;
AddControls(TotalNumberAdded);
}
protected void PrevPage_Click(object sender, EventArgs e)
{
Response.Redirect("employment_driversapplication_personalinfo.aspx");
}
}