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

纵饮孤独 提交于 2019-12-11 16:28:49

问题


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.


回答1:


You need to add the textbox before viewstate loads, such as in Page_Init, and you should be able to do this.




回答2:


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.



来源:https://stackoverflow.com/questions/722570/how-to-pull-postback-data-into-a-dynamically-added-usercontrol-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!