【问题标题】:ASP.NET Timer results in an updatepanel gives full postbackASP.NET 计时器导致更新面板提供完整的回发
【发布时间】:2011-01-19 22:18:50
【问题描述】:

我有一个非常奇怪的情况,我有以下代码:

<asp:Timer ID="GameClock" runat="server" Interval="5000" Enabled="true" 
    ontick="GameClock_Tick">
</asp:Timer>

 <asp:UpdatePanel ID="ItemsUpdatePanel" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false">
       <Triggers>
            NOTE THE TRIGGER IS COMMENTED OUT
            <%--<asp:AsyncPostBackTrigger ControlID="GameClock" EventName="Tick" />--%>
       </Triggers>
       <ContentTemplate>
           <asp:ListView ID="PlayerItems" runat="server" GroupItemCount="7" 
                                            onitemdatabound="PlayerItems_ItemDataBound">
                                            <LayoutTemplate>
                                                <table border="1" cellpadding="2" cellspacing="0" runat="server" id="tblProducts">
                                                    <tr runat="server" id="groupPlaceholder">

                                                    </tr>
                                                </table>        
                                            </LayoutTemplate>
                                            <ItemTemplate>
                                            <td id="Td1"  runat="server" style="vertical-align:top; text-align:left; height:100%;">
                                                <div>
                                                    <div id="category" runat="server">
                                                        <asp:Panel ID="ItemPanel" runat="server">
                                                        </asp:Panel>
                                                    </div>        

                                                </div>    
                                            </td>

                                            </ItemTemplate>

                                            <GroupTemplate>
                                                <tr runat="server" id="productRow">
                                                    <td runat="server" id="itemPlaceholder"></td>
                                                </tr>            
                                            </GroupTemplate>
                                        </asp:ListView> 
       </ContentTemplate>
 </asp:UpdatePanel>

这给出了一个非常奇怪的结果:我的整个页面每隔 5 秒就会进行一次完整的回发。当我在(激活)asyncpostbacktrigger 中发表评论时,更新面板不会给出完整的回发。

在 PlayerItems_ItemDataBound 我有以下代码(我认为这无关紧要):

protected void PlayerItems_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    if (e.Item.ItemType == ListViewItemType.DataItem)
    {
        ListViewDataItem dataItem = e.Item as ListViewDataItem;

        if (dataItem != null)
        {
            BaseItem myItem = dataItem.DataItem as BaseItem;
            Panel itemPanel = new Panel();
            Literal firstLiteral = new Literal();
            firstLiteral.Text += "<div id='smoothmenu1' class='ddsmoothmenu'>";
            firstLiteral.Text += "<ul>";
            firstLiteral.Text += "<li><img src='Images/Game/Items/" + myItem.ItemImageUrl + "' />";

            firstLiteral.Text += "<ul>";
            // Add all events bound to item into contextmenu

            itemPanel.Controls.Add(firstLiteral);
            foreach (Delegate del in myItem.Actions.Items)
            {
                Literal firstItLit = new Literal();
                firstItLit.Text += "<li>";
                itemPanel.Controls.Add(firstItLit);

                MethodInfo methodInfo = del.Method;
                string commandName = myItem.ItemId + "|" + methodInfo.Name;
                LinkButton btn = new LinkButton();
                btn.Text = methodInfo.Name;
                btn.Click += new EventHandler(btn_Click);

                btn.CommandName = commandName;
                itemPanel.Controls.Add(btn);

                Literal secondItLit = new Literal();
                secondItLit.Text += "</li>";
                itemPanel.Controls.Add(secondItLit);
            }
            Literal btnLiteral = new Literal();
            btnLiteral.Text += "</ul>";

            btnLiteral.Text += "</li>";
            btnLiteral.Text += "</ul>";
            btnLiteral.Text += "</div>";

            itemPanel.Controls.Add(btnLiteral);

            Panel panel = (Panel)(e.Item.FindControl("ItemPanel"));

            panel.Controls.Add(itemPanel);


        }

    }

}

当我创建一个新的更新面板 ItemsUpdatePanel1 时,它不会在没有计时器的情况下触发完整的回发。 我什至可以开始将项目从 ItemsUpdatePanel 复制到 ItemsUpdatePanel1,然后突然发生完整的回发。我分别尝试了 2 次,它们开始在不同的时间发生!!!

有没有大神赐教?我只是希望 UpdatePanel 不提供完整的回发,即使没有计时器。

谢谢!:)

【问题讨论】:

    标签: c# asp.net jquery updatepanel postback


    【解决方案1】:

    我找到了解决方案(或者,我在 ASP.NET 上询问并找到了):

    http://forums.asp.net/p/1644391/4262140.aspx#4262140

    包装解决方案效果非常好。

    【讨论】:

    • 节省了我的时间,感谢并为问题和解决方案 +1
    【解决方案2】:

    这个项目是从 ASP.NET 1.1 升级而来的吗?如果是这样,请检查您的 web.config 文件并从中删除 xhtmlConformance 标记。我忘记了所有细节,但是在 web.config 中包含该标记会导致这种情况发生,并且默认情况下存在于 .NET 1.1 Web 应用程序中。

    【讨论】:

    • 不 - 我在 .NET 4.0 中创建了应用程序。我还检查了 web.config,但标签不存在。太糟糕了!
    猜你喜欢
    • 1970-01-01
    • 2012-12-27
    • 1970-01-01
    • 2023-03-26
    • 2012-04-28
    • 1970-01-01
    • 1970-01-01
    • 2011-03-15
    • 1970-01-01
    相关资源
    最近更新 更多