Damienn
Damienn

Reputation: 65

Updatepanel within an Update panel

I currently have an update panel within an Update panel. For example, I have a Master Page and everything inside the Master Page is within an updatepanel which I will name updatepanel1. The smaller update panel which is in an aspx page which uses the master page is called updatepanel2.

However I have a timer object and I only wish for updatepanel2 to be refreshed. However my entire page refreshes instead of just updatepanel2.

How exactly should I code it such that only updatepanel2 refreshes and not Updatepanel1 since I don't want the entire content in my master page to be refreshed.

Edit: Seems like the reason why it wouldn't refresh was because I left a refresh HTML meta tag in my page and forgot to remove it.

Upvotes: 1

Views: 11047

Answers (2)

shreyash gajbhiye
shreyash gajbhiye

Reputation: 190

MasterPage

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs" Inherits="WebApplication1.Site1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <div>
       <asp:ScriptManager ID="ScriptManager1" runat="server">
            </asp:ScriptManager>             
            <asp:UpdatePanel ID="UpdatePanel1" runat="server"  UpdateMode="Conditional">
            <ContentTemplate> <asp:Label  Id="lblmaster" runat="server" Text="Label"></asp:Label>
               <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server"> </asp:ContentPlaceHolder>
            </ContentTemplate>               
          </asp:UpdatePanel>     
    </div>
    </form>
</body>
</html>

ASPX PAGE

<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication1.WebForm2" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:Label ID="labelform" runat="server" Text="Label"></asp:Label>
          <asp:Timer ID="Timer1" runat="server" ontick="Timer1_Tick" Interval="10">
    </asp:Timer>
    </ContentTemplate>
    <Triggers>
    <asp:AsyncPostBackTrigger  ControlID="Timer1" EventName="Tick"/>
    </Triggers>

    </asp:UpdatePanel>

</asp:Content>

Codebehind of ASPX page

using System;
using System.Web.UI;
using System.Web.UI.WebControls;

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

        }

        protected void Timer1_Tick(object sender, EventArgs e)
        {
            labelform.Text = DateTime.Now.ToString();
         //  UpdatePanel1.Update();
            Label lblmaster = this.Master.FindControl("lblmaster") as Label;
            lblmaster.Text = DateTime.Now.ToString();
        }
    }
}

NOTES :

  1. Set Update Mode of both update panel to "Conditional"

  2. Add time control in updatePanel2

  3. Add AsynhronousTrigger inside UpadatePanel2 and set control id to timer and tick eventas EventName.

  4. Add your code in timer_tick evert in code behind and see.

the important point is here you have to do asynch call to server and for updating only particular potion of page set update mode of updatepane to Conditional and you have to set some condition for updating that particular part in trigger

Hope this can help you

Thank you keep coding.........! :)

Upvotes: 2

msm8bball
msm8bball

Reputation: 79

The documentation from Microsoft tells about the different situations which cause the UpdatePanel to update. I highly suggest you limit the usage of your update panel to only the controls that need it, instead of wrapping the entire page in it.

Or, even better, eliminate the UpdatePanels and use JQuery or Microsoft Ajax to update your content. It has fewer difficult side affects.

Upvotes: 0

Related Questions