I am reading data from database and displaying them in Datalist , I need to get the data of the selected item(s) from the datalist and display them but im getting an error stating :
Unable to cast object of type 'System.Web.UI.WebControls.DataListItem' to type 'System.Web.UI.WebControls.ListItem'.
below is my complete code :
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("Data Source=.\\sqlexpress;Initial Catalog=dbmanipulate;Integrated Security=True");
SqlCommand cmd = new SqlCommand("SELECT WDID,WebSiteName,WebSiteKey FROM tbl_websiteDatabases ",conn);
SqlDataReader reader;
conn.Open();
reader = cmd.ExecuteReader();
employeesList.DataSource = reader;
employeesList.DataBind();
reader.Close();
}
protected void employeesList_SelectedIndexChanged(object sender, EventArgs e)
{
List<String> YrStrList = new List<string>();
foreach (ListItem naf in employeesList.Items)
{
if (naf.Selected)
{
YrStrList.Add(naf.Value);
}
else
{
}
}
// Join the string together using the ; delimiter.
String YrStr = String.Join(";", YrStrList.ToArray());
// Write to the page the value.
Response.Write(String.Concat("Selected Items: ", YrStr));
}
The List<String>.Add accept a object of type String while you ware just passing as sting value.
The "New String(Char[])" will intilize a new instance of type String and add this in the List<String> on fly. And as the constructoer of the String() accept the array of char you can convert the DataListItem.Value in to ToCharArray() befor adding it to the
List<>.
man thank you for this hint , now I cant get to complete the code , If I type :
foreach (DataListItem item in employeesList.Items)
{
if (item.selected) == here is the error !!!
{
// If the item is selected, add the value to the list.
// YrStrList.Add(item.Value);
YrStrList.Add(new String(item.Value.ToCharArray()));
}
You are using the DataListItem and then parsing it aginst the listitem in foreach loop. Use the ListBox rather then DataListItem. So in the View page change it like this and then rest of the code will be fine
You are DataBinding in Page_Load. If you do that on the PostBack of the SelectedIndexChanged, the DataBind will overwrite the Controls that caught the event.
When DataBinding in Page_Load, do so only when !IsPostBack.
Superguppie.
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.
senior_oracl...
Member
30 Points
163 Posts
cant get selected item in DataList
Aug 20, 2012 03:37 PM|LINK
I am reading data from database and displaying them in Datalist , I need to get the data of the selected item(s) from the datalist and display them but im getting an error stating :
Unable to cast object of type 'System.Web.UI.WebControls.DataListItem' to type 'System.Web.UI.WebControls.ListItem'.
below is my complete code :
protected void Page_Load(object sender, EventArgs e) { SqlConnection conn = new SqlConnection("Data Source=.\\sqlexpress;Initial Catalog=dbmanipulate;Integrated Security=True"); SqlCommand cmd = new SqlCommand("SELECT WDID,WebSiteName,WebSiteKey FROM tbl_websiteDatabases ",conn); SqlDataReader reader; conn.Open(); reader = cmd.ExecuteReader(); employeesList.DataSource = reader; employeesList.DataBind(); reader.Close(); } protected void employeesList_SelectedIndexChanged(object sender, EventArgs e) { List<String> YrStrList = new List<string>(); foreach (ListItem naf in employeesList.Items) { if (naf.Selected) { YrStrList.Add(naf.Value); } else { } } // Join the string together using the ; delimiter. String YrStr = String.Join(";", YrStrList.ToArray()); // Write to the page the value. Response.Write(String.Concat("Selected Items: ", YrStr)); }Zaheerbasra
Member
418 Points
91 Posts
Re: cant get selected item in DataList
Aug 20, 2012 03:52 PM|LINK
Try this way
Zaheerbasra
Member
418 Points
91 Posts
Re: cant get selected item in DataList
Aug 20, 2012 03:53 PM|LINK
The List<String>.Add accept a object of type String while you ware just passing as sting value.
The "New String(Char[])" will intilize a new instance of type String and add this in the List<String> on fly. And as the constructoer of the String() accept the array of char you can convert the DataListItem.Value in to ToCharArray() befor adding it to the List<>.
senior_oracl...
Member
30 Points
163 Posts
Re: cant get selected item in DataList
Aug 20, 2012 03:58 PM|LINK
Still facing the same problem friend .......
oned_gk
All-Star
31766 Points
6493 Posts
Re: cant get selected item in DataList
Aug 20, 2012 03:59 PM|LINK
senior_oracl...
Member
30 Points
163 Posts
Re: cant get selected item in DataList
Aug 20, 2012 04:18 PM|LINK
man thank you for this hint , now I cant get to complete the code , If I type :
foreach (DataListItem item in employeesList.Items) { if (item.selected) == here is the error !!! { // If the item is selected, add the value to the list. // YrStrList.Add(item.Value); YrStrList.Add(new String(item.Value.ToCharArray())); }any hint ???
Zaheerbasra
Member
418 Points
91 Posts
Re: cant get selected item in DataList
Aug 20, 2012 04:25 PM|LINK
You are using the DataListItem and then parsing it aginst the listitem in foreach loop. Use the ListBox rather then DataListItem. So in the View page change it like this and then rest of the code will be fine
<asp:ListBox id="employeesList" runat="server"></asp:ListItem>
In the Code behind change it
List<String> YrStrList = new List<string>(); foreach (ListItem naf in employeesList.Items) { if (naf.Selected) { YrStrList.Add(new String(naf.Value.ToCharArray())); } else { } } String YrStr = String.Join(";", YrStrList.ToArray()); // Write to the page the value. Response.Write(String.Concat("Selected Items: ", YrStr));senior_oracl...
Member
30 Points
163 Posts
Re: cant get selected item in DataList
Aug 20, 2012 04:45 PM|LINK
my friend I need a datalist and not a listview, as the page will be displaying products to the user to select from ,
anyway thank for your help ..
Zaheerbasra
Member
418 Points
91 Posts
Re: cant get selected item in DataList
Aug 20, 2012 05:53 PM|LINK
Here is the link where you can find information if want to use the DataList as list of products and then find the selected item from it
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datalist.selecteditem.aspx
superguppie
All-Star
48225 Points
8679 Posts
Re: cant get selected item in DataList
Aug 21, 2012 08:25 AM|LINK
You are DataBinding in Page_Load. If you do that on the PostBack of the SelectedIndexChanged, the DataBind will overwrite the Controls that caught the event.
When DataBinding in Page_Load, do so only when !IsPostBack.
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.