访问更新面板内动态添加的控件的值
我有一个包含更新面板的页面,在其中,我们有一个占位符,当按下 button1 时,该占位符将填充一个文本框。
现在我需要访问当用户单击 Button2 时在文本框中输入的值。我正在努力寻找解决这个问题的简单方法。
请找到下面的代码片段。
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:PlaceHolder ID="wplcHolder" runat="server"></asp:PlaceHolder>
</ContentTemplate>
</asp:UpdatePanel>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="Button" onclick="Button2_Click" />
</div>
</form>
protected void Button1_Click(object sender, EventArgs e)
{
TextBox _txt = new TextBox();
wplcHolder.Controls.Add(_txt);
}
谢谢, 钱德鲁
I have a page which contains an update panel and inside it, we have a placeHolder which will be populated with a textbox when button1 is pressed.
Now I neeed to access the value entered in the textbox when the user click Button2. I am struggling to find a simple solution to this problem.
Please find the below code snippet.
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:PlaceHolder ID="wplcHolder" runat="server"></asp:PlaceHolder>
</ContentTemplate>
</asp:UpdatePanel>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="Button" onclick="Button2_Click" />
</div>
</form>
protected void Button1_Click(object sender, EventArgs e)
{
TextBox _txt = new TextBox();
wplcHolder.Controls.Add(_txt);
}
Thanks,
Chandru
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议您在将文本框添加到占位符之前为其提供一个 ID,例如,假设您提供:
yourTextBoxID
之后,理论上您可以通过
Button2_Click
执行此操作> 事件:这可行,但前提是您在 Page_Init 中始终添加
yourTextBoxID
控件,因为如果您仅在触发新页面生命周期时从Button1_Click
执行此操作通过单击Button2
很可能文本框不会出现。当然,所有这些都有待测试和验证;-)
I suggest you to give an ID to the textbox before adding it to the placeholder, for example let's say you give:
yourTextBoxID
after that, in theory you could do this from your
Button2_Click
event:this would work but only if you add the
yourTextBoxID
control all the times in the Page_Init because if you only do it fromButton1_Click
when a new page life cycle is triggered by clicking onButton2
most likely the textbox won't be there.all to be tested and verified of course ;-)