ListView 中项目的动态 Id 属性

Rai*_*ion 5 c# asp.net webforms

我正在 ASP.NET Web 表单中开发一个简单的留言板,并在 ListView 控制器中列出所有帖子。我的代码看起来像这样:

<ItemTemplate>
   <article class="post">
       <div class="postinfo">
           <div class="postauthor">
               Author: <strong><%# Eval("Author") %></strong>
           </div>
           <div class="postdate">
               Date: <strong><%# Eval("PostDate", "{0:D}") %></strong>
           </div>
           <div class="postvotes">
               <asp:Button class="postupvote" id='up<%# Eval("Id") %>' runat="server" />
               <asp:Label ID="postvotescount_<%# Eval("Id") %>" class="postvotescount" runat="server" Text="<%# Eval("Votes") %> votes"></asp:Label>
               <asp:Button class="postdownvote" id='down<%# Eval("Id") %>' runat="server" />
           </div>
       </div>
       <div class="postcontent"><%# Eval("Text") %></div>
   </article>
</ItemTemplate>
Run Code Online (Sandbox Code Playgroud)

我的问题是投票功能。我想要有 css id 属性,包含帖子的唯一数据库 id。这样我就能知道该帖子的 ID,它已被投票。那么这可能吗?如果不可能,我该如何实现这一目标?

谢谢!

SCB*_*SCB 4

您可以使用 ClientIdMode 并将其设置为可预测

http://msdn.microsoft.com/en-us/library/system.web.ui.clientidmode.aspx

<asp:ListView ID="ListView1" 
              ClientIDMode="Predictable" 
              ClientIDRowSuffix="ProductID"  
              DataSourceID="XmlDataSource1" runat="server" >
<ItemTemplate>
</ItemTemplate>
</asp:ListView>
Run Code Online (Sandbox Code Playgroud)

这假设您使用的是 .Net 4.0 或更高版本。