开发者

Databound controls and UpdatePanel

I have the following code:

<asp:FormView ID="PhotoFormView" runat="server" DataKeyNames="PhotoID" DataSourceID="PhotoDataSource">
    <EmptyDataTemplate>
        No photo here.
    </EmptyDataTemplate>
    <ItemTemplate>
        <asp:Panel runat="server" Width='<%#PhotoController.FullPhotoSize.Width%>' Style="float: left;">
            <asp:UpdatePanel runat="server" UpdateMode="Conditional">
                <ContentTemplate>
                    <asp:Button ID="VoteButton" runat="server" OnClick="VotePhoto" />
                    <asp:Literal ID="VoteCountLL" runat="server" Text='<%#Bind("VoteCount")%>' />
                    likes <span style="float: right;">
                        <asp:Button runat="server" Text="Previous" />
                        <asp:Button runat="server" Text="Next" /开发者_运维知识库>
                    </span>
                </ContentTemplate>
            </asp:UpdatePanel>

In the VotePhoto method (in the code behind) invoked by the VoteButton button, I try to call DataBind on VoteCountLL, but I receive an InvalidOperationException with the message "Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.".

In English, here's what I am trying to do: I have a label showing the number of times a photo has been voted for. The button can be clicked to have the currently logged in user vote for the photo, thus increasing the vote count, which happens in the VotePhoto method. At the end of the VotePhoto method, it tries to call DataBind on the VoteCountLL literal to update the count, and it causes the aforementioned exception. How can I overcome this issue?


Instead of using <%#Bind("VoteCount")%> , setting the VoteCountLL value from DataBound event may help.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜