0

I have the following snippet of code in my .aspx file, I want the button to send the ID to the code behind.

<%List<Notes> CommentsList = FindNotes(ParentNote.ID); %>
        <%foreach (var comment in CommentsList) { %>
            <div id="note" class="tierTwo"><p><%: comment.Content %></p><ul><li><%: comment.AddedDate %></li><li>20:29</li><li><%: comment.AddedByName %></li><li><a href="#" onclick="showAddComment(addComment<%:comment.NoteID%>)" class="but">Add comment</a></li></ul> >
                <div id="addComment<%:comment.NoteID%>" class="tierOne" style="display:none">
                    <asp:TextBox ID="commentreplytext" runat="server" rows="4"> </asp:TextBox>
                    <asp:HiddenField id="ParentIdReply" value='<%=comment.NoteID%>' runat="server" />
                    <asp:Button ID="Button2" runat="server" Text="Add" CommandArgument="<%: comment.NoteID%>" OnCommand="Add_Comment" /> 
                </div>
            </div>

The code behind currently looks like this

 protected void Add_Comment(object sender, CommandEventArgs e)
 {
    string uniqueNoteID = e.CommandArgument.ToString(); 
 }

The command argument is getting sent to the Add_Comment method but it returns "<%: comment.NoteID%>" rather than the value it represents. This way of retrieving the NoteID value works fine when setting the div id so I don't know why it is causing an issue in the commandArgument.

1
  • why are you doint it like this? you can use repeater and bind data on ,cs page. Commented Mar 6, 2015 at 17:05

2 Answers 2

1

Unfortunately you can't use <%%> code blocks in .NET controls. It's to do with the order things get rendered in. You'll have to set the CommandArgument in the code behind when you build the page. You could do that if you built the page using a repeater rather than a for each in the page.

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

Comments

0
<asp:Repeater runat="server" id="repeater1">

<ItemTemplate>
<div id="note" class="tierTwo"><p><%# Eval("Content") %></p><ul>
<li><%# Eval("AddedDate") %></li><li>20:29</li>
<li><%# Eval(AddedByName") %></li><li>
<a href="#" onclick='showAddComment(addComment<%#Eval("NoteID")%>)' class="but">Add comment</a></li></ul> 
                <div id='addComment<%# Eval("NoteID") %>' class="tierOne" style="display:none">
                    <asp:TextBox ID="commentreplytext" runat="server" rows="4"> </asp:TextBox>
                    <asp:HiddenField id="ParentIdReply" value='<%#Eval("NoteID")%>' runat="server" />
                    <asp:Button ID="Button2" runat="server" Text="Add" CommandArgument='<%#Eval("NoteID")%>' OnCommand="Add_Comment" /> 
                </div>
            </div>
</ItemTemplate>
</asp:Repeater>

.cs page:

repeater1.DataSource = FindNotes(ParentNote.ID);
repeater1.DataBind();

1 Comment

Thank you, I have managed to get the correct value being send back to my .cs file using the repeater control. I should be able to adapt this approach for my needs. I am not familiar with asp.net applications which may explain my first approach.

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.