页面生命周期和文本框

本文关键字:文本 周期 生命 | 更新日期: 2023-09-27 18:36:33

在此处输入代码我在下拉列表使用自动回发更改项目时制作动态文本框。稍后我想在激活提交按钮时获取文本框的文本。但是当按钮激活时,文本框返回空白字符。问题一定是由于 页面寿命 .我应该使用什么技术来获取这些值?

    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
            }
        }
    }

页面生命周期和文本框

这超出了我的头顶,所以我可能把事件弄错了,但在 Page_Init(?) 中,您需要添加任何动态添加的控件,以便在回发周期的其余部分获取它们的值。

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