【问题标题】:Access Selected Date from dynamically created Calendar Control asp/javascript/VB.net从动态创建的日历控件 asp/javascript/VB.net 访问选定日期
【发布时间】:2017-05-07 05:13:02
【问题描述】:

更新:对于仍然可能阅读此内容的任何人,我使用了一种完全不同的方式来做到这一点。使用 AjaxToolkit 的日历扩展器,我能够轻松地动态添加日历并使用 Javascript 更改不同日期输入的格式,以便日历扩展器可以读取它。如果您尝试做与我类似的事情,强烈建议您使用 AjaxToolkit 路线。

我为评论类动态创建了日历控件(asp:Calendar)、按钮和下拉列表。 Comment Class 将始终在其中包含一个文本框,但如果文本框的 ID/属性标签是 DOB 或生日或类似的东西,这些其他控件将动态创建。

到目前为止,日历、下拉列表和其中一个按钮都可以使用,但我在使用最后一个按钮时遇到了问题。目前,我动态添加了一个脚本,用于其中一个按钮来显示和隐藏包含该日历、下拉列表和相关按钮的 div,效果非常好。我正在尝试在按下按钮时添加另一个脚本触发器,以从日历中获取选定的日期并将其放入文本框中。

现在,我只是想访问日历。我正在使用如下代码:

function use_calendarDate(){
    var calendarDate = '<%=question128_Cal1.SelectedDate%>';
    alert(calendarDate);
}

我在另一个类似的问题中发现了这个函数,但它不是获取日期,而是将 作为字符串放入警报框中。

这个LINK 正是我想要做的事情,但我得到了不同的结果。谁能帮我解决我在这里做错的事情?

有趣的是,当我使用

var calendarDate = question128_Cal1.SelectedDate;

或

var calendarDate = question128_Cal1.value;

我的警告框告诉我未定义。

提前致谢。

如果需要,我的日历控件是这样创建的:

在 Page_Init 中

Dim calendar1 As New Calendar
Call BuildCalendar(calendar1)
calendarDiv.Controls.Add(calendar1)

这是上面引用的函数。

Private Sub BuildCalendar(ByRef calendar1 As Calendar)

    calendar1.ID = "Cal1"
    calendar1.SelectedDate = DateTime.Today
    calendar1.Attributes.Add("runat", "server")
    calendar1.Attributes.Add("OnClientDateChanged", "onDateChange")
    calendar1.Attributes.Add("borderwidth", "2px")
    calendar1.Attributes.Add("BackColor", "White")
    calendar1.Attributes.Add("width", "200px")
    calendar1.Attributes.Add("ForeColor", "Black")
    calendar1.Attributes.Add("Height", "180px")
    calendar1.Attributes.Add("Font-Size", "8pt")
    calendar1.Attributes.Add("Font-Names", "Verdana")
    calendar1.Attributes.Add("BorderColor", "#999999")
    calendar1.Attributes.Add("BorderStyle", "Outset")
    calendar1.Attributes.Add("DayNameFormat", "FirstLetter")
    calendar1.Attributes.Add("CellPadding", "4")
    calendar1.Attributes.Add("ShowNextPrevMonth", "True")
    calendar1.Attributes.Add("SelectionMode", "Day")
    calendar1.Attributes.Add("ShowTitle", "false")
    calendar1.Attributes.Add("OnSelectionChanged", "Calendar_SelectionChanged")
    calendar1.TodayDayStyle.ForeColor = Drawing.Color.Black
    calendar1.Attributes.Add("todaydaystyle-backcolor", "#cccccc")
    calendar1.Attributes.Add("selectorstyle-backcolor", "#cccccc")
    calendar1.NextPrevStyle.VerticalAlign = VerticalAlign.Bottom
    calendar1.Attributes.Add("dayheaderstyle-font-size", "7pt")
    calendar1.Attributes.Add("dayheaderstyle-font-bold", "true")
    calendar1.Attributes.Add("dayheaderstyle-backcolor", "#cccccc")
    calendar1.Attributes.Add("selecteddaystyle-font-bold", "true")
    calendar1.Attributes.Add("selecteddaystyle-forecolor", "White")
    calendar1.Attributes.Add("selecteddaystyle-backcolor", "#666666")
    calendar1.Attributes.Add("titlestyle-font-bold", "true")
    calendar1.TitleStyle.BorderColor = Drawing.Color.Black
    calendar1.Attributes.Add("titlestyle-backcolor", "#999999")
    calendar1.Attributes.Add("weekenddaystyle-backcolor", "#ffffcc")
    calendar1.OtherMonthDayStyle.BackColor = Drawing.Color.Gray

End Sub

【问题讨论】:

    标签: javascript asp.net vb.net calendar


    【解决方案1】:

    是 question128_Cal1 还是 question128_Calendar1 ?您的function use_calendarDate() 指的是第一个。错别字?

    有趣的是,当我使用

    var calendarDate = question128_Calendar1.SelectedDate;

    或

    var calendarDate = question128_Calendar1.value;

    我的警告框告诉我未定义。

    这是因为客户端脚本不知道“question128_Calendar1”是什么。那是一个服务器端 ID。

    您是否有理由不能使用标记来定义日历,而不是使用相当大的“BuildCalendar”函数? IMO,您这样做有点像维护噩梦。

    【讨论】:

    • 在程序的其他地方使用了 Comment 类。我不希望添加到我的页面的每个 Comment 控件的标记中都有日历。这涉及的软件允许用户为消费者创建在线调查和测试。如果他们决定在他们的测试中放置大量评论控件,我不想每次不需要时都隐藏或禁用日历。
    • 我之前使用 question128_divCal1.style.display 在单击按钮时隐藏或显示包含日历、下拉列表等的 div。所以我不明白为什么它也不能识别 question128_Cal1。
    • 对于仍然可能阅读此内容的任何人,我使用了一种完全不同的方式来执行此操作。使用 AjaxToolkit 的日历扩展器,我能够轻松地动态添加日历并使用 Javascript 更改不同日期输入的格式,以便日历扩展器可以读取它。如果您尝试做与我类似的事情,强烈建议您使用 AjaxToolkit 路线。
    猜你喜欢
    • 1970-01-01
    • 2013-04-20
    • 1970-01-01
    • 1970-01-01
    • 2011-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多