好吧,我需要在会话变量中保存文本框中的每个输入。问题是这是动态的,这意味着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");
}
}
答案 0 :(得分:1)
这对我有用,请记住,只有在点击“添加”时才会保留这些值 - 所以我建议使用另一个保存按钮或者在不添加新项目时保留值。
public partial class TestRJF2 : System.Web.UI.Page
{
private IList<TextBox> AddedControls = new List<TextBox>();
protected override void CreateChildControls()
{
BuildControls();
base.CreateChildControls();
}
private void BuildControls()
{
for (var x = 0; x < TotalNumberAdded; x++)
{
var id = String.Format("NatureTextBox{0}", x);
//Check if control was already added
//only create controls that are new for this postback
if (AccidentPlaceHolder.FindControl(id) == null)
{
var textBox = new TextBox() {ID = id};
AccidentPlaceHolder.Controls.Add(textBox);
AddedControls.Add(textBox);
}
}
}
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);
}
protected void AccidentButton_Click(object sender, EventArgs e)
{
TotalNumberAdded++;
BuildControls();
}
protected int TotalNumberAdded
{
get { return (int)(ViewState["TotalNumberAdded"] ?? 0); }
set { ViewState["TotalNumberAdded"] = value; }
}
}