Link to home
Start Free TrialLog in
Avatar of kruegerste
kruegersteFlag for United States of America

asked on

Update Server Control Value inside User Control in OnChange Event after User Control reloaded in Page_Load Method

Hello,

To get right to it, I'm basically trying to update the value of a server control that is inside a User Control.  I trying to update it inside an OnChange event method that is fired when user changes the value.  

The Problem:  The OnChange event method is not hit until after the Page_Load method, in which, the user control has already been reloaded.  I don't know the value the user selected until the OnChange Event.  I am not able to get this value in the Page_Load anytime before the User Control initially reloads.

In the OnChange event I tried to remove the User Control, change the value and then reload it. But this gives me javascript errors on the Calendar control image (eWorld Calendar PopUp).  Should I be able to load the user control in the Page_Load, remove it and reload it again all in the same postback?  I have read that this should not be done.

I have included 4 snippets of code: the .ascx page and its code-behind, and the .aspx page and its code-behind.  

As the code stands now, the user control works fine and fires correctly, I'm just always one postback behind for the correct value since I'm not able to update it.

Please Help, Thanks.
Here is the code for the .ascx (user control UI) page:
 
 
1    <%@ Control Language="vb" AutoEventWireup="false" CodeBehind="ucCalendarPopUpAjax.ascx.vb" Inherits="Q3Web.Q3Web.UserControls.ucCalendarPopUpAjax" %>
2    <%@ Register Assembly="eWorld.UI.Compatibility, Version=2.0.6.2393, Culture=neutral, PublicKeyToken=24d65337282035f2" Namespace="eWorld.UI.Compatibility" TagPrefix="ewc" %>
3    <ewc:CalendarPopup ID="CalendarPopUpAjax" runat="server" AutoPostBack="True" OnDateChanged="CalendarPopUpAjax_DateChanged"></ewc:CalendarPopup>
 
 
Here is the code for the .ascx.vb (user control code-behind) page:
 
1    Partial Public Class ucCalendarPopUp
2            Inherits System.Web.UI.UserControl
3    
4            'Event Delegate created to hold Function's address that is to be assigned from the pages to the Custom Control
5            Public Delegate Sub OnChangeDelegate(ByVal sender As Object, ByVal e As EventArgs)
6    
7            'A Public Event to hold the function to be assigned to onchange event of Type Event Delegate, created above
8            Public Event DateChanged As OnChangeDelegate
9    
10           'Normal event which is fired when the actual CalendarPopUp textbox fires on DateChanged
11           Public Sub CalendarPopUpAjax_DateChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CalendarPopUpAjax.DateChanged
12               'Manually Raise the Event Change
13               RaiseEvent DateChanged(sender, e)
14           End Sub
15   
16           Private _SelectedDate As Date
17   
18           Public Property SelectedDate() As Date
19               Get 'Get Value
20                   Return _SelectedDate
21               End Get
22   
23               Set(ByVal Value As Date) 'Set Value
24                   _SelectedDate = Value
25               End Set
26           End Property
27   
28           Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
29   
30           End Sub
31       End Class
 
 
 
  Here is the code for the .aspx page:
 
1    <asp:UpdatePanel ID="udpDepartureDate" runat="server" UpdateMode="Conditional">
2         <ContentTemplate>
3                <asp:PlaceHolder ID="phDepartureDate" runat="server"></asp:PlaceHolder> 
4         </ContentTemplate>
5    </asp:UpdatePanel>
 
 
 
Here is the code for the .aspx.vb (code-behind) page:
 
       Method called in Page_Load: 
 
1    Public Sub LoadDepartureDateCalendarControl()
2            DepartureDateCalendar = CType(LoadControl("~/UserControls/ucCalendarPopUpAjax.ascx"), Q3Web.UserControls.ucCalendarPopUpAjax)
3    
4            DepartureDateCalendar.Enabled = True
5            DepartureDateCalendar.IsVisible = True
6            DepartureDateCalendar.ShowGoToToday = True
7            DepartureDateCalendar.Nullable = True
8    
9            If Not (hdnDepartureDate.Value = "") Then
10               DepartureDateCalendar.SelectedDate = CDate(hdnDepartureDate.Value)
11               DepartureDateCalendar.SelectedValue = CDate(hdnDepartureDate.Value)
12           End If
13   
14           AddHandler DepartureDateCalendar.DateChanged, AddressOf DepartureDateCalendar_DateChanged
15   
16           phDepartureDate.Controls.Add(DepartureDateCalendar)
17       End Sub
 
 
 
         Lastly, the code in the OnChange Event:
 
 
1        Public Sub DepartureDateCalendar_DateChanged(ByVal sender As Object, ByVal e As System.EventArgs)
2            Dim CalendarPopUpTemp As New eWorld.UI.Compatibility.CalendarPopup
3            Dim UserSelectedDate As Date
4    
5            'Instantiate Calendar Object
6            CalendarPopUpTemp = DirectCast(DepartureDateCalendar.FindControl("CalendarPopUpAjax"), eWorld.UI.Compatibility.CalendarPopup)
7    
8            'Get Date User Selected
9            UserSelectedDate = CDate(CalendarPopUpTemp.VisibleDate)
10   
11           'Assign Date to Hidden Field for Reloading Control
12           hdnDepartureDate.Value = CStr(UserSelectedDate)
13   
14           'Update Hidden Fields (wrapped in Update Panel)
15           udpHiddenFields.Update()
16   
17           'Clear controls in PlaceHolder before Reloading
18           'phDepartureDate.Controls.Clear()
19           'phDepartureDate.Controls.Remove(DepartureDateCalendar)
20   
21           'Reload Calendar Control since we have User Selected Value
22           'LoadDepartureDateCalendarControl()
23       End Sub

Open in new window

Avatar of jmwheeler
jmwheeler

If you change your LoadDepartureDateCalendarControl() routine slightly you should be able to utilize the ViewState and retrieve the value correctly.
Public Sub LoadDepartureDateCalendarControl()
2            DepartureDateCalendar = CType(LoadControl("~/UserControls/ucCalendarPopUpAjax.ascx"), Q3Web.UserControls.ucCalendarPopUpAjax)
3            phDepartureDate.Controls.Add(DepartureDateCalendar) 'Add the control here this will make the control playcatchup including loading information from the ViewState.
4            DepartureDateCalendar.Enabled = True
5            DepartureDateCalendar.IsVisible = True
6            DepartureDateCalendar.ShowGoToToday = True
7            DepartureDateCalendar.Nullable = True
8    
9            If Not (hdnDepartureDate.Value = "") Then
10               DepartureDateCalendar.SelectedDate = CDate(hdnDepartureDate.Value)
11               DepartureDateCalendar.SelectedValue = CDate(hdnDepartureDate.Value)
12           End If
13   
14           AddHandler DepartureDateCalendar.DateChanged, AddressOf DepartureDateCalendar_DateChanged
17       End Sub

Open in new window

Avatar of kruegerste

ASKER

Thanks but sorry, I didn't clarify one thing.  The problem with this is that I'm not sure the reason for the postback and I only want to change the value if it is from the DateChanged event postback.

The default for this Date Control is Date.Now so if I set the SelectedDate in the LoadDepartureDateCalendarControl everytime, it will load this default.  But if the user didn't select anything, it should stay null.  

Shouldn't I be able to set the value to the Control in the OnChange event, or is it too late in the Page life Cycle to do so?  
I don't think I am following you.  My suggestion was only a modification to how you dynamically add the control to the page and has nothing to do with changing the value.  If you add the control as I have shown then the value should be the one the user selected when you get to your "DepartureDateCalendar_DateChanged" code.
I have always been able to get the user selected value in my OnChange event, whether I load the control as I had it or the way you suggested.  Moving the add up 6 lines in the Load method has no affect on the values obtained in the Onchange event.

Re-Iteration of issue:

My issue is that once I get to the OnChange Event, I am unable to update the value of the control.  The statements:

 DepartureDateCalendar.SelectedDate = CDate(hdnDepartureDate.Value)
 DepartureDateCalendar.SelectedValue = CDate(hdnDepartureDate.Value)

do not work in the OnChange event.  So I need to know how I can get the user control updated in the ONChange event.



In "LoadDepartureDateCalendarControl()" make sure you assign an ID to the control

DepartureDateCalendar.ID = "DepartureDate"

Then in the OnChange event you need to find the control

Dim departureDate As Q3Web.UserControls.ucCalendarPopUpAjax
departureDate = phDepartureDate.FindControl("DepartureDate")
departureDate.SelectedDate = CDate(hdnDepartureDate.Value)
departureDate.SelectedValue = CDate(hdnDepartureDate.Value)
SOLUTION
Avatar of jmwheeler
jmwheeler

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
So you are saying that I need to use FindControl on the placholder that the user control was loaded into and then use FindControl again on that new object to find the specific server control in the user control?  

I think that is what you are suggesting.  I will try that and report back.  Thanks.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial