如何将 PostBack 数据拉入动态添加的 UserControl (.NET) 中?
我的页面上有一个面板:
<asp:Panel ID="pnlTest" runat="server" />
然后我在 Page_Load 上动态添加一个文本框:
TextBox simpleTextBox = new TextBox();
pnlTest.Controls.Add(simpleTextBox);
simpleTextBox.ID = "SimpleTextBox-1";
有没有办法提取在此文本框中键入的信息,而不直接从 Request.Form 中提取? 我想在再次添加它后我可以做这样的事情:
lblPresentResults.Text = myTextBox.Text;
我知道这个例子似乎是人为的,但我想我会尝试消除我的特定应用程序中的所有其他变量,特别是在这里提出一个问题。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要在视图状态加载之前添加文本框,例如在 Page_Init 中,并且您应该能够执行此操作。
You need to add the textbox before viewstate loads, such as in Page_Init, and you should be able to do this.
只需在
Init
或PreInit
上创建文本框而不是在 Load 上,以便它在 ViewState 恢复之前存在于页面中。 然后 ASP.Net 会自动为您更新。Just create the text box on
Init
orPreInit
rather than Load, so that it exists in the page before ViewState is restored. Then ASP.Net will update it for you automatically.