找到当前页面控制 [英] find a control in current page
问题描述
您好,我的问题是,我似乎无法找到当前页面的控制。我的页面类有以下代码:
Hello, my problem is that I can't seem to find the control from current page. My page class has the following code:
<div class="meeting_body_actions">
<efv:ViewMeetingActions ID="ViewMeetingActions" runat="server" />
</div>
我的控制有:
My control has:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ViewMeetingActions.ascx.cs" Inherits="EFV.Controls.ViewMeetingActions" %>
<%@ Register assembly="Telerik.Web.UI" namespace="Telerik.Web.UI" tagprefix="telerik" %>
Telerik:RadListBox runat="server" CssClass="RadListBox" ID="listbox_action_member" Width="125" Height="200px" Skin="Telerik" OnTransferring="ActionListBoxViewer_Transferring" OnDeleting="ActionListBoxViewer_Deleting" >
<ButtonSettings AreaHeight="30" Position="Bottom" HorizontalAlign="Center" />
<HeaderTemplate>
</HeaderTemplate>
<Items>
</Items>
<FooterTemplate>
<asp:DropDownList runat="server" ID="action_member_dropdown" Height="22" Width="125" ></asp:DropDownList>
</FooterTemplate>
</telerik:RadListBox
从其他的控制,我需要在扔信息action_member_dropdown;
From an other control I need to throw information in the "action_member_dropdown";
Control control = this.Page.FindControl("ViewMeetingActions");
-> doesnt work
Page page = HttpContext.Current.Handler as Page;
Control ViewMeetingActions = page.FindControl("ViewMeetingActions");
-> didnt work as well
Page test = this.Parent.Page;
-> no succes
如果我问的页面有多少控件都有它说我有1控制和我增加了更多的则5。
If I ask the page how many controls I have it says I have 1 control, and I added more then 5.
因此,在短期,我怎么调用在同一页面从其他控制的控制?
So in short, how do I call a control from the same page from an other control?
推荐答案
如果一个控件嵌套其他控件里面,你需要递归找到它。
If a control is nested inside other controls, you need to find it recursively.
下面是一个helper方法。它递归搜索控制。
Here is a helper method. It searches a control recursively.
public static Control FindControlRecursive(Control root, string id)
{
if (root.ID == id)
return root;
return root.Controls.Cast<Control>()
.Select(c => FindControlRecursive(c, id))
.FirstOrDefault(c => c != null);
}
用法
Usage
var myControl = (MyControl)FindControlRecursive(Page, "ViewMeetingActions");
这篇关于找到当前页面控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!