protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
Label l = (Label)e.Item.FindControl("raj");
if (l != null)
{
using (OSEntities context1 = new OSEntities())
{
var vv = (from nInst in context1.OS select nInst.InsertDate).ToList();
var newList = new List<string>();
foreach (DateTime t in vv)
{
String ss = GetPrettyDate(t);
newList.Add(ss);
}
this.ListView1.DataSource = newList;
//HERE I WANT TO BIND DATA TO DateDisplayLabel like below
l.Text = ss; --- But I am getting an error message.
this.ListView1.DataBind();
}
}
}
How should I bind data for "DateDisplayLabel" label from code instead of "InsertDate" database field.
I have modified this "InsertDate" database field in my code and want to use the new modified value.
you are getting error becuase you have declared ss variable in foreach loop and using ss variable out of function scope
athelli_reddy
foreach(DateTime t in vv) {
String ss
=GetPrettyDate(t);
newList.Add(ss);
}
this.ListView1.DataSource= newList; //HERE I WANT TO BIND DATA TO DateDisplayLabel like below
l.Text= ss;---But I am getting an error message. this.ListView1.DataBind();
just declare string ss variable before foreach loop , and do this
String ss = "";
foreach (DateTime t in vv)
{
ss = GetPrettyDate(t);
newList.Add(ss);
}
"Data properties on data control 'ListView1' such as DataSource, DataSourceID, and DataMember cannot be changed during the databinding phase of the control"
athelli_redd...
Member
8 Points
55 Posts
Binding modified data source to listview label
Apr 27, 2012 01:35 PM|LINK
This is my .aspx page:
<asp:ListView ID="ListView1" runat="server" DataSourceID="EntityDataSource1" OnItemDataBound="ListView1_ItemDataBound"> <ItemTemplate> <asp:Label ID="DescriptionLabel" runat="server" Text='<%# Eval("Description") %>' /> <asp:Label ID="DateDisplayLabel" ForeColor="Red" runat="server" Text='<%# Eval("InsertDate") %>' /> </ItemTemplate> </ListView>protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e) { Label l = (Label)e.Item.FindControl("raj"); if (l != null) { using (OSEntities context1 = new OSEntities()) { var vv = (from nInst in context1.OS select nInst.InsertDate).ToList(); var newList = new List<string>(); foreach (DateTime t in vv) { String ss = GetPrettyDate(t); newList.Add(ss); } this.ListView1.DataSource = newList; //HERE I WANT TO BIND DATA TO DateDisplayLabel like below l.Text = ss; --- But I am getting an error message. this.ListView1.DataBind(); } } }How should I bind data for "DateDisplayLabel" label from code instead of "InsertDate" database field.
I have modified this "InsertDate" database field in my code and want to use the new modified value.
can someone please help me?
thanks in advance.
databind listview
newbiefreak
Member
468 Points
214 Posts
Re: Binding modified data source to listview label
Apr 27, 2012 01:47 PM|LINK
you are getting error becuase you have declared ss variable in foreach loop and using ss variable out of function scope
just declare string ss variable before foreach loop , and do this
String ss = ""; foreach (DateTime t in vv) { ss = GetPrettyDate(t); newList.Add(ss); }i hope this helps you
newbiefreak
Member
468 Points
214 Posts
Re: Binding modified data source to listview label
Apr 27, 2012 01:55 PM|LINK
and also change from
Label l = (Label)e.Item.FindControl("raj"); ToLabel l = (Label)e.Item.FindControl("DescriptionLabel");and you dont need to call databind in the item bound you obviously calling databind function when you had assigned datasource to listview e.g
athelli_redd...
Member
8 Points
55 Posts
Re: Binding modified data source to listview label
Apr 27, 2012 02:06 PM|LINK
Thanks Ahmed,
now I am getting the following error message:
"Data properties on data control 'ListView1' such as DataSource, DataSourceID, and DataMember cannot be changed during the databinding phase of the control"
newbiefreak
Member
468 Points
214 Posts
Re: Binding modified data source to listview label
Apr 27, 2012 02:13 PM|LINK
remove
this.listview1.databind();
from itemdatabound function and try running again
athelli_redd...
Member
8 Points
55 Posts
Re: Binding modified data source to listview label
Apr 27, 2012 02:15 PM|LINK
Hi Ahmed,
I am still getting the same error message.
I remoed this: this.listview1.databind();
athelli_redd...
Member
8 Points
55 Posts
Re: Binding modified data source to listview label
Apr 27, 2012 02:20 PM|LINK
If i remove this as well: this.ListView1.DataSource = newList
only last record is displaying in my label.
I have 20 records in my table but it is displaying only one value 20 times.
How should I display all the value instead of same one 20 times
athelli_redd...
Member
8 Points
55 Posts
Re: Binding modified data source to listview label
Apr 27, 2012 02:27 PM|LINK
It if displaying last record only when use:
l.Text = ss;
it is not working if write :
l.Test = newList;
newbiefreak
Member
468 Points
214 Posts
Re: Binding modified data source to listview label
Apr 27, 2012 02:27 PM|LINK
hi,
also remove this.listview1.datasource line in the function
you cant rebind or assign datasource to the same control like you are doing in listview1.itemdatabound
can you describe your senerio ?? if after removing datasource line in the function didnt helped u
newbiefreak
Member
468 Points
214 Posts
Re: Binding modified data source to listview label
Apr 27, 2012 02:31 PM|LINK
why dont you just append the string in the foreach loop
like
ss += " " + GetPrettyDate(t);
and
label.text = ss;