Can't find other Controls from my own Control



Hi there,

I'm creating a "PopUpDatePicker" control, inheriting from Hyperlink. The goal is to click on that control, select a value from a Calendar and set this date value to a target control, usually a TextBox.

Problem is on the OnPreRender event, where I try to find the target control. The FindControl() method always return null there, that is, the specified Target control is not found on the current page. Here's the event code:

protected override void OnPreRender(EventArgs e)

{

System.Web.UI.Page page = System.Web.HttpContext.Current.Handler as System.Web.UI.Page;

Control control = page.FindControl(TargetControl);

if (control != null) // null is always returned here :(

this.Attributes.Add("onclick", string.Format("window.open('DatePicker.aspx?field={0}', 'calendarPopup', 'width=230,height=209,resizable=no,statusbar=no');", control.ClientID));

base.OnPreRender(e);

}

And below is the full code-behind, too. The question is: Why is the FindControl() method returning null here? The TargetControl property does have the name of the target control on the page (I can see it with the VS debugger) so that is not the problem. So what am I missing?

Thanks in advance,

-Benton

Full code-behind below:


using System;
using System.Drawing;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.ComponentModel;

[assembly: TagPrefix("Tenerife.WebControls", "Tenerife")]

namespace Tenerife.WebControls

{

[ToolboxData("<{0}:PopUpCalendar runat=server></{0}:PopUpCalendar>"),

ToolboxBitmap(typeof(Calendar))]

public class PopUpCalendar : HyperLink

{

public PopUpCalendar()

{

this.NavigateUrl = "javascript:;";

this.ImageUrl = "~/img/SmallCalendar.gif";

this.ToolTip = "Click to select date";

}

protected override void OnPreRender(EventArgs e)

{

System.Web.UI.Page page = System.Web.HttpContext.Current.Handler as System.Web.UI.Page;

Control control = page.FindControl(TargetControl);

if (control != null) // This is always false :(

this.Attributes.Add("onclick", string.Format("window.open('DatePicker.aspx?field={0}', 'calendarPopup', 'width=230,height=209,resizable=no,statusbar=no');", control.ClientID));

base.OnPreRender(e);

}

[IDReferenceProperty(typeof(Control))]

[Themeable(false)]

[TypeConverter(typeof(System.Web.UI.WebControls.ValidatedControlConverter))]

[DefaultValue("")]

public string TargetControl

{

get { return ViewState["TargetControl"] == null ? String.Empty : ViewState["TargetControl"].ToString(); }

set { ViewState["TargetControl"] = value; }

}

}

}

.



Relevant Pages

  • Re: CSEH Unable to connect?
    ... in "Platform Settings" dialog? ... Check the middle one "Target Control" ... on other machines that I have set up elsewehere. ...
    (microsoft.public.windowsce.platbuilder)
  • Re: Moving from a form to a Nested Subform elsewhere on the form -
    ... the code below works and goes to my target control, ... instead it comes back to my departure control and ... subform linked to the first subform so sf2 always displays ...
    (microsoft.public.access.formscoding)
  • RE: Limiting Available values in Combo box on form
    ... enter the following into the rowsource of the target control, ... rowsource on the right end of it to open a query window that will allow you ...
    (microsoft.public.access.forms)
  • Re: How Can I Reference Custom Controls in the CreateUserWizard?
    ... This method takes the ID of the target control as a parameter and determines ... protected Control FindWizardControl ... Control ctrlRtn = null; ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • Re: HyperLink Bug
    ... But what is the syntax to add it into the HyperLink object? ... The URL returned by this method is relative to the folder ... containing the source file in which the control is instantiated. ...
    (microsoft.public.dotnet.framework.aspnet)

Loading