Why doesn't asp:UpdatePanel refresh an Image? - c#

I have the following UpdatePanel that gets an image from an ashx handler all of which works fine when the page is refreshed. However, when the timer fires, the label is refreshed with current time, but never the image.
<asp:UpdatePanel runat="server" id="TimedPanel" UpdateMode="Conditional">
<ContentTemplate>
<asp:Image ID="Image1" runat="server" Height="218px"
ImageUrl="~/getImage.ashx?cam=1" Width="303px" BorderWidth="10px" />
<asp:Timer ID="UpdateTimer" runat="server" interval="1250"
ontick="UpdateTimer_Tick" />
<asp:Label ID="DateStampLabel" runat="server" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger controlid="UpdateTimer" eventname="Tick" />
</Triggers>
</asp:UpdatePanel>
The timer routine is:
protected void UpdateTimer_Tick(object sender, EventArgs e)
{
DateStampLabel.Text = DateTime.Now.ToString();
}
Why isn't the image refreshed?

AJAX in general is really prone to browser caching. I normally add a DateTime.Now.Ticks to the URL. Also, your UpdateMode is Conditional, you have to call Update():
protected void UpdateTimer_Tick(object sender, EventArgs e)
{
DateStampLabel.Text = DateTime.Now.ToString();
Image1.ImageUrl += "&CacheBuster=" + DateTime.Now.Ticks.ToString();
TimedPanel.Update();
}

Related

Can't get timer event handler working

On page, I have UpdatePanel with Timer that works fine:
<asp:Content ID="content" ContentPlaceHolderID="contentholder" runat="server">
<asp:ScriptManager runat="server" EnablePartialRendering="true" ID="ScriptManager"></asp:ScriptManager>
<asp:Timer ID="Timer" runat="server" Interval="10000" ></asp:Timer>
<asp:UpdatePanel ID="MainUpdatePanel" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer" EventName="Tick" />
</Triggers>
<ContentTemplate>
<!-- some content, including repeaters... -->
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
I can't get event handler in code behind working though.
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
Timer.Tick += Timer_Tick;
}
}
private void Timer_Tick(object sender, EventArgs e)
{
_someFlag = true;
}
I can see during debug that Timer.Tick += happens just fine, yet _someFlag never changes its value and _someFlag = true; never happens despite page itself updating correctly. What am I missing?
You have to add OnTick="Timer_Tick" attribute to your timer:
<asp:Timer runat="server" id="Timer1" Interval="10000" OnTick="Timer_Tick"></asp:Timer>
And remove below code from Page_Load:
Timer.Tick += Timer_Tick;
Put 'Timer' inside 'Update panel' & 'Content Template' tag to handle trigger event

response.redirect not working within update panel using timer tick

I need to redirect page using a button within a update panel. but when I try this It didn't work for me. can anyone help me on this??/. Please Find My Code Below.
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<div>
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="Label2" runat="server" Text="Label" Width="345px"></asp:Label><br />
<br />
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button2" EventName="Click" />
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
</asp:UpdatePanel>
<asp:Timer ID="Timer1" runat="server" ontick="Timer1_Tick" Interval="500">
</asp:Timer>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" Width="344px" OnClick="Button1_Click" /><br />
<asp:Button ID="Button2" runat="server" Text="Button" />
<br />
<br />
</div>
</form>
</body>
My Code Behind Code
public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
}
public void imgBtn_Click(object sender, EventArgs e)
{
Response.Redirect("~/HotelResult.aspx");
}
protected void Timer1_Tick(object sender, EventArgs e)
{
Timer1.Enabled = false;
Label1.Text = DateTime.Now.ToString();
Label2.Text = DateTime.Now.ToString();
Label3.Text = DateTime.Now.ToString();
ImageButton testbtn = new ImageButton();
testbtn.ID = "testbtnid";
testbtn.Click += new System.Web.UI.ImageClickEventHandler(this.imgBtn_Click);
testbtn.ImageUrl = "images/book-btn.png";
PlaceHolder1.Controls.Add(testbtn);
}
}
The problem here is that ImageButton is added to the page only after timer tick. If you want server-side handlers to be invoked for such dynamically added controls, you have to added the to the page on every postback. Here is how it can be done using key view state:
protected void Page_Load(object sender, EventArgs ea)
{
bool? addHotelResultButton = ViewState["AddHotelResultButton"] as bool?;
if (addHotelResultButton.HasValue && addHotelResultButton.Value)
{
AddHotelResultButton();
}
}
protected void Timer1_Tick(object sender, EventArgs e)
{
ViewState["AddHotelResultButton"] = true;
AddHotelResultButton();
}
protected void AddHotelResultButton()
{
Timer1.Enabled = false;
Label1.Text = DateTime.Now.ToString();
Label2.Text = DateTime.Now.ToString();
Label3.Text = DateTime.Now.ToString();
ImageButton testbtn = new ImageButton();
testbtn.ID = "testbtnid";
testbtn.Click += new System.Web.UI.ImageClickEventHandler(this.imgBtn_Click);
testbtn.ImageUrl = "images/heroAccent.png";
PlaceHolder1.Controls.Add(testbtn);
}
I would do it like this :
Put your button inside your updatePanel and disable it
Put it as a trigger inside the <triggers> tag
When the tick event come, enable your button

Does RegisterStartupScript increase page size

I'm using this code in a page:
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:Timer ID="timer" Interval="4000" runat="server" OnTick="timer_Tick" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Panel ID="pnlAlarm" runat="server" CssClass="pnlAlarm" ClientIDMode="Static">
<div id="Alarm">
<asp:Label ID="lblContent" runat="server" Text="Updating" CssClass="AlarmLogo"></asp:Label>
ClientIDMode="Static" />
</div>
</asp:Panel>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="timer" />
</Triggers>
</asp:UpdatePanel>
and in code behind I use this simple code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["nima"] = 1;
}
}
protected void timer_Tick(object sender, EventArgs e)
{
int i = int.Parse(Session["nima"].ToString());
if (i==3)
{
lblContent.Text = i.ToString();
ScriptManager.RegisterStartupScript(this, GetType(), "AlarmMessage", "$('#pnlAlarm').slideToggle();", true);
Session["nima"] = 0;
}
else
{
i = i + 1;
Session["nima"] = i;
}
}
I want to know every time that I use RegisterStartupScript , $('#pnlAlarm').slideToggle(); add to my page and increase my page size?
thanlks
By definition, that method will:
register a startup script block that is included every time that an asynchronous postback occurs.
So yes, it will be included, and therefore increase your page size.
msdn ScriptManager.RegisterStartupScript Method

Imagebutton onclick not working in updatepanel, How can I make it work?

<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" ChildrenAsTriggers="true"
runat="server">
<ContentTemplate>
<asp:ImageButton ID="imagebutton1" runat="server" ImageUrl="~/image.jpg" Width="125"
Height="125" onclick="imagebutton1_Click" />
<asp:Timer ID="timer1" runat="server" Interval="10000" ontick="timer1_Tick" />
<asp:Label ID="label1" Visible="false" runat="server" Text="" />
</ContentTemplate>
</asp:UpdatePanel>
If I don't use an update panel the whole page refreshes.
protected void imagebutton1_Click(object sender, ImageClickEventArgs e){
string link = label1.Text;
Page.ClientScript.RegisterStartupScript(this.GetType(), "OPEN", "window.open(" + link +
",'mywindow','width=200,height=200,');", true);
}
This is the variable "link" here in the timer handler
Random r = new Random();
if (datatable1.Rows.Count > 0)
{
int randomnumber = r.Next(0, i);
DataRow datarow1= datatable1.Rows[randomnumber ];
imagebutton1.ImageUrl = (String)datarow1["image"];
imagebutton1.DataBind();
label1.Text = (String)datarow1["Link"];
}
I had to just add postback triggers as shown below in the updatepanel to make it work.
<Triggers>
<asp:PostBackTrigger ControlID="ImageButton"/>
</Triggers>
It's hard to say since you didn't really include a question, but if you want a piece of client side code to run when an UpdatePanel refreshes, use the client side pageLoad() method.
Also, you are using a server side event to do a client side operation. Use the OnClientClick handler instead:
<asp:ImageButton ID="imagebutton1" runat="server" ImageUrl="~/image.jpg" Width="125"
Height="125" OnClienClick="return openPopUp(this.href);" />
function openPopUp(link)
{
window.open(this.href,'mywindow','width=200,height=200,');
return false;
}
Try changing to UpdateMode="Always"

Force certain controls to do a full postback?

I have some controls within an UpdatePanel. Included are some buttons. How can I make these buttons so that they do a full postback rather than a partial AJAX postback?
Use a PostbackTrigger rather than an AsyncPostbackTrigger
Here's an example demonstrating how to use the PostbackTrigger instead of the AsyncPostbackTrigger:
ASPX Page:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="MyLabel" runat="server" />
<br/>
<asp:button ID="AjaxPostbackButton" Text="AJAX Postback" OnClick="AjaxPostbackButton_Click" runat="server" />
<asp:button ID="FullPostbackButton" Text="Full Postback" OnClick="FullPostbackButton_Click" runat="server" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="AjaxPostbackButton" />
<asp:PostBackTrigger ControlID="FullPostbackButton" />
</Triggers>
</asp:UpdatePanel>
Code Behind:
private void AjaxPostbackButton_Click(object sender, EventArgs e)
{
MyLabel.Text = "Ajax Postback: " + DateTime.Now;
}
private void FullPostbackButton_Click(object sender, EventArgs e)
{
MyLabel.Text = "Full Postback: " + DateTime.Now;
}
Clicking on the "AJAX Postback" button will will update the panel using AJAX whereas the "Full Postback" button will reload the entire page.

Categories

Resources