How do I pass a DateTime to a custom control ?

A

Alan Silver

Hello,

I have a custom control that displays the date and time. I currently
have properties for the day, month, year, hour and minute, which have to
be set separately. This is inefficient.

I would like to have one property that I can use to pass in a DateTime,
and have the control pull out the various bits from that. I tried ...

public DateTime DateTimeDT {
get {
return new DateTime(m_Year, m_Month, m_Day, m_Hour, m_Minute, 0);
}
set {
m_Year = value.Year;
m_Month = value.Month;
m_Day = value.Day;
m_Hour = value.Hour;
m_Minute = value.Minute;
}
}

but when I try and use this in code ...

DateTimeDT="<%=DateTime.Now()%>"

I get the error "<%=DateTime.Now()%> is not a valid value for DateTime".

Please could someone enlighten me as to a) what I'm doing wrong and b)
the correct/best/easiest way to do this? TIA
 
J

Jon Sagara

Try this:

public DateTime DateTimeDT {
get {
return m_dtDateTime;
}
set {
m_dtDateTime = value;
}
}

where m_dtDateTime is a private or protected instance variable of type
DateTime.
 
A

Alan Silver

Jon,

Thanks for the reply, but either I didn't make myself clear or you
misread the question ;-)

I have the property set/get code working fine, my problem is how to use
this in the calling page. I would like to be able to do something like
....

<MyControls:DateTimePicker ID="dtpDTP" DateTimeDT="<%=DateTime.Now()%>"
Runat="Server" />

but this gives the error "<%=DateTime.Now()%> is not a valid value for
DateTime". I tried adding ToString() on to that as well, but that gave
the same error.

Your code did the same. I think the problem is in the way I'm passing
the value to the control. I can set the property fine in code ...

dtpDTP.DateTimeDT = dateTime.Now;

I just can't set it in the tag.

Any ideas? Thanks again.
Try this:

public DateTime DateTimeDT {
get {
return m_dtDateTime;
}
set {
m_dtDateTime = value;
}
}

where m_dtDateTime is a private or protected instance variable of type
DateTime.
 
J

Jon Sagara

OIC

Well, you're effectively initializing DateTimeDT with DateTime.Now (), so
why not do that in the code rather than in the tag? Are you always going to
be using DateTime.Now ()?

- Jon


Alan Silver said:
Jon,

Thanks for the reply, but either I didn't make myself clear or you misread
the question ;-)

I have the property set/get code working fine, my problem is how to use
this in the calling page. I would like to be able to do something like ...

<MyControls:DateTimePicker ID="dtpDTP" DateTimeDT="<%=DateTime.Now()%>"
Runat="Server" />

but this gives the error "<%=DateTime.Now()%> is not a valid value for
DateTime". I tried adding ToString() on to that as well, but that gave the
same error.

Your code did the same. I think the problem is in the way I'm passing the
value to the control. I can set the property fine in code ...

dtpDTP.DateTimeDT = dateTime.Now;

I just can't set it in the tag.

Any ideas? Thanks again.
 
W

Wilco Bauwer

<%= is equal to Response.Write. So you're essentially doing a
Response.Write(DateTime.Now()) with that code.

I suggest you set the DateTimeDT property within your codebehind
instead, or you could use databinding syntax, and databind your control.
 
A

Alan Silver

Well, you're effectively initializing DateTimeDT with DateTime.Now (), so
why not do that in the code rather than in the tag? Are you always going to
be using DateTime.Now ()?

No, that was just an example for simplicity. Usually the datetime would
be set to a value from a database.
 
A

Alan Silver

<%= is equal to Response.Write. So you're essentially doing a
Response.Write(DateTime.Now()) with that code.

I suggest you set the DateTimeDT property within your codebehind
instead, or you could use databinding syntax, and databind your control.

One of the main things I want to be able to do with this is use
databinding. Say I have this control in a DataList Itemtemplate, I want
to be able to pass in a datetime value from the data source.

That's really where my question lies. The fact of being able to set the
datetime in the tag was more a simple example of the problem. If you
could show me how to do this with data binding, I would be very
grateful.

Thanks for the reply.
 
W

Wilco Bauwer

....
<ItemTemplate>
My date: <%# DateTime.Now %> .. and a property from the data source:
<%# ((DataRowView)Container.DataItem)["SomeDate"] %>
</ItemTemplate>

Container.DataItem is of the type object, which represents ONE item in
the data source. You can cast it to its real type and access its
properties/etc.
 
A

Alan Silver

My date: <%# DateTime.Now %> .. and a property from the data source:
<%# ((DataRowView)Container.DataItem)["SomeDate"] %>
</ItemTemplate>

Container.DataItem is of the type object, which represents ONE item in
the data source. You can cast it to its real type and access its
properties/etc.

I tried casting it directly to DateTime, but that didn't work. You seem
to be casting the DataItem to a DataRowView and passing that in. Don't
you need to cast that to a DateTime first?

I actually found a way around it by creating a small utility function
....

DateTime MakeDT(object objDt) {
return (DateTime)objDt;
}

which did the trick. I then did the data binding like this ...

<%# MakeDT(DataBinder.Eval(Container.DataItem, "Eventdate"))%>

Obviously this is for internal use only, and would only be used when I
knew the object being passed in contained a valid DateTime, that's why I
didn't use any error checking. Your way is certainly neater as it avoids
the auxiliary function.

Thanks for the reply.
 

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,771
Messages
2,569,587
Members
45,098
Latest member
KetoBaseReview
Top