locked
List View Click Event Error. RRS feed

  • Question

  • User988538322 posted

    Hi i want to show Listview click Name in MsgBox. here is my Code

    <head runat="server">
        <title>
        </title>
    
        <script runat="server">
           Protected Sub EmployeesListView_OnItemCommand(ByVal sender As Object, ByVal e As ListViewCommandEventArgs)
                Dim dataItem As ListViewDataItem = CType(e.Item, ListViewDataItem)
                Dim CountryName As String = lvCountryNames.DataKeys(dataItem.DisplayIndex).Value.ToString()
                MsgBox("The Name of Country is " & CountryName)
           End Sub
          
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                     <table class="Len">
                        <tr>
                            <td>
                                <asp:ListView ID="lvCountryNames"  OnItemCommand="EmployeesListView_OnItemCommand" runat="server"  GroupItemCount="5" 
                                    style="font-family: Cambria; font-size: small">
                      
                                    <LayoutTemplate>
                                       <table id="Table1" runat="server">
                                           <tr id="Tr1" runat="server">
                                               <td id="Td1" runat="server">
                                                   <table ID="groupPlaceholderContainer" runat="server" border="0" style="width:962px;">
                                                       <tr ID="groupPlaceholder" runat="server">
                                                       </tr>
                                                   </table>
                                               </td>
                                           </tr>
                                           <tr id="Tr3" runat="server">
                                               <td id="Td2" runat="server" style="">
                                               </td>
                                           </tr>
                                       </table>
                                    </LayoutTemplate>
                       
                                    <GroupTemplate>
                                       <tr ID="itemPlaceholderContainer" runat="server">
                                           <td ID="itemPlaceholder" runat="server">
                                           </td>
                                       </tr>
                                    </GroupTemplate>
    
                                    <ItemTemplate>
                                        <td class="ListView">
                                            <table>
                                                 <tr>
                                                    <td align="center">
                                                       <asp:LinkButton ID="LinkButton1" runat="server" Text='<%#Eval("Name") %>'>
                                                        </asp:LinkButton>
                                                    </td>
                                               </tr>
                                            </table>
                                        </td>
                                    </ItemTemplate>
                                </asp:ListView>
                </td>
            </tr>
            </table>    
            </div>
        </form>
    </body>
    
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Fills.FillListView(lvCountryNames, "select Name from CountryInfo order by Name")
        End Sub

    Error is on this line. 

    Dim CountryName As String = lvCountryNames.DataKeys(dataItem.DisplayIndex).Value.ToString()

    Please Help

    Thursday, June 30, 2011 5:55 AM

Answers

  • User2143783175 posted

    Please try this

    Protected Sub EmployeesListView_OnItemCommand(ByVal sender As Object, ByVal e As ListViewCommandEventArgs)
                Dim dataItem As ListViewDataItem = CType(e.Item, ListViewDataItem)
                Dim CountryName As String = CType(dataItem.FinfControl("LinkButton1")).Text.ToString()             MsgBox("The Name of Country is " & CountryName)
           End Sub
    
    Hope this will solve ur problem
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 30, 2011 8:33 AM

All replies

  • User2143783175 posted

    Please try this

    Protected Sub EmployeesListView_OnItemCommand(ByVal sender As Object, ByVal e As ListViewCommandEventArgs)
                Dim dataItem As ListViewDataItem = CType(e.Item, ListViewDataItem)
                Dim CountryName As String = CType(dataItem.FinfControl("LinkButton1")).Text.ToString()             MsgBox("The Name of Country is " & CountryName)
           End Sub
    
    Hope this will solve ur problem
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 30, 2011 8:33 AM
  • User988538322 posted

    thank you so much najmulansari . you send me that code

    Dim CountryName As String = CType(dataItem.FinfControl("LinkButton1")).Text.ToString()

    but when i use it comes error and in you advice i use that Code

    Dim cName As LinkButton = CType(dataItem.FindControl("LinkButton1"), LinkButton)
    Dim countryName As String = cName.Text
    but its posible with you Advice Thank you .


    Thursday, June 30, 2011 11:41 PM