2

I have a listview with label which displays messages from different user.I have used the table row <tr> to get the id and pass it to the javascript.Now i need to pass this javascript variable to the .cs file.

<asp:ListView ID="msg_list" runat="server" OnItemCommand="selectedMsg" >
   <ItemTemplate>
     <table>
      <tr class="myitem" onclick=" selectedid(<%#Eval("senderId")%>) "/>
        <td>
         <asp:Label role="menuitem" ID="msg_lbl" runat="server" text='<%#Eval("msg")%>'/>
        </td>
       <tr>
     </table>

   </ItemTemplate>
</asp:ListView>

Here's the java-script

<script type="text/javascript">
function selectedid( id ) {
    alert(id);

}
</script>

The alert shows the selected row's id value.I need to pass this id value to .cs file.

4
  • I guess you could assign the id to Hidden Field. Commented Aug 7, 2014 at 5:13
  • If you don't want to do a form post, then Ajax? Commented Aug 7, 2014 at 5:19
  • Can you provide a sample example please. Commented Aug 7, 2014 at 5:21
  • There is a lot of documentation available. Commented Aug 7, 2014 at 5:33

1 Answer 1

2

You can use hidden field for that purpose.Looking at the code above it seems you have got your java-script variable.Now you can pass that value to the hiddenfield and access the hiddenfield value in the .cs file.

This is how it is done.

<asp:HiddenField ID="hdn" runat="server" />

<script type="text/javascript">
  function selectedid( id ) {
   var abc = id;
    document.getElementById('<%= hdn.ClientID %>').value = abc;
}
</script>

Now you can finally get your value in .cs file as string mystr = hdn.value.

Good luck

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.