Can't access set property in custom control

C

Chris

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

Mark Fitzpatrick

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;}
}
 
C

Chris

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top