页面生命周期和文本框

时间:2012-06-07 17:18:50

标签: c# asp.net pageload

在此输入代码当dropdownlist使用autopostback更改项目时,我会生成动态文本框。后来我想在激活提交按钮时获取文本框的文本。但是当按钮激活时,文本框会返回空白字符。问题必然是页面寿命。我应该使用哪些技术来获得这些值?

    public class GuiOtherAuthors
    {
        public OtherAuthors[] tableOtherAuthors;
        public Label[] lblName;
        public TextBox[] txtName;
        public Label[] lblSurname;
        public TextBox[] txtSurname;
        public Label[] lblInstitution;
        public TextBox[] txtInstitution;
        public Label[] lblCountry;
        public TextBox[] txtCountry;
        public Label[] lblEmail;
        public TextBox[] txtEmail;
        public PlaceHolder PlaceHolder1 = new PlaceHolder();

        public Int32 NumberOfAutherOthos { set; get; }


        public void InitializeControls()
        {
            tableOtherAuthors = new OtherAuthors[NumberOfAutherOthos];
            lblName = new Label[NumberOfAutherOthos];
            txtName = new TextBox[NumberOfAutherOthos];
            lblSurname = new Label[NumberOfAutherOthos];
            txtSurname = new TextBox[NumberOfAutherOthos];
            lblInstitution = new Label[NumberOfAutherOthos];
            txtInstitution = new TextBox[NumberOfAutherOthos];
            lblCountry = new Label[NumberOfAutherOthos];
            txtCountry = new TextBox[NumberOfAutherOthos];
            lblEmail = new Label[NumberOfAutherOthos];
            txtEmail = new TextBox[NumberOfAutherOthos];

            for (int i = 0; i < NumberOfAutherOthos; i++)
            {
                lblName[i] = new Label();
                lblName[i].Text = "Name:";
                txtName[i] = new TextBox();
                lblSurname[i] = new Label();
                lblSurname[i].Text = "Surname:";
                txtSurname[i] = new TextBox();
                lblInstitution[i] = new Label();
                lblInstitution[i].Text = "Institution:";
                txtInstitution[i] = new TextBox();
                lblCountry[i] = new Label();
                lblCountry[i].Text = "Country:";
                txtCountry[i] = new TextBox();
                lblEmail[i] = new Label();
                lblEmail[i].Text = "Email:";
                txtEmail[i] = new TextBox();
            }

        }
        public PlaceHolder makeGUI()
        {
            int i = 0;
            int j = 1;



            for (i = 0; i < NumberOfAutherOthos; i++)
            {
                Label authorInformation = new Label();
                authorInformation.Text = "Information for Author " + j.ToString() + " :";
                lblName[i].Text = "Name:";
                lblSurname[i].Text = "Surname:";
                lblInstitution[i].Text = "Institution:";
                lblCountry[i].Text = "Country:";
                lblEmail[i].Text = "Email:";

                PlaceHolder1.Controls.Add(new LiteralControl("<table>"));
                PlaceHolder1.Controls.Add(new LiteralControl("<span style=\"font-weight:bold;\" "));
                PlaceHolder1.Controls.Add(authorInformation);
                PlaceHolder1.Controls.Add(new LiteralControl("</span>"));
                PlaceHolder1.Controls.Add(new LiteralControl("<tr><td>"));
                PlaceHolder1.Controls.Add(lblName[i]);
                PlaceHolder1.Controls.Add(new LiteralControl("</td><td>"));
                PlaceHolder1.Controls.Add(txtName[i]);
                PlaceHolder1.Controls.Add(new LiteralControl("</td></tr>"));
                PlaceHolder1.Controls.Add(new LiteralControl("<tr><td>"));
                PlaceHolder1.Controls.Add(lblSurname[i]);
                PlaceHolder1.Controls.Add(new LiteralControl("</td><td>"));
                PlaceHolder1.Controls.Add(txtSurname[i]);
                PlaceHolder1.Controls.Add(new LiteralControl("</td></tr>"));
                PlaceHolder1.Controls.Add(new LiteralControl("<tr><td>"));
                PlaceHolder1.Controls.Add(lblInstitution[i]);
                PlaceHolder1.Controls.Add(new LiteralControl("</td><td>"));
                PlaceHolder1.Controls.Add(txtInstitution[i]);
                PlaceHolder1.Controls.Add(new LiteralControl("</td></tr>"));
                PlaceHolder1.Controls.Add(new LiteralControl("<tr><td>"));
                PlaceHolder1.Controls.Add(lblCountry[i]);
                PlaceHolder1.Controls.Add(new LiteralControl("</td><td>"));
                PlaceHolder1.Controls.Add(txtCountry[i]);
                PlaceHolder1.Controls.Add(new LiteralControl("</td></tr>"));
                PlaceHolder1.Controls.Add(new LiteralControl("<tr><td>"));
                PlaceHolder1.Controls.Add(lblEmail[i]);
                PlaceHolder1.Controls.Add(new LiteralControl("</td><td>"));
                PlaceHolder1.Controls.Add(txtEmail[i]);
                PlaceHolder1.Controls.Add(new LiteralControl("</td></tr>"));
                PlaceHolder1.Controls.Add(new LiteralControl("</table><br /> "));
                j++;
            }
            return PlaceHolder1;

        }
    }

The code is in c#
Thanks

 this is my page load:


    private GuiOtherAuthors gui = new GuiOtherAuthors();

    protected void Page_Load(object sender, EventArgs e)
    {

        if (IsPostBack)
        {
            numberOfOtherAuthors = NumberOfOtherAuthors();
            if (!numberOfOtherAuthors.Equals(0))
            {
                gui.NumberOfAutherOthos = numberOfOtherAuthors;
                gui.InitializeControls(); //Initialize the textboes
                Panel1.Controls.Add(gui.makeGUI()); .. create my gui 
                setValueofOtherAuthors(numberOfOtherAuthors); //create object with value of textboxes
            }
        }

    }

2 个答案:

答案 0 :(得分:0)

在生命周期的Page_Init()处理程序的早期添加控件,并在生命周期的OnPreRender处理程序的后期获取文本框值。

答案 1 :(得分:0)

这不是我的头脑,所以我可能有错误的事件,但在Page_Init(?)中你需要添加任何动态添加的控件,以便在回发周期的其余部分获取它们的值。