生成动态日历控件

发布于 2024-11-03 23:52:05 字数 743 浏览 0 评论 0原文

如何根据用户的选择生成动态控件?

我最近参加了一项实际测试,他们有下面提到的要求

  1. 他们有 2 个日历(asp.net 默认)。

  2. 他们有两个选项(单选按钮列表):一个是“重复开启”,另一个是“重复开启”

重复有 2 个下拉菜单:

1) 天下拉菜单:其值为:每隔一天、每隔一天

2) 持续时间下拉菜单:其值为:日、月、周、年

重复日期有 2 个下拉列表:

1) 周:值是第一周、第二周、第三周

2) 日:星期日、星期一 .... 星期六

现在,当我单击第一个日历的日期时比如 2010 年 8 月 10 日,第二个是 2011 年 1 月 26 日,那么我希望应该生成动态日历,该日历将显示从 8 月到 1 月(包括两者)的日历,

出于过滤目的,如果我选择第一个星期日(从 REPEATED ON 选项中) ) 那么应该选择动态日历的第一个星期日。如果我选择每三天,那么应该选择每三天(在动态生成的日历中)

我所做的是:可以通过创建日历类的对象来生成动态日历..是这样吗?另外,我在谷歌上搜索,他们表明 DayRender Event 可能是一个可能的解决方案,但这没有帮助......

如何做到这一点?如何生成动态日历?

如果问题没有被理解,请告诉我。

请给我类似的代码,至少

谢谢!

How to generate dynamic controls based on selecton by user?

I had appeared for one practical test recently where they had the below mentioned requirements

  1. They had 2 calendars (asp.net default).

  2. They had two options (radio button list) : One was "Repeat On" and another "Repeated On"

The Repeat on had 2 drop down :

1) Day Dropdown: It values were: Every, Every Other Day, Every Third Day

2) Duration dropdown: It values were: Day, Month, Week, Year

The Repeated on had 2 drop down :

1) Week: the values were First Week, Second Week, Third Week

2) Day: Sunday, Monday .... Saturday

Now when i click the first calendar's date as say August 10 2010 and second one as January 26 2011, then i wanted that dynamic calendars should be generated that would show the Calendars from August to January (inclusive of both)

For Filter purpose, if i select First Sunday (From REPEATED ON Option) then first sunday of dynamic calendars should be selected. If i select every third day then every 3rd day should be selected (in dynamic generated calendars)

What i did was : Could generate dynamic calendars by creating object of calendar class.. is that right? Plus i searched on google and they show that DayRender Event could be a possible solution but that didn't help...

How to do that? How to generate dynamic calendar?

Please let me know if question is not being understood.

Pass me on code similar to that the least

thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一桥轻雨一伞开 2024-11-10 23:52:05

听起来您想要做的是设置属性 Calendar.SelectedDateCalendar.VisibleDate

我不确定你到底想要什么,因为我不知道用户将从什么类型的控件中选择你的 1 月 1 日/6 月 1 日日期,或者第三个星期日与此有何关系。只有一本日历吗?如果以下信息对您没有帮助,请多解释一下。

我将粘贴我的一个应用程序中的一些 VB 代码,希望对您有所帮助。

Protected Sub calArrival_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles calArrival.SelectionChanged
    SetFormDepartureEntry()

    lblArrivalDate.Text = calArrival.SelectedDate.ToLongDateString
    calDeparture.SelectedDate = calArrival.SelectedDate
    calDeparture.VisibleDate = calArrival.SelectedDate
    dtSelectedArrival = "1/1/2000"

    'Determine if we need to see 2 months to view all possible departure dates.
    If (DatePart(DateInterval.Month, calDeparture.SelectedDate) <> _
        DatePart(DateInterval.Month, DateAdd(DateInterval.Day, 14, calDeparture.SelectedDate))) Then
        calDeparture2.Visible = True
        calDeparture2.SelectedDate = Nothing
        calDeparture2.VisibleDate = DateAdd(DateInterval.Month, 1, calDeparture.SelectedDate)
    Else
        calDeparture2.Visible = False
    End If
    End Sub

好吧,现在您已经发布了详细信息,这变成了另一个问题。您不知道需要多少个日历,因此您可能希望在代码中创建它们。我建议在标记中使用面板控件来包含所有可能的结果日历。然后,正如您之前所建议的,根据需要创建新的日历对象并将它们添加到“锚”面板的控件集合中。

Calendar objCal = new Calendar();
pnlResultsContainer.Controls.Add(objCal);

至于设置选定的日期,我相信每个日历只能有一个“选定”的日期。但是,您可以通过处理 Calendar.DayRender 事件来设置渲染样式以及是否启用该日期等。也许有人可以帮助我为 C# 中动态创建的控件设置处理程序?我觉得大概是这样的:

objCal.DayRender += AddressOf(DynamicDayRenderHandler());

如有不妥之处,请指正。

确定在处理程序中设置样式的正确日期将是针对 DayRenderEventArgs.Day 对象(该事件的参数)进行编码的问题。我不知道如何立即找到您正在寻找的特定日期/间隔,但通过以下文章中的一些研究应该可以实现:

如何:自定义日历 Web 服务器控件中的各个日期

DayRenderEventArgs.Day 属性

希望这有帮助。

Sounds like what you want to do is set the properties Calendar.SelectedDate and Calendar.VisibleDate.

I'm not sure exactly what you're going for since I don't know what type of control the user will be selecting your Jan 1/Jun 1 dates from, or what the 3rd sunday has to do with that. Is there only one calendar? Please explain a little more if the info below doesn't help you.

I'll paste some VB code from one of my apps, hopefully it will help you out.

Protected Sub calArrival_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles calArrival.SelectionChanged
    SetFormDepartureEntry()

    lblArrivalDate.Text = calArrival.SelectedDate.ToLongDateString
    calDeparture.SelectedDate = calArrival.SelectedDate
    calDeparture.VisibleDate = calArrival.SelectedDate
    dtSelectedArrival = "1/1/2000"

    'Determine if we need to see 2 months to view all possible departure dates.
    If (DatePart(DateInterval.Month, calDeparture.SelectedDate) <> _
        DatePart(DateInterval.Month, DateAdd(DateInterval.Day, 14, calDeparture.SelectedDate))) Then
        calDeparture2.Visible = True
        calDeparture2.SelectedDate = Nothing
        calDeparture2.VisibleDate = DateAdd(DateInterval.Month, 1, calDeparture.SelectedDate)
    Else
        calDeparture2.Visible = False
    End If
    End Sub

OK now that you have posted the details this becomes a different question. You don't know how many calendars you need to so you will probably want to create them in code. I would suggest using a Panel control in your markup to contain all the possible result calendars. Then as you suggested earlier, create new Calendar objects as needed and add them to the "anchor" Panel's Controls collection.

Calendar objCal = new Calendar();
pnlResultsContainer.Controls.Add(objCal);

As far as setting selected days, you can only have one "selected" day per calendar I believe. However you can set the rendering styles, and whether the day is enabled, etc., by handling the Calendar.DayRender event. Maybe someone can help me out on setting up a handler for dynamically created controls in C#? I think it's something like:

objCal.DayRender += AddressOf(DynamicDayRenderHandler());

Please correct if this is not right.

Determining the correct day to set styles on in the handler will be a matter of coding against the DayRenderEventArgs.Day object which is a parameter of the event. I don't know offhand how to find the specific days/intervals you are looking for but it should be possible with a little research in the following articles:

How to: Customize Individual Days in a Calendar Web Server Control

DayRenderEventArgs.Day Property

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文