Multiple Calendar Control Multiselectcalendar in User Control

xiaoxiao2021-03-06  57

statement of problem:

During the development system, I have encountered the situation of the employee, I think about the calendar control, I can use the calendar control. Intuitive and convenient. However, the calendar control in ASP.NET is radio, and each time you click on a date, you will trigger Postback. Each time postback's selectedDates property will be refreshed, and the control can be set to false AutoPostBack properties, So if you want to achieve the date, you can only write it yourself. After consideration, it is considered that this function is to be achieved, the key is to save each selected date collection, regenerating the elements in this collection after the page Postback, draw the style of the cell in the calendar.

The implementation of the idea is to save the selected date with a collection and remain in the session. Each time you click on a date, it is already a day in the test collection. If there is, it is deleted if it is not added. This will achieve the effect of clicking on the mouse click, the effect of clicking after clicking.

Implementation:

Create a web user control, named MultiSelectCalendar, drag into a normal calendar control in the design view, name Multicalendar, for aesthetics, select professional 2 in "Automatic Setting Format". The following is the source code.

Public Class MultiSelectcalendar

Inherits System.Web.ui.userControl

#Region "The code" of the web form designer "

'省 省

#End region

Private Coldt As SELECTEDDATESCOLLECTION

Private Sub Page_Load (Byvale AS System.Object, Byval E AS System.Eventargs) Handles MyBase.Load

'Place the user code of the initialization page here

IF not ispostback.

Coldt = Multicalendar.SelectedDates

Session ("Date") = Coldt

Else

Coldt = session ("Date")

END IF

End Sub

Private sub demultagendar_dayrender (Byval e as system.Web.ui.webcontrols.dayrenderEventArgs) Handles multicalendar.dayrender

DIM CTLCELL AS TABLECELL

DIM DT AS DATE

IF E.day.isothermonth Then

E.Cell.Controls.clear ()

END IF

If Coldt.Contains (E.day.date) THEN

E.cell.backcolor = color.fromargb (51, 51, 153)

E.Cell.ForeColor = color.white

Else

E.cell.backcolor = color.lightgray

E.Cell.Forecolor = color.black

END IF

End Sub

Private sub multicalendar_prender (Byval E AS System.EventArgs) Handles Multicalendar.PrenderEnderender

IF Ispostback Then

If not foldt.contains (multicalendar.selectedddate) thencoldt.add (multicalendar.selectedDate)

Else

Coldt.remove (Multicalendar.SelectedDate)

END IF

Session ("Date") = Coldt

END IF

End Sub

END CLASS

The variable Coldt is used to save the collection of selected dates. It can be seen that the implementation is relatively simple, it is entirely in the control, the method implemented in the method, without having to write additional functions yourself. If you set a breakpoint in three functions, you can see that the execution order of the three functions is page_load, multicalendar_prender, multicalendar_dayrender, and understanding the logical order is very important.

Finally, we can add a list box on .ascx to test it, modify the multicalendar_prender to the following, you can list all the date in the selected date collection in the list box.

Private sub multicalendar_prender (Byval E AS System.EventArgs) Handles Multicalendar.PrenderEnderender

IF Ispostback Then

IF not foldt.contains (multicalendar.selecteddddate) THEN

Coldt.add (multicalendar.selectedDate)

Else

Coldt.remove (Multicalendar.SelectedDate)

END IF

Session ("Date") = Coldt

END IF

DIM DT AS DATE

Listbox1.Items.clear ()

For Each DT in Coldt

Listbox1.items.add (dt.toString)

NEXT

End Sub

Note that in this function, the code is included in the intermediate of if ispostback, because when I use the list box to test, I found it to add the page when I open the page.

0001 years

January

1 day zero

This date, I don't know why, is it the default date of the calendar control?

to sum up:

The basic function of the multi-selection calendar control is taken into a web user control, which improves the reusability of the code, but there is also a place to improve. If you should expose the date collection into a read-only properties, you can also consider how some settings related to the appearance are exposed to properties, and how to implement it in the user control. Finally, it is best to be able to directly reference the control box.

转载请注明原文地址:https://www.9cbs.com/read-114926.html

New Post(0)