简体   繁体   中英

How to know which item is selected in the ListView in asp.net

I have a Listview which I have used to display messages.Along with each message there is a link to "Reply" message.Since different user can send message to one user.The user have different message from different user.Now i need to track each user who are sending message.I was wondering if there is a way to know which message is selected so that the sender's id can be traced.

Here is my List view.

  <asp:ListView ID="msg_list" runat="server">
   <ItemTemplate>
    <table>
      <tr class="myitem">
        <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>

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

With this list view i display the messages.In the above code you can see this line <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 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>

So,when I click the icon a lightbox appears(modal of bootstrap) where i can write my message.

This lightbox has a button to send message(insert to database) which is as follows.

 public void reply_msg(object sender, EventArgs e)
    {
        string tym = DateTime.Now.ToString();
        string sid = Session["userid"].ToString();
        //string rid = ((Label)msg_list.FindControl("easy")).Text;
        //Label mr = (Label)msg_list.FindControl("reg_id_reply");
       // string rid = mr.Text;
        string rid = Session["msgreply"].ToString();

        //msg_list.Items.FindControl("easy");

        sc.connection();
        SqlCommand cmd = new SqlCommand("insert into message(msg,senderId,receiverId,tme) values( @msg,@sid,@rid,@tme) ", sc.con);
        cmd.Parameters.AddWithValue("@msg", ReplyMsgTb.Text);
        cmd.Parameters.AddWithValue("@sid", sid);
        cmd.Parameters.AddWithValue("@rid", rid);
        cmd.Parameters.AddWithValue("@tme", tym);

        cmd.ExecuteNonQuery();

        sc.con.Dispose();
        sc.con.Close();

    }

What i need is to know which item of ListView is selected when i click the icon that brings out the lightbox to send message.

OR is there any other way to know which item of Listview is selected.

You can use check box with every row. So when the user checks the check box for the particular row you can easily reply to the selected user.

your code will be something like this:-

      <asp:ListView ID="msg_list" runat="server">
      <ItemTemplate>
      <table>
      <tr class="myitem">
      <td>
      <asp:TemplateField HeaderText="Id" ItemStyle-HorizontalAlign="Left">
      <ItemTemplate >
      <asp:CheckBox ID="chkRow" onclick ="CheckSingleCheckbox(this)" AutoPostBack="true"  runat="server"  OnCheckedChanged="chkRow_CheckedChanged"/>

       </ItemTemplate>
       </asp:TemplateField>
      <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>

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

you can use the script given below for finding which check box has been checked:-

    <script type="text/javascript">
     function CheckSingleCheckbox(ob) {
         var grid = ob.parentNode.parentNode.parentNode;
         var inputs = grid.getElementsByTagName("input");
         for (var i = 0; i < inputs.length; i++) {
             if (inputs[i].type == "checkbox") {
                 if (ob.checked && inputs[i] != ob && inputs[i].checked) {
                     inputs[i].checked = false;
                 }
             }
         }
     }
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM