简体   繁体   中英

How to pull PostBack data into a dynamically added UserControl (.NET)?

I have a Panel on my Page:

<asp:Panel ID="pnlTest" runat="server" />

Then I dynamically add a TextBox to it on Page_Load:

    TextBox simpleTextBox = new TextBox();
    pnlTest.Controls.Add(simpleTextBox);
    simpleTextBox.ID = "SimpleTextBox-1";

Is there a way to pull in the information typed in this TextBox without pulling it directly from Request.Form? I thought I could do something like this after I added it again:

lblPresentResults.Text = myTextBox.Text;

I know this example seems contrived, but I figured I'd try to eliminate all the other variables in my specific application, especially to ask a question here.

您需要在加载视图状态之前添加文本框,例如在Page_Init中,并且应该能够执行此操作。

Just create the text box on Init or PreInit rather than Load, so that it exists in the page before ViewState is restored. Then ASP.Net will update it for you automatically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM