回发后查找控件
我有一个在回发时动态创建的表;它是一个文档列表,每行都有一个标准的 html 复选框。目的是能够选择和使用一键下载多个文档。
我遇到的问题是我无法访问任何复选框,我的猜测是它们不在视图状态中,因为它们不是在 onLoad 或 onInit 时创建的。
好的,客户选择他们的文档类别和类别。单击获取文档按钮。然后访问数据库并创建包含复选框的 html 行。然后 HTML 被加载到标签之间的文字中。
更新
好的,所以我没有使用并通过后面的代码创建了行和单元格。我现在可以通过跟踪看到控制树中的复选框。
ctl00$ContentPlaceHolder1$639eec32-f1a8-4fbb-a442-d429571fc3a5 System.Web.UI.WebControls.CheckBox
这是我尝试查找复选框的内容:
public static Control FindControlRecursive(Control Root, string Id) {
if (Root.ID == Id) return Root;
foreach (Control Ctl in Root.Controls) {
Control FoundCtl = FindControlRecursive(Ctl, Id);
if (FoundCtl != null) return FoundCtl;
}
return null;
}
当 FindControlRecursive 函数运行时;页面上只看到 5 个控件;我是否已经做了足够多的工作来找到选中的复选框,还是我仍然没有朝着正确的方向前进?
提前致谢!
I have at table which is dynamically created at postback; its a list of documents with a standard html checkbox which for each row. The aim is to be able to select & download multiple documents in 1 click.
The problem I am having is I cannot access any of the checkbox's my guess is they are not in the viewstate because they were not created onLoad or onInit.
Ok so a client selects their document category & clicks the get documents button. That then hits the database and creates the html rows including the checkboxes. The HTML is then loaded into a literal sitting between the tags.
Update
Ok so I'm not using and have created the rows and cells via the code behind. I can now see the checkboxes in the Conrol Tree via Trace.
ctl00$ContentPlaceHolder1$639eec32-f1a8-4fbb-a442-d429571fc3a5 System.Web.UI.WebControls.CheckBox
This is what I've tried to find the checkboxes:
public static Control FindControlRecursive(Control Root, string Id) {
if (Root.ID == Id) return Root;
foreach (Control Ctl in Root.Controls) {
Control FoundCtl = FindControlRecursive(Ctl, Id);
if (FoundCtl != null) return FoundCtl;
}
return null;
}
When the FindControlRecursive function runs; it only sees 5 controls on the page; have I done enough to find the checked checkboxes or am I still not going in the right direction?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
确保它们是按照您提到的方式创建的。您可以使用 IE 开发工具 (F12) 或页面源并检查其 ID。然后您应该能够使用 FindControl()。您可能必须将其转换为 CheckBox 控件,以便您可以检查其是否已选中或未选中...
Make sure they are being created as you mentioned. You can either use the IE Developer tool (F12) or page source and check their ID's. Then you should be able to use FindControl(). You would probly have to cast it to a CheckBox control so you can check if its checked or unchecked...
这不是搜索控件的问题。您只需在 OnInit 阶段的每个请求中正确添加它们即可。
不要使用 Literal 控件添加 ASP.NET 服务器控件。请改用 PlaceHolder 等特殊容器。
然后,如果您知道控件的 ID,则可以简单地使用 FindControl():
It is not the issue of searching controls. You need just add them properly at every request at OnInit phase.
Don't use Literal control to add ASP.NET server controls. Use special containers like PlaceHolder instead.
Then, if you know the Id of control you can simply use FindControl():