简体   繁体   English

使用<%%>的ASP.NET按钮CommandArgument

[英]ASP.NET button CommandArgument using <% %>

I have the following snippet of code in my .aspx file, I want the button to send the ID to the code behind. 我的.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>

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. 命令参数将发送到Add_Comment方法,但它返回“ <%:comment.NoteID%>”,而不是它所表示的值。 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. 设置div ID时,这种检索NoteID值的方法效果很好,因此我不知道为什么它在commandArgument中引起问题。

Unfortunately you can't use <%%> code blocks in .NET controls. 不幸的是,您不能在.NET控件中使用<%%>代码块。 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. 这与事物呈现的顺序有关。构建页面时,必须在后面的代码中设置CommandArgument。 You could do that if you built the page using a repeater rather than a for each in the page. 如果您使用中继器而不是页面中的每个中继器来构建页面,则可以执行此操作。

<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: .cs页面:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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