When I click my "Query" button, my TestID textbox TestIDTxt
retained its value, but both myDatePicker
's are blank without any value.
Question: How do I preserve StartDate.SelectedDate and EndDate.SelectedDate between postbacks?
Solution:
As suggested multiple times, removing ReadOnly="true"
on the DateTxt
kept the text value across postbacks. (Thanks Raj Kaimal!)
I then set the SelectedDate
(Date?) property by Date.Parse
'ing the text value automatically on Page_Load() for that user control see code below.
I never did use the ViewState object directly.
myDatePicker.ascx.vb (FINAL)
Imports System.Globalization
Partial Public Class myDate
Inherits System.Web.UI.UserControl
Public Property SelectedDate() As Date?
Get 'Simply return the date'
Return Me.DateTxt_CalendarExtender.SelectedDate
End Get
Set(ByVal value As Date?)
If value = Nothing Then Exit Property
Me.DateTxt_CalendarExtender.SelectedDate = value
Me.DateTxt.Text = Format(value, Me.DateTxt_CalendarExtender.Format)
End Set
End Property
Public Sub Refresh()
Dim tempDate As Date 'SelectedDate is nullable Date (Date?)'
Dim parseState As Boolean = False
parseState = Date.TryParseExact(DateTxt.Text, _
DateTxt_CalendarExtender.Format, _
DateTimeFormatInfo.CurrentInfo, _
DateTimeStyles.None, tempDate)
If parseState Then 'if successful, set the date'
Me.DateTxt_CalendarExtender.SelectedDate = tempDate
End If
End Sub
Private Sub Page_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
Me.Refresh()
End Sub
End Class
myDatePicker.ascx.vb (ORIGINAL)
Partial Public Class myDate
Inherits System.Web.UI.UserControl
Public Property SelectedDate() As Date?
Get
Dim o As Object = ViewState("SelectedDate")
If o = Nothing Then
Return Nothing
End If
Return Date.Parse(o)
End Get
Set(ByVal value As Date?)
ViewState("SelectedDate") = value
End Set
End Property
End Class
myDatePicker.ascx
<%@ Control Language="vb" CodeBehind="myDatePicker.ascx.vb"
Inherits="Website.myDate" AutoEventWireup="false" %>
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit"
tagprefix="asp" %>
<asp:TextBox ID="DateTxt" runat="server" ReadOnly="True" />
<asp:Image ID="DateImg" runat="server" ImageUrl="~/Calendar_scheduleHS.png"
EnableViewState="True" EnableTheming="True" />
<asp:CalendarExtender ID="DateTxt_CalendarExtender" runat="server" Enabled="True"
TargetControlID="DateTxt" PopupButtonID="DateImg"
DefaultView="Days" Format="ddd MMM dd, yyyy"
EnableViewState="True"/>
Default.aspx
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb"
Inherits="Website._Default" EnableEventValidation="false"
EnableViewState="true" %>
<%@ Register TagPrefix="my" TagName="DatePicker" Src="~/myDatePicker.ascx" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit"
TagPrefix="ajax" %>
<%@ Register Assembly="..." Namespace="System.Web.UI.WebControls"
TagPrefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
...
<body>
<form id="form1" runat="server">
<div class="mainbox">
<div class="query">
Start Date<br />
<my:DatePicker ID="StartDate" runat="server" EnableViewState="True" />
End Date
<br />
<my:DatePicker ID="EndDate" runat="server" EnableViewState="True" />
<br/>
<asp:TextBox ID="TestIdTxt" runat="server" /><br />
<div class="query_buttons">
<asp:Button ID="Button1" runat="server" Text="Query" />
</div>
</div>
<asp:GridView ID="GridView1" ... >
</form>
</body>
</html>
Default.aspx.vb
Imports System.Web.Services
Imports System.Web.Script.Services
Imports AjaxControlToolkit
Protected Sub Page_Load(ByVal sender As Object, _
ByVal e As EventArgs) Handles Me.Load
End Sub
Partial Public Class _Default
Inherits System.Web.UI.Page
Protected Sub Button1_Click(ByVal sender As Object, _
ByVal e As EventArgs) Handles Button1.Click
GridView1.DataBind()
End Sub
End Class