无法使用jquery在Asp.Net中设置日历控件中的日期

时间:2013-07-01 08:06:05

标签: jquery asp.net

我正在尝试使用带有asp.net日历控件的jquery来更改年份和月份,但我无法更改年份。可能是日历控制器属性存在问题。

以下是我的代码

$(document).ready(function () {

    var cyear = $('#<%=Ddyear.ClientID %>');
    var cmonth = $('#<%=Ddmonth.ClientID %>');

    var cal = $('#<%=Calendar1.ClientID %>');
    cyear.change(function () {
        var item = cyear.val();
        var item2 = cmonth.val();

        item = item + ',' + item2;
        $.ajax({
            type: "POST",
            url: "dashboardQuery.aspx/calanderdates",
            data: '{item:"' + item + '"}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {
                var sdate = Date.parse(response.d, "MM/dd/yyyy");
                dates = sdate;
                $('#<%=Calendar1.ClientID %>').attr('VisibleDate', sDate);
            },
            error: function () {
                alert(msg.status);
            }
        });
    });
}); 

1 个答案:

答案 0 :(得分:0)

您无法通过jQuery在ASP.Net日历上设置VisibleDate属性,它是服务器端属性,因此生成的HTML中没有VisibleDate属性。

你可以改变你的代码来改变服务器端的日期,这样的事情会起作用:

<asp:DropDown runat="server" ID="Ddmonth" OnSelectedIndexChanged="Ddmonth_SelectedIndexChanged" />

代码隐藏:

protected sub Ddmonth_SelectedIndexChanged(object sender, EventArgs e)
{
    // depending on what is in your drop down list will affect this code
    // this code assumes that you are listing the months by number, e.g. 1 for January, 2 for February, etc.
    int selectedMonth = int.Parse(DdMonth.SelectedValue);
    int monthsToAdd = selectedMonth - Calendar1.VisibleDate.Month;
    Calendar1.VisibleDate = Calendar1.VisibleDate.AddMonths(monthsToAdd);
}