Professional Web Applications Themes

Can't find other Controls from my own Control - ASP.NET Building Controls

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 ...

  1. #1

    Default 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.Val idatedControlConverter))]

    [DefaultValue("")]

    public string TargetControl

    {

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

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

    }

    }

    }

    Benton Guest

  2. #2

    Default Re: Can't find other Controls from my own Control

    > 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.
    Adding something to my own question here. I've modified my OnPreRender event
    to look like this:

    protected override void OnPreRender(EventArgs e)
    {

    Control control = Page.FindControl(TargetControl);


    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);

    }

    This time I am not casting the current page from the current handler, I'm
    just using the Page property the Hyperlink control inherited. However, the
    result is just the same: FindControl() always returns null. What am I doing
    wrong?

    Thanks in advance,

    -Benton

    Benton Guest

  3. #3

    Default Re: Can't find other Controls from my own Control

    >> 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.
    >
    > This time I am not casting the current page from the current handler, I'm
    > just using the Page property the Hyperlink control inherited. However, the
    > result is just the same: FindControl() always returns null.
    Duh. Solved by using NamingContainer.FindControl() instead of
    Page.FindControl(). Long live GoogleGroups. :)

    Cheers,

    -Benton



    Benton Guest

  4. #4

    Default Re: Can't find other Controls from my own Control

    Benton wrote: 
    >>
    >> This time I am not casting the current page from the current handler,
    >> I'm just using the Page property the Hyperlink control inherited.
    >> However, the result is just the same: FindControl() always returns null.[/ref]
    >
    > Duh. Solved by using NamingContainer.FindControl() instead of
    > Page.FindControl(). Long live GoogleGroups. :)
    >
    > Cheers,
    >
    > -Benton
    >
    >
    >[/ref]
    Benton, I am browsing this old question. Do you understand why you
    needed to use the NamingContainer.

    I thought of asking you whether your textbox is in a composite control
    along with the Calendar control. I would then understand why you can't
    see it via the Page if your control does not implement NamingContainer;
    only visible controls are those under the form directly.

    Please let me know
    Thanks
    intrader Guest

Similar Threads

  1. Assembly with USER controls (ascx) cannot find its resouces
    By mschep@deloitte.nl in forum ASP.NET Building Controls
    Replies: 0
    Last Post: March 14th, 06:24 AM
  2. Control.Controls bug? Control's child controls missing at the run time.
    By sinelnikov.andrei@gmail.com in forum ASP.NET Building Controls
    Replies: 1
    Last Post: December 8th, 06:47 AM
  3. using javascript in User controls to access server controls of the user control
    By Faizan Ahmed in forum ASP.NET Building Controls
    Replies: 1
    Last Post: January 4th, 04:09 PM
  4. controls getting values from another control---within a control
    By DC Gringo in forum ASP.NET Building Controls
    Replies: 1
    Last Post: September 16th, 07:59 AM
  5. Replies: 2
    Last Post: August 1st, 10:04 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139