0

好的,所以我需要将文本框中的每个输入保存在会话变量中。问题是这是动态的,这意味着 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");

}

}

4

1 回答 1

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; }
    } 
}
于 2012-10-04T13:33:46.150 回答