为什么我只能在Datalist中的奇数记录中得到正确的数据 [英] Why I Get The Correct Data Only In Odd No Of Records In Datalist
问题描述
我有一个数据列表,其中有一个显示日期时间的标签,并且在datalist中有一个网格。问题是我得到了所需的时间格式(例如1:48 AM)和网格内的数据仅在例如,1 3 5 7记录是正确的,并且2 4 6 7显示网格中的空记录,并且标签也显示不正确的格式(10/21/2015 1:48:20 AM)。这是代码。 onitemDataBound事件
I have a datalist in which have a label that shows the datetime and have a grid inside the datalist .The problem is that i get the desire format of time (for e.g 1:48 AM) and data inside the grid only on odd no of record for example 1 3 5 7 records are correct and 2 4 6 7 show the empty record in the grid and the label also show the incorrect format(10/21/2015 1:48:20 AM). Here is the code. onitemDataBound event
protected void dataListOrder_ItemDataBound(object sender, DataListItemEventArgs e)
{
if(e.Item.ItemType==ListItemType.Item)
{
Label date = e.Item.FindControl("lblOrderDate") as Label;
string[] datesplit = date.Text.Split(' ');
string time =string.Concat(datesplit[1]+" "+datesplit[2]);
date.Text = time.ToString();
int order_ID = Convert.ToInt32((e.Item.FindControl("orderid") as HiddenField).Value);
GridView innerDataList = e.Item.FindControl("datalistOrderDetails") as GridView;
string cs = ConfigurationManager.ConnectionStrings["SqlConnection"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
{
DataTable orderDetailDT = new DataTable();
con.Open();
string orderDetail = "select ItemName,Quantity from Order_Details where Order_ID=" + order_ID + "";
SqlCommand cmd = new SqlCommand(orderDetail, con);
cmd.CommandType = CommandType.Text;
SqlDataAdapter orderDetailDA = new SqlDataAdapter(cmd);
orderDetailDA.Fill(orderDetailDT);
innerDataList.DataSource = orderDetailDT;
innerDataList.DataBind();
}
}
}
注意:我也尝试在aspx文件中使用字符串格式来实现我想要的时间格式但是它不起作用
NOTE: I also try to use string format in aspx file to achieve my desired time format but it didn't work
<asp:Label ID="lblOrderDate" runat="server" Text='<%#Eval("OrderDate","{0:t}") %>'.
请指导代码中的问题,为什么它只显示奇数中的数据。谢谢
Please guide me where the problem in the code why its only show data in odd no. Thankyou
推荐答案
更改此行:
Change this line:
if(e.Item.ItemType==ListItemType.Item)
包含
to include
e.Item.ItemType == ListItemType.AlternateItem
这篇关于为什么我只能在Datalist中的奇数记录中得到正确的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!