ASP.NET用户控件缓存造成的BUG一例

一个UserControl 中使用了缓存,如下:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ucIndexBureauMap.ascx.cs"
    Inherits
="Include_ucIndexBureauMap" %>
<%@ OutputCache Duration="60" VaryByParam="none" %>

在调用这个用户控件的页面里,在对这个uc的visible属性进行调整的时候,会出现“未……引用到实例”的错误。

protected void Page_Load(object sender, EventArgs e)
    {
        
try
        {
            Page.Title 
= PageBase.WebName;
            
//if (!Page.IsPostBack)
            {
                tgContentList.PageSize 
= PageBase.ModulePageSize;
                labLocation.Text 
= GetLocation();
                strModuleID 
= Request.QueryString["ModuleID"].ToString();
                UcNavBar1.strSelectedModuleID 
= strModuleID.Substring(04).Replace('A''B');
                
if (strModuleID == "A017001")
                {
                    UcBureauMap1.Visible 
= true;//在这里会出现错误,UcBureauMap1为空
                    tgContentList.Visible = false;//这个TableGrid就不会出错
                }
                
else
                {
                    UcBureauMap1.Visible 
= false;
                    tgContentList.Visible 
= true;
                    BindData();
                }
                
//InitSearchField(strModuleID);
            }
        }
        
catch
        {
            AlertMsn.PopMsn_Close(
0, PageBase.M_Error_Bad_Param, false);
        }
    }


但偶尔一两次进入引用的页面没有这样的问题,但等我再试就又出错了。

这是因为,在UserControl里设置了缓存,而我的页面根据传入的参数ModuleID来返回不同的页面,这两个页面的区别在于一个显示UcBureauMap1而另一个显示tgContentList.所以某时刻访问该页面时缓存了没有UcBureauMap的页面,下次换一个ModuleID参数传入时,缓存的页面中没有UcBureauMap这个对象,于是出错.

取消用户控件的缓存或者设置缓存的 VaryByParam 属性来解决问题。

posted @ 2008-01-30 19:08  Franking  阅读(968)  评论(0编辑  收藏  举报