Raisolution
Raisolution

Reputation: 358

Dynamic Id property on items in ListView

I am developing a simple message board in ASP.NET Web forms and I list all posts in a ListView controller. My code looks something like this:

<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>

My problem is the voting functionality. I want to have css id properties, containing the unique database id of the post. That way I will know the Id of the post, it was voted for. So is this possible and if not, how can I achieve this?

Thanks!

Upvotes: 5

Views: 1897

Answers (1)

SCB
SCB

Reputation: 3084

You could use ClientIdMode and set it to predictable

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>

This assumes you are using .Net 4.0 or above.

Upvotes: 4

Related Questions