Can't access set property in custom control

Discussion in 'ASP .Net' started by Chris, Jun 28, 2007.

  1. Chris

    Chris Guest

    I have created a date picker user control. I want to have a boolean
    property, which I can set to determine whether the control defaults to
    today's date. I have created a property which I set to true but when I
    create the child controls it has always reset to false. I know it has been
    set to true as I have trace written the value in the property "setter". Is
    the create child controls event the wrong place to set the textbox, with the
    date. Also have trace written the value in the create child controls and it
    appears twice. Does this event fire twice? Regards, Chris.
    Chris, Jun 28, 2007
    #1
    1. Advertising

  2. Can you post the code you are using as a get/set accessor? Something a lot
    of people make a mistake about is putting code into the page_load event of a
    child control because they're used to doing it in the page. The page_load
    takes place before that of the page's page_load event. That means that if
    you set the property in the page it won't be accessible in the child
    control, you need to use a later event, such as overriding the on-prerender
    event.

    Also, keep in mind that unless you're storing the property values into the
    viewstate or another state management mechanism they values won't persist
    unless they're specifically set. For example, to set a boolean property of a
    child control into the viewstate you can use

    public bool MyProperty
    {
    get {
    if(ViewState["MyProperty"] != null)
    return (bool)ViewState["MyProperty"];
    else
    return false;
    }
    set{ ViewState["MyProperty"] = value;}
    }



    --
    Hope this helps,
    Mark Fitzpatrick
    Former Microsoft FrontPage MVP 199?-2006

    "Chris" <> wrote in message
    news:...
    >I have created a date picker user control. I want to have a boolean
    >property, which I can set to determine whether the control defaults to
    >today's date. I have created a property which I set to true but when I
    >create the child controls it has always reset to false. I know it has been
    >set to true as I have trace written the value in the property "setter". Is
    >the create child controls event the wrong place to set the textbox, with
    >the date. Also have trace written the value in the create child controls
    >and it appears twice. Does this event fire twice? Regards, Chris.
    >
    Mark Fitzpatrick, Jun 28, 2007
    #2
    1. Advertising

  3. Chris

    Chris Guest

    I don't have access to the code but I am setting the propterty declaratively
    in the design view on my aspx page (is that the same as setting it in the
    onLoad) . Should I try and access that property in the on-prerender?


    "Mark Fitzpatrick" <> wrote in message
    news:%...
    > Can you post the code you are using as a get/set accessor? Something a lot
    > of people make a mistake about is putting code into the page_load event of
    > a child control because they're used to doing it in the page. The
    > page_load takes place before that of the page's page_load event. That
    > means that if you set the property in the page it won't be accessible in
    > the child control, you need to use a later event, such as overriding the
    > on-prerender event.
    >
    > Also, keep in mind that unless you're storing the property values into the
    > viewstate or another state management mechanism they values won't persist
    > unless they're specifically set. For example, to set a boolean property of
    > a child control into the viewstate you can use
    >
    > public bool MyProperty
    > {
    > get {
    > if(ViewState["MyProperty"] != null)
    > return (bool)ViewState["MyProperty"];
    > else
    > return false;
    > }
    > set{ ViewState["MyProperty"] = value;}
    > }
    >
    >
    >
    > --
    > Hope this helps,
    > Mark Fitzpatrick
    > Former Microsoft FrontPage MVP 199?-2006
    >
    > "Chris" <> wrote in message
    > news:...
    >>I have created a date picker user control. I want to have a boolean
    >>property, which I can set to determine whether the control defaults to
    >>today's date. I have created a property which I set to true but when I
    >>create the child controls it has always reset to false. I know it has been
    >>set to true as I have trace written the value in the property "setter". Is
    >>the create child controls event the wrong place to set the textbox, with
    >>the date. Also have trace written the value in the create child controls
    >>and it appears twice. Does this event fire twice? Regards, Chris.
    >>

    >
    >
    Chris, Jun 28, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joe
    Replies:
    0
    Views:
    325
  2. Brook
    Replies:
    2
    Views:
    407
    Brook
    Dec 2, 2007
  3. Joe

    Custom Server Control Property Won't Set

    Joe, Jan 15, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    138
  4. Adam Rogas
    Replies:
    0
    Views:
    203
    Adam Rogas
    Sep 15, 2004
  5. Brook
    Replies:
    0
    Views:
    97
    Brook
    Nov 29, 2007
Loading...

Share This Page