I have a list view which bind with custom data source by using ListView.DataSource Property. However, if so, the select button will not work properly. First click on select button in a desired row, there is no changes on post back pages. Another click on
any select button, the row that first selected will be selected. That's frustrating.
This is because selectedIndexChanging is handled when the select button is clicked but before the control handles the select. There is therefore no selectedIndex yet! But by the second click, there is a selectedIndex - the one from last click. It did not
change yet because the new select was not yet handled.
Change the method to selectedIndexChanged and you should be good.
well, ListView_SelectedIndexChanged doesn't have ListViewSelectEventArgs as parameter, it takes EventArgs as event parameter, which doesn't have any NewSelectedIndex property.
Also, I have find that e.NewSelectedIndex indeed has correct value during selectedIndexChanging events.
Try to bind your listview in (!IsPostBack) in Page load event.
This won't work. In fact, my original code has (!IsPostBack). I google some solution, some one said to Data Bind every time in page_load will work, but it is not for me
Cheng Bao
Member
245 Points
145 Posts
ListView Select Button have to be clicked twice to function for custom datasource
Jan 03, 2013 06:27 PM|LINK
I have a list view which bind with custom data source by using ListView.DataSource Property. However, if so, the select button will not work properly. First click on select button in a desired row, there is no changes on post back pages. Another click on any select button, the row that first selected will be selected. That's frustrating.
Code:
<form id="form1" runat="server" > <asp:ListView ID="lvTest" runat="server" onselectedindexchanging="lvTest_SelectedIndexChanging"> <LayoutTemplate> <table> <tr runat="server" id="itemPlaceholder"></tr> </table> </LayoutTemplate> <ItemTemplate> <tr runat="server"> <td><%# Container.DataItem.ToString() %></td> <td><asp:LinkButton ID="LinkButton1" runat="server" CommandName="Select">Select</asp:LinkButton></td> </tr> </ItemTemplate> <SelectedItemTemplate> <tr id="Tr1" runat="server"> <td><%# Container.DataItem.ToString() %></td> <td>Selected</td> </tr> </SelectedItemTemplate> </asp:ListView> </form>Code Behind
public partial class _Default : Page { void Page_Load(object sender, EventArgs e) { BindData(); } void BindData() { int[] list = new int[] {1,2,3,4,5}; lvTest.DataSource = list; lvTest.DataBind(); } protected void lvTest_SelectedIndexChanging(object sender, ListViewSelectEventArgs e) { lvTest.SelectedIndex = e.NewSelectedIndex; } }msmk
Participant
797 Points
180 Posts
Re: ListView Select Button have to be clicked twice to function for custom datasource
Jan 03, 2013 06:31 PM|LINK
This is because selectedIndexChanging is handled when the select button is clicked but before the control handles the select. There is therefore no selectedIndex yet! But by the second click, there is a selectedIndex - the one from last click. It did not change yet because the new select was not yet handled.
Change the method to selectedIndexChanged and you should be good.
Cheng Bao
Member
245 Points
145 Posts
Re: ListView Select Button have to be clicked twice to function for custom datasource
Jan 03, 2013 06:42 PM|LINK
well, ListView_SelectedIndexChanged doesn't have ListViewSelectEventArgs as parameter, it takes EventArgs as event parameter, which doesn't have any NewSelectedIndex property.
Also, I have find that e.NewSelectedIndex indeed has correct value during selectedIndexChanging events.
Madhu1234
Participant
1380 Points
287 Posts
Re: ListView Select Button have to be clicked twice to function for custom datasource
Jan 03, 2013 06:58 PM|LINK
Try to bind your listview in (!IsPostBack) in Page load event.
Cheng Bao
Member
245 Points
145 Posts
Re: ListView Select Button have to be clicked twice to function for custom datasource
Jan 03, 2013 06:59 PM|LINK
Ok, I remember I met similar problem before, then I search my own posts, I find I already got a solution before
http://forums.asp.net/t/1798672.aspx/1?DataPager+bug+
I need to bind data in Page_PreRender event. I believe it is a bug.
Cheng Bao
Member
245 Points
145 Posts
Re: ListView Select Button have to be clicked twice to function for custom datasource
Jan 03, 2013 07:00 PM|LINK
This won't work. In fact, my original code has (!IsPostBack). I google some solution, some one said to Data Bind every time in page_load will work, but it is not for me
Madhu1234
Participant
1380 Points
287 Posts
Re: ListView Select Button have to be clicked twice to function for custom datasource
Jan 03, 2013 07:15 PM|LINK
Try to get the selectedindex value like this..
protected void lvTest_SelectedIndexChanged(object sender,System.Web.UI.WebControls.DataListItemEventArgs e)
{
lvTest.SelectedIndex=e.Item.ItemIndex;
}
Cheng Bao
Member
245 Points
145 Posts
Re: ListView Select Button have to be clicked twice to function for custom datasource
Jan 03, 2013 07:30 PM|LINK
You will get compliation error
Compiler Error Message: CS0123: No overload for 'lvTest_SelectedIndexChanged' matches delegate 'System.EventHandler'
Even force cast EventArgs to DataListItemEventArgs will fail
protected void lvTest_SelectedIndexChanged(object sender, EventArgs e) { var e2 = (System.Web.UI.WebControls.DataListItemEventArgs) e; }Unable to cast object of type 'System.EventArgs' to type 'System.Web.UI.WebControls.DataListItemEventArgs'.
the args is indeed System.EventArgs, nothing else.
Madhu1234
Participant
1380 Points
287 Posts
Re: ListView Select Button have to be clicked twice to function for custom datasource
Jan 03, 2013 07:40 PM|LINK
Sorry I mistook it as Datalist instead of listview....:(
will now check the solution for listview..
Madhu1234
Participant
1380 Points
287 Posts
Re: ListView Select Button have to be clicked twice to function for custom datasource
Jan 03, 2013 07:46 PM|LINK
Check this example for How to make a row selectable in listview...
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listview.selectedindexchanged(v=vs.90).aspx
Hope this helps you.