Update Parent UpdatePanel from Child UpdatePanel - Conditionally - asp.net

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 ?

Related

ASP Timer causes update of Updatepanel every interval regardless of conditions

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.

Losing update panel trigger from databound gridview in tab container

I'm having some trouble getting a command link in a gridview to maintain it's ability to change tabs after the initial postback. So below you will see the structure of my content (heavily simplified):
<ajaxToolkit:TabContainer runat="server" ID="tabBody">
<ajaxToolkit:TabPanel runat="server" ID="tabPanel1">
<ContentTemplate>
<asp:UpdatePanel runat="server" ID="updPanel1">
<ContentTemplate>
<asp:Gridview runat="server" ID="grd1" OnRowCommand="grd1_RowCommand" OnRowDataBound="grd1_RowDataBound">
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkChangePanels" runat="server" CommandArgument='<%#Eval("id") %>' CommandName="gotopanel2" Text='<%#Eval("FirstName") & " " & Eval("LastName")%>' />
</ItemTemplate>
</asp:TemplateField>
</asp:Gridview>
</ContentTemplate>
</asp:UpdatePanel>
</ContentTemplate>
</ajaxToolkit:TabPanel>
<ajaxToolkit:TabPanel runat="server" ID="tabPanel2">
<ContentTemplate>
<asp:UpdatePanel runat="server" ID="updPanel2">
<ContentTemplate>
<asp:Gridview runat="server" ID="grd2">
</asp:Gridview>
</ContentTemplate>
</asp:UpdatePanel>
</ContentTemplate>
</ajaxToolkit:TabPanel>
</ajaxToolkit:TabContainer>
In order to fill the gridview on panel 1, there is a search box which the user types into and I call a function to bind a linq query to it.
Now I add the rowcommand as a postback trigger on rowdatabound:
Protected Sub grd1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim lb As LinkButton = CType(e.Row.FindControl("lnkChangePanels"), LinkButton)
If Not lb Is Nothing Then
ToolkitScriptManager1.RegisterPostBackControl(lb)
End If
End If
End Sub
Then here is the code I have to trigger the tab panel to change (and do some other stuff):
Protected Sub grd1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles grd1.RowCommand
Dim id = e.CommandArgument.ToString()
Select Case e.CommandName
Case "gotopanel2"
eventDetails(id, "C")
tabBody.ActiveTab = tabPanel2
End Select
End Sub
This causes a proper postback and changes the tab, everything works as intended. But if I go back to the first tab and try clicking another row in gridview 1, nothing happens.
Is there a way to structure this so the that either the tab can change without losing the postback trigger or am I going about this all wrong?
Thanks.
Postback trigger is not lost. Problem is caused by individual UpdatePanels in each tab.
Put entire TabContainer within UpdatePanel and you can remove UpdatePanels from tabs (but you don't have to). Make sure that UpdateMode of that new panel is set to "Always".
I think the reason why it does not change in your example is that UpdatePanel only refreshes it's own content and attribute that decides if tab is visible or not is set for div (tabPanel) outside that UpdatePanel. When you go back to tab with grid you do it client-side by clicking on it and that's when it goes wrong.
To get to the bottom of the problem and figure out why it does work during the first postback you would probably have to debug ajax toolkit javascript for TabContainer control.

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.

trigger controlID from user control page

I have the code below which looks like it should work. However, the trigger that references the control with the controlID of "gvSearch" is actually a gridview inside a user control page.
How can I access that gridview so that I can use it as a trigger?
Thanks!
<asp:UpdatePanel ID="pnlSearch" ChildrenAsTriggers="true" runat="server" >
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnSearch" />
<asp:AsyncPostBackTrigger ControlID="gvSearch" />
</Triggers>
</asp:UpdatePanel>
Thanks!
Outline:
<%# Page Title="test">
<%# Register src="test1.ascx" tagname="test1" tagprefix="test1uc" %>
<UpdatePanel>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnSearch" />
<asp:AsyncPostBackTrigger ControlID="gvSearch" />
</Triggers>
<ContentTemplate>
<test1uc:test1 ID="test1a" runat="server" />
</ContentTemplate>
</UpdatePanel>
What event do you want to use as trigger, the SelectedIndexChanged-event? Then apply the appropriate EventName.
<asp:AsyncPostBackTrigger ControlID="gvSearch" EventName="SelectedIndexChanged" />
However, that should already be the default event for GridView (see the remarks section).
Have a look at this thread: http://forums.asp.net/t/1136932.aspx
Update I think the best approach in your situation is to provide a custom event in your UserControl that is bubbled from the SelectedIndexChanged event from the GridView in the UC. Then you can use this event as AsyncPostBackTrigger for your UpdatePanel, e.g.:
<asp:AsyncPostBackTrigger ControlID="test1a" EventName="GridSearchClicked" />
Setting aside the fact that requiring access to a subcontrol is not good practice, here's my best suggestion...
Create a Public Sub in your user control as follows:
Private ParentUpdatePanel As System.Web.UI.UpdatePanel
' Must be called on every Page_Load!
Public Sub RegisterAsyncTrigger(MyScriptManager As System.Web.UI.ScriptManager, MyUpdatePanel As System.Web.UI.UpdatePanel)
MyScriptManager.RegisterAsyncPostBackControl(gvSearch)
ParentUpdatePanel = MyUpdatePanel
End Sub
On the Page_Load event, you'd call the function as follows:
Protected Sub Page_Load(Sender As Object, e As EventArgs)
' Call our new function, passing in the current ScriptManager and the UpdatePanel
' The ScriptManager handles the asynchronous postbacks
' The UpdatePanel handles the dynamic updates
test1.RegisterAsyncTrigger(ScriptManager.GetCurrent(Me), pnlSearch)
End Sub
In the event handler inside the gvSearch GridView control which must update the panel would contain the following code:
ParentUpdatePanel.Update()

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.

Resources