Force refresh image in update panel - asp.net

I have a button and an image in update panel. How do I force the image refresh by clicking on the button?
update
<b>Enter the code</b>
<asp:UpdatePanel runat="server">
<ContentTemplate>
<uc:TextBox ID="txtCaptcha" runat="server" />
<asp:Button ID="btnRefreshCaptcha" runat="server" Text="Refresh the code" CausesValidation="false" onclick="btnRefreshCaptcha_Click" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnRefreshCaptcha" EventName="Click" />
</Triggers>
</asp:UpdatePanel>

Add random value parameter to the end of your image url and change that on each update
kind of ImageUrl = baseUrl + "?" + new Random().Next()

Did you try
Sub btnRefresh_Click() Handles btnRefresh.Click
Me.Image.ImageUrl = "path to your image file"
End Sub

i used the above code but it shows the script problem in dropdown list what i gave it in master page and call them as a class files in every pages.After using the above code in my coding and i refresh the captcha it shows the overload of dropdownlist in my page and refreshment of captcha works only once after tis problem occurs i cant to refresh the captcha image in my page.

Related

update panel webcontrol required form field

I created a simple control which uses an update panel with a button click trigger and yes a script manager above the control. Click the button and a label is updated with the current time and this has worked fine for 6 years
Today, I changed the page to be responsive with Bootstrap but this is irrelevant to this question.
The control was added to a page which has labels and textboxes (simple form with first name / last name).
<asp:TextBox ID="txtFirstName" runat="server" CssClass="form-control input-lg" Placeholder="Your First Name" MaxLength="20" Required="true"></asp:TextBox>
If I remove:
Required="true"
and click the button in the control the date / time updates but placing this back stops the date / time updating. I need to use both as I wish for the first name to be required but also for the time to update
Simple Example:
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="lblTime" runat="server" Text=""></asp:Label>
<asp:Button ID="btnRefreshTime" runat="server" Text="Refresh Time" OnClick="btnRefreshTime_Click" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnRefreshTime" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
Code Behind:
Protected Sub btnRefreshTime_Click(sender As Object, e As EventArgs)
lblTime.Text = DateTime.Now.ToString()
End Sub
What am I doing wrong and what would I need to do to resolve it without opening the form up to abuse (script attacks) etc? I don't wish to add the following to the page directive:
ValidateRequest="false"
Thank you in advance
Probabily the button server side event is prevented to execute due to the client side validation of required = "true".
Try to change the button with somewhat else which does not trigger client side validation by design. I would try with a LinkButton.

Update Panel PostBackTrigger, Update Progress not displaying

I have an update panel and update progress with a PostBackTrigger Event. But update progress is not showing when i am clicking on the button. please find the below sample code
<asp:UpdateProgress ID="UpdateProgress1" runat="server" AssociatedUpdatePanelID="updatepanelDropDownTaskType" CssClass="Token-setup-popup" DynamicLayout="true">
<ProgressTemplate>
<div id="loading" class="loading">
<asp:Image runat="server" ID="imgBusyIndicator" ImageUrl="~/images/busy-indicator.gif" />
</div>
</ProgressTemplate>
</asp:UpdateProgress>
<asp:UpdatePanel ID="updatepanelDropDownTaskType" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:PostBackTrigger ControlID="btnExport" />
</Triggers>
<ContentTemplate>
<asp:Button ID="btnExport" runat="server" Text="Export To CSV" CssClass="button" CausesValidation="true" onclick="btnExport_Click" ClientIDMode="Static"/></asp:Button>
</ContentTemplate>
</asp:UpdatePanel>
My code behind
HttpResponse Response = System.Web.HttpContext.Current.Response;
Response.ClearHeaders();
Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName);
Response.ContentType = FileType;
Response.Write(content);
HttpContext.Current.ApplicationInstance.CompleteRequest();
Well Your code is Okay. Problem is with your Triggers which you are using in UpdatePanel.
Microsoft says
The UpdateProgress control renders a <div> element that is
displayed or hidden depending on whether an associated UpdatePanel
control has caused an asynchronous postback. For initial page
rendering and for synchronous postbacks, the UpdateProgress
control is not displayed.
See more details on MSDN
So you are using PostBackTrigger in your UpdatePanel which will cause a synchronous postback and UpdateProgress will not show up .
<Triggers>
<asp:PostBackTrigger ControlID="btnExport" />
// Incorrect
</Triggers>
Change it to
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnExport" />
// Correct
</Triggers>
And this will show up your UpdateProgress and will work as you are expecting.
As you have mentioned in your comments you are performing downloading as well on your Grid. Same way you could register your Button with ScriptManager. This will register your button and will aware of download button while asynchronous postbacks.
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Button btnExport = e.item.FindControl("btnExport") as Button;
if (btnExport != null)
{
((ScriptManager)this.Page.Master.FindControl("ID of your Script manager")).RegisterPostBackControl(downloadDocColumn);
// In Above line i assumed Script Manager is placed on Your master page.
}
}
}
Hope this helps...
I wrote an "Ajaxable Panel" (term explained in blog post) that may be helpful in your scenario.
http://leftyftw.wordpress.com/2011/10/01/an-ajaxable-panel/
It was geared towards SharePoint, but is perfectly applicable here.
Finally i acheived this task, with the help of iframe and endrequestHandler of sys.webforms. i explained clearly in my blog , please find the link http://bhuvanram.wordpress.com/. The Complete method has to be called in Iframe instead of calling in the same page
Try adding below to your web.config file under system.web
<xhtmlConformance mode="Transitional"/>
It may fix your problem if code is perfect.
Vote as answer if it fix your issue.

Cannot update an image using HttpHandler, UpdatePanel and a Timer

I'm trying in ASP.NET to use a HTTPHandler to display an image and update every 5 sec.
This httphandler simply renders the current time into a Bitmap.
In aspx side, the Image is inside a AJAX UpadtePanel, and I'm using a timer to refresh the image every 5 sec.
My problem is that :
in IE9, the image is not updated at all. My HttpHandler is requested only once.
in chrome, the image is updated but it "blinks", ie every 5s it is cleared for few secs then displayed, then cleared again etc...
For debugging purpose I have also added inside the UpdatePanel a label, filled by a random value. It is well updated in both web browsers.
Here is the code in my aspx page :
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:Timer runat="server" ID="UpdateTimer" Interval="5000" OnTick="UpdateTimer_Tick" />
<asp:UpdatePanel runat="server" ID="TimedPanel" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="UpdateTimer" EventName="Tick" />
</Triggers>
<ContentTemplate>
<asp:Image ID="TheImage" alt="httpHandler" src="getImage.ashx?id=1" runat="server" />
<asp:Label ID="Label" runat="server" Font-Size="XX-Large" ></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
In my HttpHandler, I did not forget to disable caching like this :
cache.SetCacheability(HttpCacheability.NoCache);
cache.SetNoStore();
cache.SetExpires(DateTime.MinValue);
And to prevent my IE9 web browser caching the image, I also tried to change ImageUrl each time the timer is invoked in the aspx.cs file, like following. But my HttpHandler is still called only once when the page is displayed for the first time :
protected void UpdateTimer_Tick(object sender, EventArgs e)
{
Label.Text = ((System.Environment.TickCount / 100.0) % 360).ToString("F2");
TheImage.ImageUrl = "getImage.ashx?id=" + System.Environment.TickCount.ToString();
}
I hope my problem is well explained.
What do I do wrong?
Thanks in advance!
Some suggestions:
Add Enabled="True" to your timer.
Add the source code to your getImage.ashx to your post.
Use the asp.net tags like ImageUrl in your <asp:Image /> instead of the native html tags.
Use a div instead of an img (i.e. <asp:Panel />)
change the update mode of your UpdatePanel to Always

ASP.NET Ajax Text Box with Button not firing

Hey just wondering if my syntax is wrong here
<asp:UpdatePanel ID="UpdatePanel2"
runat="server"
UpdateMode="Always">
<ContentTemplate>
<asp:textbox id="searchProductName" runat="server"></asp:textBox> <asp:Button ID="btnProductSearch" runat="server" Text="Search Product Name" CssClass="search" OnClick="ProductSearch_Click" UseSubmitBehavior="true" CausesValidation="false" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnProductSearch" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
and my OnClick method
Protected Sub ProductSearch_Click(ByVal sender As Object, ByVal e As EventArgs)
' Filter by ProductName
If searchProductName.Text.Length > 0 Then
srcProductListPerCustomer.FilterExpression = " (productName like '%" + searchProductName.Text.ToString & "%')"
productListTable.DataBind()
Else
srcProductListPerCustomer.FilterExpression = ""
productListTable.DataBind()
End If
End Sub
The problem is nothing is happening when I click on the button. The button works fine without the Ajax
Your button doesn't need to be in an UpdatePanel. The controls in a UpdatePanel should be the controls that are to be updated asynchronously. Put your UpdatePanel around the GridView you're updating, and use the AsyncPostBackTrigger in the same way.
It's best to keep UdpatePanels as small as possible; the fewer controls inside them, the less HTML will be sent back from the server (less bandwidth, faster request/response time). PostBackTriggers can refer to controls outside of the UpdatePanel with no problems.

Update Panel Triggers

Working on a page with multiple sections.
at the very top there is a "status" label.
beneath that is a section for adding new data...
beneath that is a section for updating data...
beneath that is a section for deleting data...
and...
beneath that is a section for viewing data... (repeater)
not even really concerned about the updating and deleting sections at this point... just stating that they are there so show the general layout of the page.
Now.. when I go to add new data, the submit button is set up as a trigger for an update panel that surrounds the repeater at the bottom of the page... that works perfectly... BUT its not removing the text from the text boxes or updating the status label because the page is only partialy posting back... (obviously)
when you click the button i also want the label to display ("You added data") and the text boxes to be emptied out... SOOO... i thought i'd be tricky and put an update panel around the status and the adding and setting their triggers to the same button... doesnt seem to work :-\ i usually dont bother with update panels... but this page has the potential to have A LOT of text data and formatting...
any ideas?
Figgured it out.
<asp:updatepanel id="updatepanel1" runat="server">
<contenttemplate>
<asp:label id="lblstatus" runat="server /> <br />
</contenttemplate>
<triggers>
<asp:asyncpostbacktrigger controlid="btnaddkey" eventname="Click" />
</triggers>
</asp:updatepanel>
<asp:updatepanel id="updatepanel2" runat="server">
<contenttemplate>
<asp:textbox id="tbxkeyname" runat="server />
<asp:textbox id="tbxkeytitle" runat="server />
<asp:textbox id="tbxkeyvalue" runat="server />
</contenttemplate>
<triggers>
<asp:asyncpostbacktrigger controlid="btnaddkey" eventname="Click" />
</triggers>
</asp:updatepanel>
<asp:button id="btnaddkey" runat="server" text="submit" OnClick="btnAddKey_Click" />
<asp:updatepanel id="updatepanel3" runat="server">
<contenttemplate>
<asp:repeater id="rptkeyview" runat="server">
...
</asp:repeater>
</contenttemplate>
<triggers>
<asp:asyncpostbacktrigger controlid="btnaddkey" eventname="Click" />
</triggers>
</asp:updatepanel>
Above is the basic layout of the page.... keep in mind that there is other content between each of the update panels... (i still need to add functionality to edit and delete as well)
With the btnaddkey click the following code occurs:
protected void btnAddKey_Click(object sender, EventArgs e)
{
Configuration toConfiguration = new Configuration();
toConfiguration.Title = tbxKeyTitle.Text;
toConfiguration.Name = tbxKeyName.Text;
toConfiguration.Value = tbxKeyValue.Text;
toConfiguration.AddKey();
lblStatus.Text = "New Key Added.";
BindKeys();
tbxKeyName.Text = "";
tbxKeyTitle.Text = "";
tbxKeyValue.Text = "";
}
Problem was that i need the labels and the text boxes (each in their own update panels) to all update on that one click....
using the above code it is working now
Are you saying you want multiple updatepanels on the same page?
If so see this

Resources