开发者

Does having more focused UpdatedPanels help versus just one big generic UpdatePanel around the entire page?

开发者 https://www.devze.com 2023-03-18 20:41 出处:网络
Does having more focused UpdatedPanels, around fewer 开发者_StackOverflow社区elements help versus just one big generic UpdatePanel around the entire page?

Does having more focused UpdatedPanels, around fewer 开发者_StackOverflow社区elements help versus just one big generic UpdatePanel around the entire page? What is the advantage of doing such a thing?


Having separate UpdatePanels will allow you to update isolated areas of a page, resulting in less downstream and potentially less server-side work.

For instance. If you have a page with a Label surrounded by an UpdatePanel, along with a GridViewsurrounded by an UpdatePanel, you could add a Timer that does a post to the server every 5 seconds and updates the Label to the current DateTime. When doing this, you would not have to re-bind your grid on the server side because the content of the grid is not involved in the downstream response.

The result would be: bind the Label and Grid on the first request. Refresh only Label on each Timer tick.

EDIT: Example added.

Markup:

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:Timer ID="Timer1" runat="server" Interval="5000" ontick="Timer1_Tick">
        </asp:Timer>
        <asp:Label ID="Label1" runat="server"></asp:Label>
    </ContentTemplate>
</asp:UpdatePanel>

<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:GridView ID="GridView1" runat="server"></asp:GridView>
    </ContentTemplate>
</asp:UpdatePanel>

CodeBehind:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        //Bind Grid, only on first load
    }
}

protected void Timer1_Tick(object sender, EventArgs e)
{
    Label1.Text = DateTime.Now.ToString();  //Set label
    UpdatePanel1.Update();  //Update only Label's update panel
}


Keep in mind that the UpdatePanel only tells the engine which control needs to be redrawn; the entire life cycle of the page still occurs.

This is not to discount the benefits of redrawing lesser elements.

0

精彩评论

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