Setting SelectedDate in <asp:calendar

T

TenDot

I'm trying to set the SelectedDate on an <asp:calendar tag to show the
existing value of a date in a field retrieved from a database and put into
edit mode in a datagrid.

I've already got a TextBox tag in this datagrid cell that gets assigned (and
works fine):
<asp:TextBox id=INJECTIONDATE runat="server" Width="94px" Text='<%#
DataBinder.Eval(Container, "DataItem.INJECTIONDATE") %>'/>


It would seem like a no-brainer to do the following in the calendar tag that
wants to show the date preselected in the calendar component:

<asp:Calendar id="calINJECTIONDATE" runat="server" SelectedDate='<%#
DataBinder.Eval(Container, "DataItem.INJECTIONDATE" ) %>'>

But, as always seems the case with no-brainer solutions in .asp (sorry), no
dice.

I've done some other hoop jumping like calling a function to cast the result
as a DateTime:
GetDateValue(DataBinder.Eval(Container, "DataItem.INJECTIONDATE" ))

etc. etc.

I've done the usual searching through the literature which all assumes that
you want today's date pre-selected.

I'd be greatful for any adivce that would allow me to set the value to the
current data in the cell.

Thanks
 
K

Ken Cox [Microsoft MVP]

Hello ????

Are you trying to set the calendar to the value in the database or the value
in the textbox? Anyway, here's some code I came up with which seems to work.
You might want to try it and compare.

<asp:DataGrid id="DataGrid1" runat="server">
<Columns>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update"
CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
<asp:TemplateColumn HeaderText="The date">
<ItemTemplate>
<asp:Label id=Label1 runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.TheDate") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id=TextBox1 runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.TheDate") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Calendar">
<EditItemTemplate>
<asp:Calendar id="Calendar1" runat="server" VisibleDate='<%#
DataBinder.Eval(Container, "DataItem.TheDate") %>' SelectedDate='<%#
DataBinder.Eval(Container, "DataItem.TheDate") %>'></asp:Calendar>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
If Not IsPostBack Then
BindGrid()
End If
End Sub

Sub BindGrid()
' Set the data source and bind
' to the Data Grid control.
DataGrid1.DataSource = CreateDataSource()
DataGrid1.DataBind()
End Sub

Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("StringValue", GetType(String)))
dt.Columns.Add(New DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New DataColumn _
("TheDate", GetType(DateTime)))
Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = Now.AddMonths(i)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource

Private Sub DataGrid1_EditCommand _
(ByVal source As Object, _
ByVal e As System.Web.UI.WebControls. _
DataGridCommandEventArgs) _
Handles DataGrid1.EditCommand
DataGrid1.EditItemIndex = e.Item.ItemIndex
BindGrid()
End Sub

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]
Toronto
 
T

TenDot

Ken,

YES! thanks. I guess what I was missing was the VisibleDate attribute. I
had to cast the result of the databinder.eval into a DateTime as follows:

VisibleDate='<%# DateTime.Parse(DataBinder.Eval(Container,
"DataItem.INJECTIONDATE", "{0:MM/dd/yyyy}")) %>'
SelectedDate='<%# DateTime.Parse(DataBinder.Eval(Container,
"DataItem.INJECTIONDATE", "{0:MM/dd/yyyy}")) %>'>

If I didn't I got an error saying that the format was incorrect.

Thanks for your help!



Ken Cox said:
Hello ????

Are you trying to set the calendar to the value in the database or the value
in the textbox? Anyway, here's some code I came up with which seems to work.
You might want to try it and compare.

<asp:DataGrid id="DataGrid1" runat="server">
<Columns>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update"
CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
<asp:TemplateColumn HeaderText="The date">
<ItemTemplate>
<asp:Label id=Label1 runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.TheDate") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id=TextBox1 runat="server" Text='<%#
DataBinder.Eval(Container, "DataItem.TheDate") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Calendar">
<EditItemTemplate>
<asp:Calendar id="Calendar1" runat="server" VisibleDate='<%#
DataBinder.Eval(Container, "DataItem.TheDate") %>' SelectedDate='<%#
DataBinder.Eval(Container, "DataItem.TheDate") %>'></asp:Calendar>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
If Not IsPostBack Then
BindGrid()
End If
End Sub

Sub BindGrid()
' Set the data source and bind
' to the Data Grid control.
DataGrid1.DataSource = CreateDataSource()
DataGrid1.DataBind()
End Sub

Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("StringValue", GetType(String)))
dt.Columns.Add(New DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New DataColumn _
("TheDate", GetType(DateTime)))
Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 1.23 * (i + 1)
dr(3) = Now.AddMonths(i)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource

Private Sub DataGrid1_EditCommand _
(ByVal source As Object, _
ByVal e As System.Web.UI.WebControls. _
DataGridCommandEventArgs) _
Handles DataGrid1.EditCommand
DataGrid1.EditItemIndex = e.Item.ItemIndex
BindGrid()
End Sub

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]
Toronto
 
K

Ken Cox [Microsoft MVP]

Glad to help!

(But it would be nice if you used your name rather than TenDot. Unless that
is your name.)
 

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,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top