UpdatePanel Timer causes all panels to update

核能气质少年 提交于 2019-12-25 14:25:15

问题


I have an ASP.NET web form with several UpdatePanels and I would like to have a timer that triggers only one of these UpdatePanels to update. I then want a separate UpdatePanel to update on the OnSelectedIndexChanged event of a DropDownList. The code I have will update the timer-triggered panel just fine, but when I change the index, the drop down-triggered panel will wait until the next timer tick to update. How can I have the drop down-triggered panel update instantaneously? Code below.

C#:

public partial class WebForm2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Timer1_Tick(object sender, EventArgs e)
    {
        Label1.Text = "UpdatePanel1 refreshed at: " +
      DateTime.Now.ToLongTimeString();

    }

    protected void dropDown_indexChange(object sender, EventArgs e)
    {
        Label2.Text = "UpdatePanel2 refreshed at: " +
          DateTime.Now.ToLongTimeString();
        //UpdatePanel2.Update(); -- Didn't help
    }
}

ASP:

<form id="form1" runat="server">
<div>
    <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
    <br />
    <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
            <asp:Label ID="Label1" runat="server" Text="not updated yet"></asp:Label>
            <asp:Timer ID="Timer1" runat="server" Interval="3000" ontick="Timer1_Tick"></asp:Timer>
        </ContentTemplate>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
        </Triggers>
    </asp:UpdatePanel>
    <br />
    <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
            <asp:Label ID="Label2" runat="server" Text="also not updated yet"></asp:Label>
        </ContentTemplate>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="DropDownList1"  EventName="SelectedIndexChanged" />
        </Triggers>
    </asp:UpdatePanel>
    <br />
    <br />
    <asp:DropDownList OnSelectedIndexChanged="dropDown_indexChange" ID="DropDownList1" runat="server">
        <asp:ListItem>This</asp:ListItem>
        <asp:ListItem>That</asp:ListItem>
        <asp:ListItem>The Other</asp:ListItem>
    </asp:DropDownList>
</div>
</form>

回答1:


Move your DropDownList into an UpdatePanel and set .AutoPostBack = true on it.



来源:https://stackoverflow.com/questions/11634232/updatepanel-timer-causes-all-panels-to-update

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!