ASP Timer causes update of Updatepanel every interval regardless of conditions - asp.net

UpdatePanel1 updates every 3 seconds regardless of conditions inside Timer1_Tick. I wanted to only update if x=1. Further explaining: If I completely remove the line UpdatePanel1.Update() and everything inside Timer1_Tick(), then UpdatePanel1 still updates every 3 seconds. The problem is not with where integer x is declared. I'd expect the problem to be within the ASP code.
<asp:ScriptManager runat="server" id="ScriptManager1" />
<div><asp:Timer runat="server" id="Timer1" Interval="3000" OnTick="Timer1_Tick" /></div>
<asp:UpdatePanel runat="server" id="UpdatePanel1" UpdateMode="Conditional" RenderMode="Block">
<Triggers><asp:AsyncPostBackTrigger ControlID="Timer1" /></Triggers>
<ContentTemplate>
<asp:Label id="Label1" runat="server"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
And the vb code..
Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs)
'If x=1 Then
'UpdatePanel1.Update() 'Update only if x=1
'Else
'Do nothing
'End If
'Regardless of what is in this subfunction, nothing or the above, Updatepanel updates every 3 seconds
End Sub
February 4th: UpdatePanel1 is still updating/reloading every 3 seconds nonconditionally. Problem is in the ASP code I believe. Answers or ideas are welcome.

Related

Ajax Update Panel - What am I missing?

Problem: When suburb drop down list value is changed - Page is posting back.
Desired result: Changing value in drop down list updates post code text box value without a page post back (post code text box is normally hidden)
Page code:
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:DropDownList ID="Suburb" runat="server" DataTextField="LocalityPhrase"
DataValueField="Locality" AutoPostBack="true" class="DropDown" OnSelectedIndexChanged="Suburb_SelectedIndexChanged"/>
<asp:UpdatePanel runat="server" id="UpdatePanelPostCode" UpdateMode="Conditional">
<ContentTemplate>
<asp:TextBox ID="Postcode" runat="server" Visible="true"/>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Suburb" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
Code behind:
Protected Sub Suburb_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Suburb.SelectedIndexChanged
'get postcode from suburb drop down
Dim pCode As String = ""
If Suburb.SelectedValue <> "" Then
pCode = Suburb.SelectedItem.Text.Substring(Len(Suburb.SelectedItem.Text) - 5, 4)
End If
Postcode.Text = pCode
End Sub
It appears there was something wrong with my install of the AJAX toolkit. Removed and reloaded it in Visual Studio (2008) and all is well.

How to customize calendar selection in ASP.NET

i'm trying to come up with a DateTime Picker in ASP.Net using textBox, Calendar, Button and Updatefields.
if a user clicks a button the panel with a hidden calendar will become visible, and if a user selects a date calendar's visibility will become false and he date will be assigned to textBox.
My problem comes if a user wants to change month because in this case calendar will disappear because selectionChange is being triggered.
My question is how can I fish out the changing of the month and bound the hiding of the panel only if a date is selected?
thank you very much in advance.
here are the code samples:
front part:
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:TextBox ID="TextBox2" runat="server" />
<asp:ImageButton ID="Imbut" runat="server" ImageUrl="imagesNew/calender.png" Height="17px" Width="17px" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Calendar1" EventName="SelectionChanged" />
</Triggers>
</asp:UpdatePanel>
<br />
<br />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Panel ID="Panel1" runat="server">
<asp:Calendar ID="Calendar1" runat="server" Height="129px" Width="209px">
</asp:Calendar>
</asp:Panel>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Imbut" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
back part
Protected Sub Imbut_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Imbut.Click
Panel1.Visible = True
End Sub
Protected Sub Calendar1_SelectionChanged(ByVal sender As Object, ByVal e As EventArgs) Handles Calendar1.SelectionChanged
TextBox2.Text = Calendar1.SelectedDate.ToString("dd/MM/yyyy")
Panel1.Visible = False
End Sub
I struggled with a similar problem. The problem is (as you stated) on the postback the control gets reset. I never found a means to do this elegantly with asp.net controls. I was able to accomplish this using the jQuery calendar and asp:hidden field. Keeping it client side via javascript I was able to allow the user to navigate the calendar change dates etc. When a date was selected the values were set to both a textbox within the ui as well as a hidden field then on submission, the value within the textbox or hidden field was picked up server side and used (in my situation) as a filter parameter.

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.

UpdatePanel async postback not updating content

I have an issue very similar to this question. There is a dropdown on my page that causes a postback, during which the ImageUrl property of an ASP:Image is changed. When that postback happens, any value that is in the FileUpload is cleared. That's the problem I'm trying to solve, but I ran into this issue in the process.
I'm trying to solve the problem by wrapping the dropdown and image in an UpdatePanel. Here is my ASP markup:
<asp:UpdatePanel ID="upPanel" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="myDropdown"
EventName="SelectedIndexChanged" />
</Triggers>
<ContentTemplate>
<tr valign="top">
<td>Tag:</td>
<td>
<asp:DropDownList ID="myDropdown" runat="server"
AppendDataBoundItems="true" DataTextField="Name"
DataValueField="ID" AutoPostBack="true">
<asp:ListItem Value="" />
</asp:DropDownList>
</td>
</tr>
<TR vAlign="top">
<TD width="150">Thumbnail:</TD>
<TD>
<asp:Image id="imgThumbnail" Runat="server"
AlternateText="No Image Found"
Visible="false"></asp:Image><BR>
</TD>
</TR>
</ContentTemplate>
</asp:UpdatePanel>
EDIT: my code-behind doing the update is here:
Private Sub myDropdown_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles myDropdown.SelectedIndexChanged
If (myDropdown.SelectedValue <> "-1") Then
imgThumbnail.ImageUrl = Application("AppPath") + "/Modules/Broadcaster/ImageGen.ashx?tag=" + myDropdown.SelectedValue
Else
imgThumbnail.ImageUrl = Application("AppPath") + "/Modules/Broadcaster/ImageGen.ashx?defaultTag=" + _modID.ToString()
End If
End Sub
I can see the async postback happening in FireBug, but the image URL does not change. What am I doing wrong?
You're missing the code that's doing the update (the code that is called by the selected index changing within the dropdown); however, I'm going to venture a guess that your problem is being caused because you are loading the DDL through a control instead of programmatically.
The reason you may be running into this issue is because the page load function is called before the datasource controls are populated, which means that the DDL is not populated by the time you are looking for a value, thus your image is coming up with a blank.
Example:
Dim sTemp As String = "images/myimagenumber" & myDropdown.SelectedIndex & ".jpg"
This will return "images/myimagenumber.jpg" as the value of the sTemp string because there is no value or index selected the the moment the page loads.
I suggest you load the values of the dropdownlist manually (programmatically) and then in the page_load subroutine make sure that it's only repopulating the dropdown when the page loads for the first time.
VB.Net Example:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
If Page.IsPostback = False Then
runDBLFillSubHere()
End If
'Run Rest of Code Here'
Sub
I've run into this a couple times over the years and it always ends up being because the DDL isn't populated before I am accessing it.

Update Parent UpdatePanel from Child UpdatePanel - Conditionally

I am trying to trying to setup an updatepanel to update every X seconds, the problem is I don't want the control to actually refresh unless there is new data. So I currently have a child updatepanel in a parent UpdatePanel, the child updatepanel gets refreshed by a timer. But I can't seem to find a way to trigger the parent panel to update. Again, only when certain conditions(data changed) are met.
Sample Code:
<asp:Timer ID="Timer1" OnTick="Timer1_Tick" runat="server" Interval="10000"></asp:Timer>
<asp:updatepanel id="upParent" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label id="lblParenttime" Runat="server">Parent Time Here</asp:Label>
<asp:updatepanel id="upChild" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label id="lblChildtime" Runat="server">Child Time Here</asp:Label>
</ContentTemplate>
<triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</triggers>
</asp:updatepanel>
</ContentTemplate>
</asp:updatepanel>
Protected Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
lblChildtime.Text = Now.Tostring
if X = Y then
'Cause Parent to Update
lblParenttime.Text = Now.Tostring
end if
End Sub
You can trigger some javascript when you UpdatePanel refreshes with the following javascript code:
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_pageLoaded(YourJavascriptFunctionNameHere);
You could then put a hidden button on the other UpdatePanel and manually call the __DoPostBack from the javascript function you have hooked onto the UpdatePanel reload.
There is probably other ideas for how to leverage add_pageLoaded in your scenario but this should at least get you on the right track.
Have you tried upParent.Update() in the Timer1_Tick event when conditions are met ?

Resources