469,647 Members | 1,706 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,647 developers. It's quick & easy.

How do I pass a DateTime to a custom control ?

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

--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #1
8 1693
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.
"Alan Silver" <al*********@nospam.thanx> wrote in message
news:t4**************@nospamthankyou.spam...
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

--
Alan Silver
(anything added below this line is nothing to do with me)

Nov 19 '05 #2
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.
"Alan Silver" <al*********@nospam.thanx> wrote in message
news:t4**************@nospamthankyou.spam...
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

--
Alan Silver
(anything added below this line is nothing to do with me)



--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #3
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" <al*********@nospam.thanx> wrote in message
news:JW**************@nospamthankyou.spam...
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.
"Alan Silver" <al*********@nospam.thanx> wrote in message
news:t4**************@nospamthankyou.spam...
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

--
Alan Silver
(anything added below this line is nothing to do with me)



--
Alan Silver
(anything added below this line is nothing to do with me)

Nov 19 '05 #4
<%= 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.

Nov 19 '05 #5
>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.
"Alan Silver" <al*********@nospam.thanx> wrote in message
news:JW**************@nospamthankyou.spam...
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.
"Alan Silver" <al*********@nospam.thanx> wrote in message
news:t4**************@nospamthankyou.spam...
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

--
Alan Silver
(anything added below this line is nothing to do with me)


--
Alan Silver
(anything added below this line is nothing to do with me)



--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #6
><%= 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.

--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #7
....
<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.

Nov 19 '05 #8
><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.


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.

--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

15 posts views Thread by Fritz Switzer | last post: by
7 posts views Thread by klynn | last post: by
2 posts views Thread by Jon Vaughan | last post: by
2 posts views Thread by Ryan | last post: by
5 posts views Thread by cpnet | last post: by
5 posts views Thread by indika | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.