如何让ListView中的UpdatePanel工作?

发布于 2024-09-12 00:20:29 字数 2706 浏览 2 评论 0原文

我有一个带有列表视图的页面,显示类似帖子的内容。每个帖子上都应该有一个“评分框”,其作用类似于 Facebook 中的“喜欢”按钮。费率框是一个用户控件,内部有一个更新面板。

如果我将带有一些随机值的控件放在页面中,它会很好地工作 - 但是当我将它放在 ListView 中(它应该位于的位置)中时,它就不起作用了。正在调用该方法,但没有任何反应。

我稍微简化了代码,使其更容易理解:

“速率框”控件:

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

        rateButtonPanel.Update();
    }

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>

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>

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

另外,如果您对我做事的方式有任何意见,请不要拘束。

谢谢

I have a page with a listview that shows something like posts. On each post there should be a "rate box" which works similar to the "Like" button in facebook. The rate box is a User Control, that has an update panel inside it.

If I put the control with some random values in the page it works great - but when I put it inside the ListView, where it should be located, it won't work. The method is being called, but nothing happens.

I simplified the code a bit to make it easier to understand:

The "rate box" control:

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

        rateButtonPanel.Update();
    }

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>

aspx (using the control):

                     <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> | 
                                <%# CurrentPost.LiteralTime %>
                            </div>
                        </ItemTemplate>
                    </asp:ListView>

While debugging I noticed the controls in the method "OnRateClick" are empty and don't contain the right values. Please advice.

Also if you have any comments about the way I did things, don't hold yourself.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荒岛晴空 2024-09-19 00:20:29

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

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

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

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

There are a lot things that you may not have set up, I cannot tell from just the code snippet you have given. But make sure you do the following: -

1) Place a ScriptManager "" on the page.

If you are using master page in your application and your web page uses the master page, place script manager in master page. Or alternatively,you can also place script manager on specific web pages anyway.

2) Add a Triggers for the button RateButton in your Update panel.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文