suman
suman

Reputation: 728

How to get selected item in ListView in asp.net

I have used Listview to display data from database and i have seperated each data from each row using a <hr/> tag.How do I select one particular row so that i can track which item is being selected.

Edit

Here is my Listview

 <asp:ListView ID="msg_list" runat="server" OnSelectedIndexChanged="selectedMsg" >
   <ItemTemplate>
    <table>
      <tr class="myitem">
       <a href="#"> <td>
             <asp:Label role="menuitem" ID="msg_lbl" runat="server" text='<%#Eval("msg")%>' /><i style=" color:Gray; " >  from   
             <asp:Label ID="tme" runat="server" Text='<%#Eval("name")%>' />
             <i> on </i>
             <asp:Label ID="tmelbl" runat="server" Text='<%#Eval("tme")%>'/>
              <a id="msg-reply" class="btn button" data-toggle="modal" data-target="#msg-rply" style="cursor:pointer;" ><i class="glyphicon glyphicon-share-alt white"> </i></a> 
            </td>
        </a>

              <hr style=" margin-top:1px; margin-bottom:1px; " />
      </tr>
     </table>
     <%--<hr style=" margin-top:1px; margin-bottom:1px; " />--%>
   </ItemTemplate>

   <SelectedItemTemplate>
      <tr class="myitem">
       <a href="#"> <td>
             <asp:Label role="menuitem" ID="msg_lbl" runat="server" text='<%#Eval("msg")%>' /><i style=" color:Gray; " >  from   
             <asp:Label ID="tme" runat="server" Text='<%#Eval("name")%>' />
             <i> on </i>
             <asp:Label ID="tmelbl" runat="server" Text='<%#Eval("tme")%>'/>
              <a id="msg-reply" class="btn button" data-toggle="modal" data-target="#msg-rply" style="cursor:pointer;" ><i class="glyphicon glyphicon-share-alt white"> </i></a> 
            </td>
        </a>

              <hr style=" margin-top:1px; margin-bottom:1px; " />
      </tr>
   </SelectedItemTemplate>
  </asp:ListView>

And this is how I tried to get the selected item

  public void selectedMsg(object sender, EventArgs e)
    {


    }

But when i use the linebreaker it dosen't get me to the above function.

Upvotes: 1

Views: 8104

Answers (1)

Michael B.
Michael B.

Reputation: 2809

First I will fix few issues with markup/HTML

       <asp:ListView ID="msg_list" runat="server" OnItemCommand="msg_list_ItemCommand">

            <ItemTemplate>
                <table>
                    <tr class="myitem">
                        <td style="border-bottom: #ccc 1px solid">
                            <asp:Label role="menuitem" ID="msg_lbl" runat="server" Text='<%#Eval("msg")%>' />
                            <i style="color: Gray;">from</i>
                            <asp:Label ID="tme" runat="server" Text='<%#Eval("name")%>' />
                            <i>on </i>
                            <asp:Label ID="tmelbl" runat="server" Text='<%#Eval("tme")%>' />
                            <asp:LinkButton ID="Link1" runat="server" CommandName="sel" CommandArgument='<%#Eval("name")%>'
                                Text="Select" />
                            <a id="msg-reply" class="btn button" data-toggle="modal" data-target="#msg-rply"
                                style="cursor: pointer;"><i class="glyphicon glyphicon-share-alt white"></i>
                            </a>
                        </td>
                    </tr>
                </table>
            </ItemTemplate>

        </asp:ListView>
        <asp:Label ID="TestLabel" runat="server" />
  • Don't put A tag inside A tag.
  • Don't mix Table/tr/td with Hr tags.
  • Fixed i tag without ending tag.
  • You could only use one template "ItemTemplate".
  • could use Styles/border as a separator.

For the selected, you could utilize the item command event by adding button/Linkbutton and use the command argument as in the sample.

protected void msg_list_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        TestLabel.Text = "index:" + e.Item.DataItemIndex.ToString() + ", Arg:" + e.CommandArgument;
    }

Upvotes: 2

Related Questions