DropDownList in usercontrol does not display new value

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I created a dropdownlist containing day, month and year field and expose a property to assign a date. When I call from a aspx page and assign the value, the new date is not displayed until a submit is performed.

How do I force the usercontrol to display the newly assigned date? I don't have this problem if I place the two dropdownlist and the year field directly on the aspx page.
 
On Thu, 10 Jun 2004 03:13:01 -0700, Benedict Teoh <Benedict
I created a dropdownlist containing day, month and year field and expose
a property to assign a date. When I call from a aspx page and assign
the value, the new date is not displayed until a submit is performed.

How do I force the usercontrol to display the newly assigned date? I
don't have this problem if I place the two dropdownlist and the year
field directly on the aspx page.

Need a little more info on this...when you say '...I call from a aspx page
and assign
the value, the new date is not displayed', can you explain the whole
scenario. From initial loading of the page the first time up to your
problem? Also, a little code snippet may help debug....

if you're still having problems...
 
this is the control and the code behind:

<%@ Control Language="vb" AutoEventWireup="false" Codebehind="cal.ascx.vb" Inherits="SHO.cal" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<asp:dropdownlist id="Dy" Width="40px" runat="server"></asp:dropdownlist><asp:dropdownlist id="Mth" runat="server"></asp:dropdownlist><asp:textbox id="Yr" Width="35px" runat="server"></asp:textbox><asp:label id="valDate" runat="server" CssClass="errMsg"></asp:label>

=============

Public Class cal
Inherits System.Web.UI.UserControl

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub
Protected WithEvents Dy As System.Web.UI.WebControls.DropDownList
Protected WithEvents Mth As System.Web.UI.WebControls.DropDownList
Protected WithEvents Yr As System.Web.UI.WebControls.TextBox
Protected WithEvents valDate As System.Web.UI.WebControls.Label

'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region
Dim loopcnt As Integer

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Dim loopCnt As Int16
Dim tmpStr As String
'If Dy.Items.Count = 0 Then
' filldate()
'End If

'valDate.Text = ""
If Not IsPostBack Then
filldate()
'Dy.SelectedIndex = Day(Now()) - 1
'Mth.SelectedIndex = Month(Now()) - 1
'Yr.Text = Year(Now())
'selcal = Now()
Else
Try
tmpStr = Date.Parse(Dy.SelectedValue & "-" & Mth.SelectedValue & "-" & Yr.Text)
'valDate.Text = "Valid"
Catch ex As Exception
valDate.Text += "<br>Invalid Date"
End Try

End If
End Sub

Private Sub filldate()
For loopCnt = 1 To 31
Dy.Items.Add(Format(New DateTime(Year(Now()), 1, loopcnt), "dd"))
Next
For loopCnt = 1 To 12
Mth.Items.Add(Format(New DateTime(Year(Now()), loopCnt, 1), "MMM"))
Next
Yr.Text = Year(Now())

End Sub

Public Function IsValid() As Boolean
If valDate.Text = "Valid" Then
IsValid = True
Else
IsValid = False
End If
End Function

Public Property selcal() As Date
Get
Try
valDate.Text += "<br>seldy is " & Dy.SelectedValue
valDate.Text += "<br>get val is " & Dy.SelectedValue & "-" & Mth.SelectedValue & "-" & Yr.Text
selcal = Date.Parse(Dy.SelectedValue & "-" & Mth.SelectedValue & "-" & Yr.Text)
'valDate.Text = "Valid"
Catch ex As Exception
valDate.Text += "<br>Invalid Date"
'selcal = Nothing
End Try
End Get
Set(ByVal Value As Date)
Dy.SelectedValue = Format(Value, "dd")
valDate.Text += "<br>dy is " & Format(Value, "dd")
Mth.SelectedValue = Format(Value, "MMM")
valDate.Text += "<br>Mth value is " & Format(Value, "MMM")
Yr.Text = Format(Value, "yyyy")
valDate.Text += "<br>yr val is " & Format(Value, "yyyy")
'Response.Write("<br>value is " & Value)
End Set
End Property

'Public Sub setCal(ByVal pDate As Date)
' Dy.SelectedValue = Format(pDate, "dd")
' Mth.SelectedValue = Format(pDate, "MMM")
' Yr.Text = Format(pDate, "yyyy")
' 'Response.Write("<br>value is " & Value)
'End Sub

Public Property enable() As Boolean
Get

End Get
Set(ByVal Value As Boolean)
If Not Value Then
Dy.Enabled = False
Mth.Enabled = False
Yr.Enabled = False
Else
Dy.Enabled = True
Mth.Enabled = True
Yr.Enabled = True
End If
End Set
End Property

End Class

================

This is the aspx and the code behind calling the code:

<%@ Register TagPrefix="uc1" TagName="cal" Src="includes/cal.ascx" %>
<%@ Register TagPrefix="uc1" TagName="FullRelJust" Src="UserControl/FullRelJust.ascx" %>
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="testDateform.aspx.vb" Inherits="SHO.testDateform"%>
<%@ Register TagPrefix="uc1" TagName="CalCtrl" Src="UserControl/CalCtrl.ascx" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>testDateform</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<LINK href="stylesheets/SHO2.css" type="text/css" rel="stylesheet">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<P>
<uc1:cal id="Cal1" runat="server"></uc1:cal></P>
<P>
<asp:DropDownList id="tDy" runat="server">
<asp:ListItem Value="01">01</asp:ListItem>
<asp:ListItem Value="02">02</asp:ListItem>
<asp:ListItem Value="03">03</asp:ListItem>
</asp:DropDownList></P>
<P>
<asp:Button id="Submit" runat="server" Text="Submit"></asp:Button></P>
<P>
<asp:Label id="msg" runat="server"></asp:Label></P>
</form>
</body>
</HTML>


=============

Public Class testDateform
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub
Protected WithEvents msg As System.Web.UI.WebControls.Label
Protected WithEvents Submit As System.Web.UI.WebControls.Button
Protected WithEvents tDy As System.Web.UI.WebControls.DropDownList

'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Protected WithEvents cal1 As cal

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Dim tmpDate As Date

msg.Text = ""

tmpDate = Date.Parse("20-dec-2003")

If Not IsPostBack Then
cal1.selcal = tmpDate
'CalCtrl1.selDate = tmpDate
'FullRelJust1.cDate1 = tmpDate
tDy.SelectedValue = "02"
Else
msg.Text += "<br>cal1 is " & Format(cal1.selcal, "dd-MMM-yyyy")
cal1.selcal = tmpDate
End If

msg.Text += "<br>selcal done"
msg.Text += "<br>setcal done"
msg.Text += "<br>tmpdate is " & Format(tmpDate, "dd-MMM-yyyy")

'msg.Text += "<br>fullreljust1 cal1 is " & Format(FullRelJust1.cDate1, "dd-MMM-yyyy")

End Sub

End Class

==============
As you can see I assign a value via the selcal property but the value does not get updated until I click the submit button to postback the form.
 
Back
Top