473,386 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 1845
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Todd Schinell | last post by:
Back in July, Jeffery Tan posted this: http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=OWOTdf0VDHA.2296%40cpmsftngxa06.phx.gbl In response as to how to get click events from a...
15
by: Fritz Switzer | last post by:
I'd like to have a string assigned the value of a DateTime.AddMinutes(amount) so that the string is formatted in "HH:MM" format. For example: DateTime.Now.AddMinutes(30) returns "00:30" ...
7
by: klynn | last post by:
I'm wondering what the best way to do the following: I have a page with a table. I built a custom class that creates a System.Web.UI.WebControls table object and return it to the webpage as a...
2
by: Glenn | last post by:
Hello Is it possible to pass arguements to a .net service once it is in a running state. If this is not possible , are they alternative ways in which to achive the same thing? Glenn
2
by: Jon Vaughan | last post by:
Hi, I was looking for a control that you enter time values into , I started off with a textbox, but then saw the datetime picker had a custom format for Time , so I entered HH:mm. The conctrol...
2
by: Ryan | last post by:
My form includes a field of the DateTime type. I want to allow the user to be able to set both the date and the time. By default the DateTimePicker shows just the date and calendar, the time is...
5
by: cpnet | last post by:
I'm able to pass string parameters with no problems to a LocalReport being displayed by the ASP.NET ReportViewer control. However, my report also has a DateTime parameter. ...
3
by: gregcm | last post by:
I've got a custom control that is trying to allow the user to enter a DateTime in a definable format and I was wondering if there was an easy way to alter the look of said control based on the...
5
by: indika | last post by:
Hi, I'm a newbie to python but have some experience in programming. I came across this requirement of using datetime.date objects associated with some another object. eg. a dictionary containing...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.