ASP.NET访问Panel内的非服务器控件
我有一个继承自 Panel
的自定义控件。在加载时,我想访问此面板内的所有控件(包括非服务器控件)以操作属性。面板的 Controls
属性为我提供了服务器控件,但不是非服务器控件。有什么办法可以访问它们吗?
例如:
<cc:MyPanel runat="server">
<asp:TextBox id="txt1" runat="server" />
<input type="text" id="txt2" />
</cc:MyPanel>
在 Load
事件(或者渲染控件之前的任何事件)期间,我想操作两个文本框。
谢谢
I've got a custom control that inherits from Panel
. At Load-time, I want to access all of the controls inside of this panel, including non-server controls, to manipulate the attributes. The Controls
property of the panel gives me the server controls, but not the non-server ones. Is there any way to access them?
For example:
<cc:MyPanel runat="server">
<asp:TextBox id="txt1" runat="server" />
<input type="text" id="txt2" />
</cc:MyPanel>
During the Load
event (or really any event before the control is rendered), I want to manipulate both text boxes.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须在每个标记上添加
runat='server'
或执行一些 JavaScript 来处理此问题。加载事件正在查看服务器控件。
将您的控件更改为:
然后您可以执行以下操作:
我能想到的唯一其他方法是,如果您想实际使用表单来发布值,然后使用
Request.
访问您所在的每个变量发帖。但我的想法是你更多地寻找前者而不是后者。最后,如果您选择 JS 路线,这里有一篇来自 MSDN 的好文章,向您展示了如何做到这一点:
http://msdn.microsoft.com/en-us/library/3hc29e2a.aspx
You'd have to either add
runat='server'
on each tag or do some JavaScript to handle this.The load event is looking at server controls.
Change your control to:
Then you can do:
The only other way I can think of is if you wanted to actually use a form to post the values and then use
Request.
to access each variable that you are posting. But my thought is you are looking more for the former rather then the latter.Finally if you go the JS route here is a nice article from MSDN showing you how to do this:
http://msdn.microsoft.com/en-us/library/3hc29e2a.aspx
我会推荐 RegisterStartupScript 方法。我做了一个快速测试项目,虽然我不知道您的项目的具体情况,但在我的测试中成功访问和修改了面板内的客户端和服务器端控件的组合。
为我生成这个...
希望这能让您继续前进!
I would recommend the RegisterStartupScript method. I did a quick test project and while I don't know the specifics of your project, a combination of client side and server side controls inside of a panel are accessed and modified successfully in my test.
Produces this for me...
Hope that gets you going!