如何在ListView中使UpdatePanel工作?

yel*_*ood 5 asp.net user-controls listview updatepanel

我有一个列表视图的页面,显示类似帖子的内容.在每个帖子上应该有一个"费率框",其工作方式类似于Facebook中的"赞"按钮.速率框是用户控件,其中包含更新面板.

如果我在页面中放置了一些随机值的控件它可以很好地工作 - 但是当我把它放在ListView中它应该位于的位置时,它将无法工作.正在调用该方法,但没有任何反应.

我简化了一些代码,以便更容易理解:

"价格框"控件:

    protected void OnRateClick(object sender, ImageClickEventArgs e)
    {
        Rate++;
        RateAmountLiteral.Text = Rate.ToString();
        RateButton.Visible = false;
        FeedbackLiteral.Visible = true;

        rateButtonPanel.Update();
    }
Run Code Online (Sandbox Code Playgroud)

ASCX:

<div class="rate_div">
    <asp:UpdatePanel ID="rateButtonPanel" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
            <fieldset>
                Rate:
                <asp:Literal ID="RateAmountLiteral" runat="server"></asp:Literal>
                <asp:ImageButton ID="RateButton" runat="server" ImageUrl="icn_rate.png"
                    OnClick="OnRateClick" />
                <asp:Literal ID="FeedbackLiteral" runat="server" Visible="false">Thanks for rating!</asp:Literal>
            </fieldset>
        </ContentTemplate>
    </asp:UpdatePanel>
</div>
Run Code Online (Sandbox Code Playgroud)

aspx(使用控件):

                     <asp:ListView ID="PostsView" runat="server" ItemPlaceholderID="itemPlaceHolder2"
                        <LayoutTemplate>
                            <div class="posts_div">
                                <asp:PlaceHolder ID="itemPlaceHolder2" runat="server" />
                            </div>
                        </LayoutTemplate>
                        <ItemTemplate>
                            <div class="post_div">
                                <div class="post_body">
                                    <%# CurrentPost.Body %>
                                </div>
                                <UC:RatingBox id="RatingBox" runat="server" 
                                PostID="<%# CurrentPost.ID %>"
                                Rate="<%# CurrentPost.Rate %>"/>
                                By: <a href="<%# CurrentPost.Author.LinkToProfile %>">
                                    <%# CurrentPost.Author.DisplayName %>
                                </a>&nbsp;|&nbsp;
                                <%# CurrentPost.LiteralTime %>
                            </div>
                        </ItemTemplate>
                    </asp:ListView>
Run Code Online (Sandbox Code Playgroud)

在调试时我注意到方法"OnRateClick"中的控件是空的,并且不包含正确的值.请指教.

如果你对我做事的方式有任何评论,也不要抱怨自己.

谢谢

小智 0

您可能没有设置很多东西,我无法仅从您提供的代码片段中看出。但请确保执行以下操作:-

1)在页面上放置一个ScriptManager“”。

如果您在应用程序中使用母版页并且您的网页也使用母版页,请将脚本管理器放置在母版页中。或者,您也可以将脚本管理器放置在特定的网页上。

2) 在更新面板中为按钮 RateButton 添加触发器。