How to format databound by DataList Control ?

B

bienwell

Hi,

I have a problem of displaying data bound by a datalist control. In my
table, I have a field Start_date which has Short Date data type. I tried to
update this field by Current Date. After that, I display End_Date on the
DataList control. The output looks like 3/18/2005 12:00:00 AM . I'd
like to format data for this field to be 3/18/2005. Please help me.

Thanks in advance.
=====================================================================
My DataList Control with the declaration as following:

<asp:DataList id="myDataList" OnItemDataBound="DataList_ItemBound"
Runat="Server" Width="500">
<HeaderTemplate>
<table border= "0">
</HeaderTemplate>
<ItemTemplate>
<tr>
<td class="dataTD" width= "100" >
<b>Start Date:</b>&nbsp;&nbsp;</b>
</td>
<td class="dataTD" width= "400">
<%# Container.DataItem("Start_Date") %>
</td>
</tr>
</ItemTemplate>
<SeparatorTemplate>
<tr>
<td colspan= "2" width= "500" align= "center">
<hr />
</td>
</tr>
</SeparatorTemplate>
<FooterTemplate>
<div align="center">
<tr>
<td colspan= "2" width= "500" align= "center">

</td>
</tr>
</table>
</FooterTemplate>
</asp:DataList>
 
K

Ken Cox [Microsoft MVP]

It seems like a custom date format string should handle that. You can insert
the function right in your HTML code as shown below.

Is that what you needed?

Ken
Microsoft MVP [ASP.NET]
Toronto

<form id="Form1" method="post" runat="server">
<asp:datalist id="myDataList"
OnItemDataBound="DataList_ItemBound" Runat="Server" Width="500">
<headertemplate>
<table border="0">
</headertemplate>
<itemtemplate>
<tr>
<td class="dataTD" width="100">
<b>Start Date:</b>&nbsp;&nbsp;</b>
</td>
<td class="dataTD" width="400">
<%#Format(Container.DataItem("Start_Date"),
"d/M/yyyy") %>
</td>
</tr>
</itemtemplate>
<separatortemplate>
<tr>
<td colspan="2" width="500" align="center">
<hr />
</td>
</tr>
</separatortemplate>
<footertemplate>
<div align="center">
<tr>
<td colspan="2" width="500" align="center">
</td>
</tr>
</table>
</footertemplate>
</asp:datalist>
</form>

Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
myDataList.DataSource = CreateDataSource()
myDataList.DataBind()
End If
End Sub

Public Sub DataList_ItemBound(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.DataListItemEventArgs)
End Sub
Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("Start_Date", GetType(DateTime)))
Dim i As Integer
For i = 0 To 4
dr = dt.NewRow()
dr(0) = Now.AddDays(i)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource
 
K

Ken Cox [Microsoft MVP]

It is from bad word-wrap. Move the line after line 266 up to the end of the
line? There shouldn't be a line break.



bienwell said:
Ken,

It's exactly what I need. However, when I tried it, I've got this error :

Argument 'Expression' is not a valid value.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.ArgumentException: Argument 'Expression' is not
a
valid value.

Source Error:

Line 264: </td>
Line 265: <td class="dataTD" width= "400">
Line 266:
<%#Format(Container.DataItem("Start_Date"),
"d/M/yyyy") %>




============================================================================
=========


Ken Cox said:
It seems like a custom date format string should handle that. You can insert
the function right in your HTML code as shown below.

Is that what you needed?

Ken
Microsoft MVP [ASP.NET]
Toronto

<form id="Form1" method="post" runat="server">
<asp:datalist id="myDataList"
OnItemDataBound="DataList_ItemBound" Runat="Server" Width="500">
<headertemplate>
<table border="0">
</headertemplate>
<itemtemplate>
<tr>
<td class="dataTD" width="100">
<b>Start Date:</b>&nbsp;&nbsp;</b>
</td>
<td class="dataTD" width="400">
<%#Format(Container.DataItem("Start_Date"),
"d/M/yyyy") %>
</td>
</tr>
</itemtemplate>
<separatortemplate>
<tr>
<td colspan="2" width="500" align="center">
<hr />
</td>
</tr>
</separatortemplate>
<footertemplate>
<div align="center">
<tr>
<td colspan="2" width="500" align="center">
</td>
</tr>
</table>
</footertemplate>
</asp:datalist>
</form>

Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
myDataList.DataSource = CreateDataSource()
myDataList.DataBind()
End If
End Sub

Public Sub DataList_ItemBound(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.DataListItemEventArgs)
End Sub
Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("Start_Date", GetType(DateTime)))
Dim i As Integer
For i = 0 To 4
dr = dt.NewRow()
dr(0) = Now.AddDays(i)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource


bienwell said:
Hi,

I have a problem of displaying data bound by a datalist control. In my
table, I have a field Start_date which has Short Date data type. I tried
to
update this field by Current Date. After that, I display End_Date on
the
DataList control. The output looks like 3/18/2005 12:00:00 AM .
I'd
like to format data for this field to be 3/18/2005. Please help me.

Thanks in advance.
=====================================================================
My DataList Control with the declaration as following:

<asp:DataList id="myDataList" OnItemDataBound="DataList_ItemBound"
Runat="Server" Width="500">
<HeaderTemplate>
<table border= "0">
</HeaderTemplate>
<ItemTemplate>
<tr>
<td class="dataTD" width= "100" >
<b>Start Date:</b>&nbsp;&nbsp;</b>
</td>
<td class="dataTD" width= "400">
<%# Container.DataItem("Start_Date") %>
</td>
</tr>
</ItemTemplate>
<SeparatorTemplate>
<tr>
<td colspan= "2" width= "500" align= "center">
<hr />
</td>
</tr>
</SeparatorTemplate>
<FooterTemplate>
<div align="center">
<tr>
<td colspan= "2" width= "500" align= "center">

</td>
</tr>
</table>
</FooterTemplate>
</asp:DataList>
 
K

Ken Cox [Microsoft MVP]

Line 266 shouldn't wrap. Make it look like this:

<%#Format(Container.DataItem("Start_Date"),"d/M/yyyy")%>

Let us know?


bienwell said:
Ken,

It's exactly what I need. I've got this error message when I tried it:

Argument 'Expression' is not a valid value.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.ArgumentException: Argument 'Expression' is not
a
valid value.

Source Error:

Line 264: </td>
Line 265: <td class="dataTD" width= "400">
Line 266:
<%#Format(Container.DataItem("Start_Date"),
"d/M/yyyy") %>



==============================================================

Ken Cox said:
It seems like a custom date format string should handle that. You can insert
the function right in your HTML code as shown below.

Is that what you needed?

Ken
Microsoft MVP [ASP.NET]
Toronto

<form id="Form1" method="post" runat="server">
<asp:datalist id="myDataList"
OnItemDataBound="DataList_ItemBound" Runat="Server" Width="500">
<headertemplate>
<table border="0">
</headertemplate>
<itemtemplate>
<tr>
<td class="dataTD" width="100">
<b>Start Date:</b>&nbsp;&nbsp;</b>
</td>
<td class="dataTD" width="400">
<%#Format(Container.DataItem("Start_Date"),
"d/M/yyyy") %>
</td>
</tr>
</itemtemplate>
<separatortemplate>
<tr>
<td colspan="2" width="500" align="center">
<hr />
</td>
</tr>
</separatortemplate>
<footertemplate>
<div align="center">
<tr>
<td colspan="2" width="500" align="center">
</td>
</tr>
</table>
</footertemplate>
</asp:datalist>
</form>

Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
myDataList.DataSource = CreateDataSource()
myDataList.DataBind()
End If
End Sub

Public Sub DataList_ItemBound(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.DataListItemEventArgs)
End Sub
Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("Start_Date", GetType(DateTime)))
Dim i As Integer
For i = 0 To 4
dr = dt.NewRow()
dr(0) = Now.AddDays(i)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource


bienwell said:
Hi,

I have a problem of displaying data bound by a datalist control. In my
table, I have a field Start_date which has Short Date data type. I tried
to
update this field by Current Date. After that, I display End_Date on
the
DataList control. The output looks like 3/18/2005 12:00:00 AM .
I'd
like to format data for this field to be 3/18/2005. Please help me.

Thanks in advance.
=====================================================================
My DataList Control with the declaration as following:

<asp:DataList id="myDataList" OnItemDataBound="DataList_ItemBound"
Runat="Server" Width="500">
<HeaderTemplate>
<table border= "0">
</HeaderTemplate>
<ItemTemplate>
<tr>
<td class="dataTD" width= "100" >
<b>Start Date:</b>&nbsp;&nbsp;</b>
</td>
<td class="dataTD" width= "400">
<%# Container.DataItem("Start_Date") %>
</td>
</tr>
</ItemTemplate>
<SeparatorTemplate>
<tr>
<td colspan= "2" width= "500" align= "center">
<hr />
</td>
</tr>
</SeparatorTemplate>
<FooterTemplate>
<div align="center">
<tr>
<td colspan= "2" width= "500" align= "center">

</td>
</tr>
</table>
</FooterTemplate>
</asp:DataList>
 
B

bienwell

Ken,

It's exactly what I need. However, when I tried it, I've got this error :

Argument 'Expression' is not a valid value.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.ArgumentException: Argument 'Expression' is not a
valid value.

Source Error:

Line 264: </td>
Line 265: <td class="dataTD" width= "400">
Line 266: <%#Format(Container.DataItem("Start_Date"),
"d/M/yyyy") %>




============================================================================
=========


Ken Cox said:
It seems like a custom date format string should handle that. You can insert
the function right in your HTML code as shown below.

Is that what you needed?

Ken
Microsoft MVP [ASP.NET]
Toronto

<form id="Form1" method="post" runat="server">
<asp:datalist id="myDataList"
OnItemDataBound="DataList_ItemBound" Runat="Server" Width="500">
<headertemplate>
<table border="0">
</headertemplate>
<itemtemplate>
<tr>
<td class="dataTD" width="100">
<b>Start Date:</b>&nbsp;&nbsp;</b>
</td>
<td class="dataTD" width="400">
<%#Format(Container.DataItem("Start_Date"),
"d/M/yyyy") %>
</td>
</tr>
</itemtemplate>
<separatortemplate>
<tr>
<td colspan="2" width="500" align="center">
<hr />
</td>
</tr>
</separatortemplate>
<footertemplate>
<div align="center">
<tr>
<td colspan="2" width="500" align="center">
</td>
</tr>
</table>
</footertemplate>
</asp:datalist>
</form>

Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
myDataList.DataSource = CreateDataSource()
myDataList.DataBind()
End If
End Sub

Public Sub DataList_ItemBound(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.DataListItemEventArgs)
End Sub
Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("Start_Date", GetType(DateTime)))
Dim i As Integer
For i = 0 To 4
dr = dt.NewRow()
dr(0) = Now.AddDays(i)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource


bienwell said:
Hi,

I have a problem of displaying data bound by a datalist control. In my
table, I have a field Start_date which has Short Date data type. I tried
to
update this field by Current Date. After that, I display End_Date on the
DataList control. The output looks like 3/18/2005 12:00:00 AM . I'd
like to format data for this field to be 3/18/2005. Please help me.

Thanks in advance.
=====================================================================
My DataList Control with the declaration as following:

<asp:DataList id="myDataList" OnItemDataBound="DataList_ItemBound"
Runat="Server" Width="500">
<HeaderTemplate>
<table border= "0">
</HeaderTemplate>
<ItemTemplate>
<tr>
<td class="dataTD" width= "100" >
<b>Start Date:</b>&nbsp;&nbsp;</b>
</td>
<td class="dataTD" width= "400">
<%# Container.DataItem("Start_Date") %>
</td>
</tr>
</ItemTemplate>
<SeparatorTemplate>
<tr>
<td colspan= "2" width= "500" align= "center">
<hr />
</td>
</tr>
</SeparatorTemplate>
<FooterTemplate>
<div align="center">
<tr>
<td colspan= "2" width= "500" align= "center">

</td>
</tr>
</table>
</FooterTemplate>
</asp:DataList>
 
B

bienwell

Ken,

It's exactly what I need. I've got this error message when I tried it:

Argument 'Expression' is not a valid value.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.ArgumentException: Argument 'Expression' is not a
valid value.

Source Error:

Line 264: </td>
Line 265: <td class="dataTD" width= "400">
Line 266: <%#Format(Container.DataItem("Start_Date"),
"d/M/yyyy") %>



==============================================================

Ken Cox said:
It seems like a custom date format string should handle that. You can insert
the function right in your HTML code as shown below.

Is that what you needed?

Ken
Microsoft MVP [ASP.NET]
Toronto

<form id="Form1" method="post" runat="server">
<asp:datalist id="myDataList"
OnItemDataBound="DataList_ItemBound" Runat="Server" Width="500">
<headertemplate>
<table border="0">
</headertemplate>
<itemtemplate>
<tr>
<td class="dataTD" width="100">
<b>Start Date:</b>&nbsp;&nbsp;</b>
</td>
<td class="dataTD" width="400">
<%#Format(Container.DataItem("Start_Date"),
"d/M/yyyy") %>
</td>
</tr>
</itemtemplate>
<separatortemplate>
<tr>
<td colspan="2" width="500" align="center">
<hr />
</td>
</tr>
</separatortemplate>
<footertemplate>
<div align="center">
<tr>
<td colspan="2" width="500" align="center">
</td>
</tr>
</table>
</footertemplate>
</asp:datalist>
</form>

Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
myDataList.DataSource = CreateDataSource()
myDataList.DataBind()
End If
End Sub

Public Sub DataList_ItemBound(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.DataListItemEventArgs)
End Sub
Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("Start_Date", GetType(DateTime)))
Dim i As Integer
For i = 0 To 4
dr = dt.NewRow()
dr(0) = Now.AddDays(i)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource


bienwell said:
Hi,

I have a problem of displaying data bound by a datalist control. In my
table, I have a field Start_date which has Short Date data type. I tried
to
update this field by Current Date. After that, I display End_Date on the
DataList control. The output looks like 3/18/2005 12:00:00 AM . I'd
like to format data for this field to be 3/18/2005. Please help me.

Thanks in advance.
=====================================================================
My DataList Control with the declaration as following:

<asp:DataList id="myDataList" OnItemDataBound="DataList_ItemBound"
Runat="Server" Width="500">
<HeaderTemplate>
<table border= "0">
</HeaderTemplate>
<ItemTemplate>
<tr>
<td class="dataTD" width= "100" >
<b>Start Date:</b>&nbsp;&nbsp;</b>
</td>
<td class="dataTD" width= "400">
<%# Container.DataItem("Start_Date") %>
</td>
</tr>
</ItemTemplate>
<SeparatorTemplate>
<tr>
<td colspan= "2" width= "500" align= "center">
<hr />
</td>
</tr>
</SeparatorTemplate>
<FooterTemplate>
<div align="center">
<tr>
<td colspan= "2" width= "500" align= "center">

</td>
</tr>
</table>
</FooterTemplate>
</asp:DataList>
 
B

bienwell

Ken,

Still have that error message. Actually, I did it last time like this
<%#Format(Container.DataItem("Start_Date"), "d/M/yyyy") %> in one line
of the .aspx file and got that error message when running the page. It's
not because of word wrap. I cut and pasted the error message from Web page
and you saw it not in one line.

Thanks.

=========================================================
Ken Cox said:
Line 266 shouldn't wrap. Make it look like this:

<%#Format(Container.DataItem("Start_Date"),"d/M/yyyy")%>

Let us know?


bienwell said:
Ken,

It's exactly what I need. I've got this error message when I tried it:

Argument 'Expression' is not a valid value.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.ArgumentException: Argument 'Expression' is not
a
valid value.

Source Error:

Line 264: </td>
Line 265: <td class="dataTD" width= "400">
Line 266:
<%#Format(Container.DataItem("Start_Date"),
"d/M/yyyy") %>



==============================================================

Ken Cox said:
It seems like a custom date format string should handle that. You can insert
the function right in your HTML code as shown below.

Is that what you needed?

Ken
Microsoft MVP [ASP.NET]
Toronto

<form id="Form1" method="post" runat="server">
<asp:datalist id="myDataList"
OnItemDataBound="DataList_ItemBound" Runat="Server" Width="500">
<headertemplate>
<table border="0">
</headertemplate>
<itemtemplate>
<tr>
<td class="dataTD" width="100">
<b>Start Date:</b>&nbsp;&nbsp;</b>
</td>
<td class="dataTD" width="400">
<%#Format(Container.DataItem("Start_Date"),
"d/M/yyyy") %>
</td>
</tr>
</itemtemplate>
<separatortemplate>
<tr>
<td colspan="2" width="500" align="center">
<hr />
</td>
</tr>
</separatortemplate>
<footertemplate>
<div align="center">
<tr>
<td colspan="2" width="500" align="center">
</td>
</tr>
</table>
</footertemplate>
</asp:datalist>
</form>

Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
myDataList.DataSource = CreateDataSource()
myDataList.DataBind()
End If
End Sub

Public Sub DataList_ItemBound(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.DataListItemEventArgs)
End Sub
Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("Start_Date", GetType(DateTime)))
Dim i As Integer
For i = 0 To 4
dr = dt.NewRow()
dr(0) = Now.AddDays(i)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource


Hi,

I have a problem of displaying data bound by a datalist control. In my
table, I have a field Start_date which has Short Date data type. I tried
to
update this field by Current Date. After that, I display End_Date on
the
DataList control. The output looks like 3/18/2005 12:00:00 AM .
I'd
like to format data for this field to be 3/18/2005. Please help me.

Thanks in advance.
=====================================================================
My DataList Control with the declaration as following:

<asp:DataList id="myDataList" OnItemDataBound="DataList_ItemBound"
Runat="Server" Width="500">
<HeaderTemplate>
<table border= "0">
</HeaderTemplate>
<ItemTemplate>
<tr>
<td class="dataTD" width= "100" >
<b>Start Date:</b>&nbsp;&nbsp;</b>
</td>
<td class="dataTD" width= "400">
<%# Container.DataItem("Start_Date") %>
</td>
</tr>
</ItemTemplate>
<SeparatorTemplate>
<tr>
<td colspan= "2" width= "500" align= "center">
<hr />
</td>
</tr>
</SeparatorTemplate>
<FooterTemplate>
<div align="center">
<tr>
<td colspan= "2" width= "500" align= "center">

</td>
</tr>
</table>
</FooterTemplate>
</asp:DataList>
 
K

Ken Cox [Microsoft MVP]

Hmmm. I can't figure out why it doesn't work for you. The script is running
just fine on my Web page here:

http://www.kencox.ca/datalstscript.aspx

Here's the entire code for that page:

<%@ Page Language="vb" AutoEventWireup="true" %>
<%@ Import namespace="System.Data" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>datalstscript</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name=vs_defaultClientScript content="JavaScript">
<meta name=vs_targetSchema
content="http://schemas.microsoft.com/intellisense/ie5">
<script language="vb" runat="server">
Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
myDataList.DataSource = CreateDataSource()
myDataList.DataBind()
End If
End Sub

Public Sub DataList_ItemBound(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.DataListItemEventArgs)
End Sub
Function CreateDataSource() As DataTable
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("Start_Date", GetType(DateTime)))
Dim i As Integer
For i = 0 To 4
dr = dt.NewRow()
dr(0) = Now.AddDays(i)
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource
</script>
</head>
<body MS_POSITIONING="FlowLayout">

<form id="Form1" method="post" runat="server">
<asp:datalist id="myDataList"
OnItemDataBound="DataList_ItemBound" Runat="Server" Width="500">
<headertemplate>
<table border="0">
</headertemplate>
<itemtemplate>
<tr>
<td class="dataTD" width="100">
<b>Start Date:</b>&nbsp;&nbsp;</b>
</td>
<td class="dataTD" width="400">
<%#Format(Container.DataItem("Start_Date"),"d/M/yyyy")%>
</td>
</tr>
</itemtemplate>
<separatortemplate>
<tr>
<td colspan="2" width="500" align="center">
<hr />
</td>
</tr>
</separatortemplate>
<footertemplate>
<div align="center">
<tr>
<td colspan="2" width="500" align="center">
</td>
</tr>
</table>
</footertemplate>
</asp:datalist>
</form>

</body>
</html>
 
B

bienwell

Ken,

I've tried to run your code; it worked fine. I will take a look on my
program or run it in another PC to see what will happen.

Thanks.
================================================
 

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,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top