domingo, 3 de junho de 2007

Codigo do Componente DropDownDate

Imports System

Imports System.Collections.Generic

Imports System.ComponentModelImports System.Text

Imports System.Web

Imports System.Web.UI

Imports System.Web.UI.WebControls

Imports System.Globalization

<defaultproperty(), DefaultEvent("MudouData"), ToolboxData("<{0}:DropDownDate runat=server></{0}:DropDownDate>")> _

Public Class DropDownDate

Inherits WebControl

Implements IPostBackDataHandler

Private _DataDiferente As Boolean= False
Private _DataAntiga As Date

Public Event MudouData(ByVal Sender As Object, ByVal e As DropDownDateEventArgs)
<Bindable(True), Category("Appearance"),DefaultValue(""), Localizable(True)>Property Value() As Date
Get
' Aqui estaremos recuperando o valor que foi gravada na viewstate
' o ctype e por que a viewstate só armazena texto
Dim s As Date = CType(ViewState("Value"), Date)
Return s
End
Get

Set(ByVal Value As Date)
'Aqui estamos gravando o valor de value na viewstate, 'a nome da variavel na viewstate pode ser qualquer texto, 'mas e bom que tenha o mesmo nome da propriedade que ela mapeia
MyClass
.DataDiferente = Not MyClass.Value =Value
If MyClass.DataDiferente Then
MyClass.DataAntiga = MyClass.Value
End If
ViewState("Value") = Value.ToString
End Set
End Property

Private
Property DataAntiga() As Date
Get
Return _DataAntiga
End Get
Set(ByVal value As Date)
_DataAntiga =value
End Set
End Property

Private
Property DataDiferente() As Boolean
Get
Return _DataDiferente
End Get
Set(ByVal value As Boolean)
_DataDiferente =value
End Set
End Property

Protected
Overrides Sub RenderContents(ByVal output As HtmlTextWriter)
'Um template dos options só para não ficarmos escrevendo toda hora.
Dim Options As String = "<option {0}>{1}</option>"
Dim Selecionado As String= String.Empty

'Definemos o div como a tag de rederização do componente.
output.RenderBeginTag(HtmlTextWriterTag.Div)

'Escrevendo o DropDown dos Dias
'Nome do DropDown do Dia no Cliente

output.Write("<select name=""" & MyClass.ClientID & "$dia"">") 'Nome do DropDown do Dia no Cliente
Dim i As Short
For i = 1 To 31
If i = MyClass.Value.Day Then
Selecionado = "selected"
Else
Selecionado = String.Empty
End If
output.Write(String.Format(Options, Selecionado, i.ToString))
Next
output.Write("</select>")

'Escrevendo o DropDown do Mes
'Nome do DropDown do Mes no Cliente

output.Write("<select name=""" & MyClass.ClientID & "$mes"">")
'Estaciamos um CultureInfo, baseado na cultura corrente da aplicação
'isto posibilitara que os meses sejam preenchidos no ediama para a qual a pagina esta preparada
Dim Cultura As New CultureInfo(CultureInfo.CurrentCulture.ToString)
For i = 1 To 12
If i = MyClass.Value.Month Then
Selecionado = "selected"
Else
Selecionado = String.Empty
End If
output.Write(String.Format(Options, Selecionado, Cultura.DateTimeFormat.GetAbbreviatedMonthName(i)))
Next
output.Write("</select>")

'Escrevendo a construção do DrowpDown do Ano
'Nome do DropDown do Ano no Cliente

output.Write("<select name=""" & MyClass.ClientID & "$ano"">")
For i = Date.Now.Year - 18 To Date.Now.Year + 100
If i = MyClass.Value.Year Then
Selecionado = "selected"
Else
Selecionado = String.Empty
End If
output.Write(String.Format(Options, Selecionado, i.ToString))
Next
output.Write("</select>")
output.RenderEndTag()
End Sub

Private Sub DropDownDate_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
Page.RegisterRequiresPostBack(Me)
ViewState("Value") = Date.Now.Date.ToString
End Sub

Public Function LoadPostData(ByVal postDataKey As String,ByVal postCollection As System.Collections.Specialized.NameValueCollection) As Boolean Implements System.Web.UI.IPostBackDataHandler.LoadPostData

Dim
stdia As String = postCollection(MyClass.ClientID & "$dia")
Dim stmes As String = postCollection(MyClass.ClientID & "$mes")
Dim stano As String = postCollection(MyClass.ClientID & "$ano")
MyClass.Value = CType(stdia & "/" & stmes & "/" & stano, Date)
Return MyClass.DataDiferente
End Function

Public Sub RaisePostDataChangedEvent() Implements System.Web.UI.IPostBackDataHandler.RaisePostDataChangedEvent
If MyClass.DataDiferente Then
Dim Arg As New DropDownDateEventArgs
Arg._DataAntiga = MyClass.DataAntiga
Arg._DataNova = MyClass.Value
OnMudouData(Me, Arg)
End If
End
Sub

Public Sub OnMudouData(ByVal Sender As Object, ByVal e As DropDownDateEventArgs)

MyClass
.DataDiferente= False
RaiseEvent MudouData(Me, e)
End Sub

Class
DropDownDateEventArgs
Inherits EventArgs
Friend _DataNova As Date
Friend _DataAntiga As Date
Public ReadOnly Property DataAntiga() As Date
Get
Return _DataAntiga
End Get
End Property
Public ReadOnly Property DataNova() As Date
Get
Return _DataNova
End
Get

End Property
End Class
End Class



Nenhum comentário: