我的.aspx文件中有以下代码片段,我希望按钮将ID发送到后面的代码。
<%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>当前背后的代码如下所示
protected void Add_Comment(object sender, CommandEventArgs e)
{
string uniqueNoteID = e.CommandArgument.ToString();
}命令参数被发送到Add_Comment方法,但它返回"<%:comment.NoteID%>“,而不是它表示的值。这种检索NoteID值的方法在设置div时工作得很好,因此我不知道它为什么会在commandArgument中引起问题。
发布于 2015-03-06 17:11:57
<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页面:
repeater1.DataSource = FindNotes(ParentNote.ID);
repeater1.DataBind();发布于 2015-03-06 17:04:07
不幸的是,您不能在<%%>控件中使用.NET代码块。这和事情呈现的顺序有关。在构建页面时,必须在后面的代码中设置CommandArgument。如果您使用的是中继器而不是页面中的每一个,那么您可以这样做。
https://stackoverflow.com/questions/28903786
复制相似问题