日历控件的疑问请澄清 [英] calendar control doubt please clarify

查看:106
本文介绍了日历控件的疑问请澄清的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

-我要在日历中突出显示假期列表

--i正在将对象引用未设置为对象的实例.

-:aspx页面..ondayrender在其中

--i want to highlight the holiday list in the calendar

--i am getting Object reference not set to an instance of an object.

--:aspx page..ondayrender is there

<asp:Calendar ID="clPopupCalendar" runat="server" Width="100%" CssClass="normalCalendar" Height="160px"
                       DayNameFormat="FirstLetter" ShowGridLines="True" OnDayRender="holidaylist" OnSelectionChanged="clPopupCalendar_SelectionChanged" >
                       <todaydaystyle forecolor="White" backcolor="Black"></todaydaystyle>
                       <SelectorStyle BackColor="Red"></SelectorStyle>
                       <nextprevstyle font-size="9pt" forecolor="#FFFFCC"></nextprevstyle>
                       <dayheaderstyle height="1px" cssclass="normalCalendarDayHeader"></dayheaderstyle>
                       <SelectedDayStyle CssClass="normalCalendarSelectedDay"></SelectedDayStyle>
                       <TitleStyle CssClass="normalCalendarTitle" BackColor="#6699CC"></TitleStyle>
                       <othermonthdaystyle forecolor="#FF0033"></othermonthdaystyle>






--code后面的代码






--code behind

 public partial class UserControls_PopUpCalendar : TMSWeb.TMSBasePage
{


    List<datetime> dtholidays = null;

....



 public List <datetime> GetPublicHolidays()
    {
  
        List <datetime> list = new List<datetime>();
        list.Add(new DateTime(2013, 02, 07));
        return list;

    }

......


protected void holidaylist(object sender, DayRenderEventArgs e)

    {

        if (dtholidays.Contains(e.Day.Date))

        {

            e.Cell.BackColor = System.Drawing.Color.Green;

 

        }



- 以上 - - - - - - - - - - - - - - - - - - - - - - - - ----------------------------------------------
---如果(dtholidays.Contains(e.Day.Date))
--i get
-对象引用未设置为对象的实例.


-对我来说似乎一切都很好....但是为什么会出错?
对象引用未设置为对象的实例.



--above---------------------------------------------------------------------------------------------
---if (dtholidays.Contains(e.Day.Date))
--i get
--Object reference not set to an instance of an object.


--it seems everyting fine for me....but why is thie error ?
Object reference not set to an instance of an object.

推荐答案

dtholidays 在您调用 GetPublicHolidays 方法
它必须返回应分配给假日的列表
就像
dtholidays = GetPublicHolidays();

在PagePage的PageLoad事件中设置
dtholidays may be null, when you called GetPublicHolidays method
It must return list which should be assigned to dtholidays
like
dtholidays = GetPublicHolidays();

Set in PageLoad event of Page


是的,此异常很明显,因为dtholidays为null.在使用变量之前,请检查是否为空.试试这个:
Yes this exception is obvious, because dtholidays is null. You check for null before using the variable. Try this:
if(dtholidays != null){
    if (dtholidays.Contains(e.Day.Date))
    {
        e.Cell.BackColor = System.Drawing.Color.Green;
    }
}



正如我看到的代码所示,dtholidays是类级别的变量,它将在每次回发时重新分配,并且它将变为null.更好的是,将此列表存储到会话中,并在需要时从会话中获取它.


-Amit



As I can see your code, dtholidays is class level variable and it''ll be reassigned on every postback and this will become null. Better, you store this list to session and whenever required fetch it from session.


--Amit


这篇关于日历控件的疑问请澄清的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆