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
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
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.
Here is a helper method. It searches a control recursively.
Helper Method
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");
First, recursively loop through the controls on the page. Using the following helper class:
using System.Web.UI;
public class ReflectionHelper
{
/// <summary>
/// Check Control for match on ID and recursively check all Children for match on ID.
/// </summary>
/// <param name="ParentControl"></param>
/// <param name="ControlId"></param>
/// <returns>Control if found, null if not found</returns>
/// /// <remarks>Jason Williams | 9/7/2014 | webprogrammerguy.com</remarks>
public static Control FindControlRecursive(Control ParentControl, string ControlId)
{
if (ParentControl.ID == ControlId) {
return ParentControl;
}
foreach (Control Ctl in ParentControl.Controls) {
Control FoundCtl = FindControlRecursive(Ctl, ControlId);
if ((FoundCtl != null)) {
return FoundCtl;
}
}
return null;
}
/// <summary>
/// Check Control for match on ID and recursively check all Children for match on ID. Attempt to Invoke() Control method.
/// </summary>
/// <param name="ParentControl"></param>
/// <param name="ControlId"></param>
/// <param name="MethodName"></param>
/// <param name="parameters"></param>
/// <returns>bool true if executed, bool false if error or not executed</returns>
/// /// <remarks>Jason Williams | 9/7/2014 | webprogrammerguy.com</remarks>
public static bool FindControlRecursiveAndInvokeMethod(Control ParentControl, string ControlId, string MethodName, object[] parameters)
{
var ctrl = FindControlRecursive(ParentControl, ControlId);
if (ctrl != null)
{
try
{
MethodInfo[] ctrlMethods = ctrl.GetType().GetMethods();
foreach (MethodInfo method in ctrlMethods)
{
if (method.Name == MethodName)
{
method.Invoke(ctrl, parameters);
return true;
}
}
//return false;
}
catch (System.Exception)
{
//return false;
}
}
else
{
//return false;
}
return false;
}
/// <summary>
/// Check Control for match on ID and recursively check all Children for match on ID. Attempt to set SetValue() on Control property.
/// </summary>
/// <param name="ParentControl"></param>
/// <param name="ControlId"></param>
/// <param name="PropertyName"></param>
/// <param name="value"></param>
/// <returns>bool true if executed, bool false if error or not executed</returns>
/// /// <remarks>Jason Williams | 9/7/2014 | webprogrammerguy.com</remarks>
public static bool FindControlRecursiveAndSetPropertyValue(Control ParentControl, string ControlId, string PropertyName, string value)
{
var ctrl = FindControlRecursive(ParentControl, ControlId);
if (ctrl != null)
{
try
{
PropertyInfo[] ctrlProperties = ctrl.GetType().GetProperties();
foreach (PropertyInfo property in ctrlProperties)
{
if (property.Name == PropertyName)
{
property.SetValue(ctrl, value, new object[0]);
return true;
}
}
//return false;
}
catch (System.Exception)
{
//return false;
}
}
else
{
//return false;
}
return false;
}
/// <summary>
/// Check Control for match on ID and recursively check all Children for match on ID. Attempt to set SetValue() on Control property.
/// </summary>
/// <param name="ParentControl"></param>
/// <param name="ControlId"></param>
/// <param name="PropertyName"></param>
/// <param name="value"></param>
/// <returns>bool true if executed, bool false if error or not executed</returns>
/// <remarks>Jason Williams | 9/7/2014 | webprogrammerguy.com</remarks>
public static bool FindControlRecursiveAndSetPropertyValue(Control ParentControl, string ControlId, string PropertyName, int value)
{
var ctrl = FindControlRecursive(ParentControl, ControlId);
if (ctrl != null)
{
try
{
PropertyInfo[] ctrlProperties = ctrl.GetType().GetProperties();
foreach (PropertyInfo property in ctrlProperties)
{
if (property.Name == PropertyName)
{
property.SetValue(ctrl, value, new object[0]);
return true;
}
}
//return false;
}
catch (System.Exception)
{
//return false;
}
}
else
{
//return false;
}
return false;
}
}
Second, use the class to get the Control Ref:
Control ctrlActionMemberDropdown = ReflectionHelper.FindControlRecursive(this.Page, "action_member_dropdown");
Third, insert the row into the DropDownList Control:
ctrlActionMemberDropdown.Items.Insert(0, "<-- Select -->");
Thanks,
Page.Controls gives you just the collection of the topmost controls in the control hierarchy. The WebForm is itself a control, and contains many other controls. You will need to walk this hierarchy to see the entire collection of controls.
The FindControl method should find the control you are looking for. Can you share more code with us to demonstrate the problem?
来源:https://stackoverflow.com/questions/16562322/find-a-control-in-current-page