2011-04-26 44 views
3

我有這樣的一箇中繼器中:ASP.net設計簡單的問題

<div class="vote-wrapper"> 
    <asp:hyperlink runat="server" CssClass="s dislike-button" ID="CommentVoteDown" />       
    <div class="vote-total-bad"> 
     23 
    </div> 
    <asp:hyperlink runat="server" CssClass="s like-button" ID="CommentVoteUp" /> 
    <div class="clear"></div> 
</div> 

重複此操作(當然!)。由於我的項目設置,我沒有生成唯一的ID,並且自己管理它,原因是它使得在外部JS文件中調用DOM元素的引用變得更容易。

身份證作爲重複出現,全部爲CommentVoteDownCommentVoteUp。我想要做的,是讓它們被讀爲:

CommentVoteUp124 
CommentVoteDown124 

其中數字之後的數字表示與其關聯的註釋的ID。

我嘗試做:

UV.Attributes["ID"] += ThisComment.ID; 

UV.Attributes.Add("ID", "CommentVoteUp" + ThisComment.ID); 

但這些只是增加第二個鍵的HTML元素。我如何命名它們以便我可以在Jquery中引用它們?

+0

是什麼'UV'在這種情況下? – MikeM 2011-04-26 00:42:07

+0

做這些按鈕做任何形式的回發? – hunter 2011-04-26 00:42:48

+0

是的,除非你回來發佈,使用控件而不是常規的html錨不會給你任何東西。可能讓它運行得慢一點,因爲每個對象都有一個「新」對象。 – R0MANARMY 2011-04-26 02:37:34

回答

2

你就不能像

<asp:hyperlink runat="server" CssClass="s dislike-button" 
       ID='<%# "CommentVoteDown" + Eval("ID") %>' />  
2

這不工作?

UV.ID += ThisComment.ID; 

除非你做回發,你不必使用asp.net服務器控件和可能與清潔,簡單的HTML更好:

<% Comment comment = Container.DataItem as Comment %> 
<div class="vote-wrapper"> 
    <a class="s dislike-button" ID="CommentVoteDown<%=comment.ID %>" />       
    <div class="vote-total-bad"> 
     23 
    </div> 
    <a class="s like-button" ID="CommentVoteUp<%=comment.ID %>" />       
    <div class="clear"></div> 
</div> 
的只是使用

類和查找包裝與jQuery的包裝容器的ID

<% Comment comment = Container.DataItem as Comment %> 
<div id="CommentVoteUp<%=comment.ID %>" class="vote-wrapper"> 
    <a class="s dislike-button" />       
    <div class="vote-total-bad"> 
     23 
    </div> 
    <a class="s like-button" />       
    <div class="clear"></div> 
</div> 

然後使用jQuery,你可以找到從第

$(".like-button").click(function(){ 
    var id = $(this).closest(".vote-wrapper").attr("id"); 
}); 

$(".dislike-button").click(function(){ 
    var id = $(this).closest(".vote-wrapper").attr("id"); 
}); 
1

你可以嘗試將其添加到在元素上的CssClass:使用這種投票向上/向下<a>標籤è範圍是什麼?這可能會使它更容易。

CommentVoteUp.Attributes [「class」] =「[在此處添加您的班級名稱]」;