Click here to Skip to main content
15,913,408 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
why ListView.selectedIndex always returns value -1 in vb.net framework 3.5 ?

I have tried the same thing in C#.Net is working fine...

Code is:
XML
<asp:ListView ID="lvSectors" runat="server" DataKeyNames="SectorID" OnItemCommand="lvSelectors_ItemCommand"
                            OnSelectedIndexChanged="lvSelectors_SelectedIndexChanged" OnSelectedIndexChanging="lvSelectors_SelectedIndexChanging">

                                <table id="itemPlaceholderContainer"  runat="server" align="center" border="1" cellpadding="0"
                                    cellspacing="0">
                                    <tr  runat="server" style="">
                                        <th align="center">
                                            <asp:LinkButton ID="lbSr_No" runat="server" CommandName="Sort" Width="150"><span style="color :Blue ">Sr No</span>
                                        </th>
                                        <th align="center">
                                            <asp:LinkButton ID="lbSector_Text" runat="server" CommandName="Sort" Width="150"><span style="color :Blue ">SectorName</span>
                                        </th>
                                        <th align="center">
                                            <asp:LinkButton ID="lbDescription" runat="server" CommandName="Sort" Width="150"><span style="color :Blue ">Description</span>
                                        </th>
                                        <th>
                                            Edit
                                        </th>
                                        <th>
                                            Delete
                                        </th>
                                    </tr>
                                    <tr id="itemPlaceholder"  runat="server">
                                    </tr>
                                </table>
                                <asp:DataPager ID="dpSectors" runat="server" PagedControlID="lvSectors">



                                <tr>
                                    <td align="center">
                                        <%#Container.DataItemIndex+1 %>
                                    </td>
                                    <td align="center">
                                        <asp:Label ID="lblSector_Text" runat="server" Text='<%#Bind("SectorText") %>'>

                                    </td>
                                    <td align="center">
                                        <asp:Label ID="lblDescription" runat="server" Text='<%#Bind("Description") %>'>

                                    </td>
                                    <td align="center" id="edit1">
                                        <asp:LinkButton ID="lbSector_Edit" runat="server" Width="150" CommandArgument='<%#Bind("SectorID") %>'
                                            CommandName="Select" Text="Edit" CssClass="editclass">
                                    </td>
                                    <td align="center">
                                        <asp:LinkButton ID="lbSector_Delete" runat="server" CommandArgument='<%#Bind("SectorID") %>'
                                            CommandName="Remove" Width="150" Text="Delete" CssClass="deleteClass" OnClientClick='return confirm("Are you sure you want to delete this entry?");'>
                                    </td>
                                </t
Posted
Updated 23-Mar-11 12:02pm
v3
Comments
Wendelius 23-Mar-11 7:44am    
Most likely there's something wrong in your VB.Net version that was correctly in C# version. Could you post the code?
Pratik Bhesaniya 23-Mar-11 8:32am    
<asp:ListView ID="lvSectors" runat="server" DataKeyNames="SectorID" OnItemCommand="lvSelectors_ItemCommand"
OnSelectedIndexChanged="lvSelectors_SelectedIndexChanged" OnSelectedIndexChanging="lvSelectors_SelectedIndexChanging">
<layouttemplate>
<table id="itemPlaceholderContainer" runat="server" align="center" border="1" cellpadding="0"
cellspacing="0">
<tr runat="server" style="">
<th align="center">
<asp:LinkButton ID="lbSr_No" runat="server" CommandName="Sort" Width="150"><span style="color :Blue ">Sr No</span>
</th>
<th align="center">
<asp:LinkButton ID="lbSector_Text" runat="server" CommandName="Sort" Width="150"><span style="color :Blue ">SectorName</span>
</th>
<th align="center">
<asp:LinkButton ID="lbDescription" runat="server" CommandName="Sort" Width="150"><span style="color :Blue ">Description</span>
</th>
<th>
Edit
</th>
<th>
Delete
</th>
</tr>
<tr id="itemPlaceholder" runat="server">
</tr>
</table>
<asp:DataPager ID="dpSectors" runat="server" PagedControlID="lvSectors">


<itemtemplate>
<tr>
<td align="center">
<%#Container.DataItemIndex+1 %>
</td>
<td align="center">
<asp:Label ID="lblSector_Text" runat="server" Text='<%#Bind("SectorText") %>'>

</td>
<td align="center">
<asp:Label ID="lblDescription" runat="server" Text='<%#Bind("Description") %>'>

</td>
<td align="center" id="edit1">
<asp:LinkButton ID="lbSector_Edit" runat="server" Width="150" CommandArgument='<%#Bind("SectorID") %>'
CommandName="Select" Text="Edit" CssClass="editclass">
</td>
<td align="center">
<asp:LinkButton ID="lbSector_Delete" runat="server" CommandArgument='<%#Bind("SectorID") %>'
CommandName="Remove" Width="150" Text="Delete" CssClass="deleteClass" OnClientClick='return confirm("Are you sure you want to delete this entry?");'>
</td>
</t
#realJSOP 23-Mar-11 10:50am    
It's because VB sucks. Believe me. I know.

1 solution

The SelectedIndex property is more than a bit messed up. I would even go so far as to call it a bug in the framework. Read this[^].
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 23-Mar-11 18:04pm    
Wow! I'll need to check it up. What's the simplest case to manifest the bug?
--SA
Dave Kreskowiak 23-Mar-11 18:10pm    
I have no idea and no desire to test it.
Sergey Alexandrovich Kryukov 23-Mar-11 19:07pm    
OK, that I can understand... Thanks anyway.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900