代码如下:::
public partial class manage_aboutpages_Default : System.Web.UI.Page
{
    DataTable dt = new DataTable();   
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ShowNoticeInfo();
        }
    }    #region 显示所有公告信息
    protected void ShowNoticeInfo()
    {       
            dt = CommonCS.GetInfo("Notice_ShowAll");
         
            if (dt.Rows.Count == 0)//这里正确,可以得到正常值
            {
                msg.Visible = true;
                msg.Text = Message.RetMsg("noinfo", "");
                healthist.Visible = false;
            }                     }    protected void delFile(int id)
    {
        int rowsCount = dt.Rows.Count;//这里就不行了。总是0.dt是全局变量啊。怎么为0??
        //是否具有数据行
        if (rowsCount>0)
        {
            for (int i = 0; i < rowsCount;i++ )
            {
                //根据id取得附件文件地址
                if (id==Convert.ToInt32(dt.Rows[i]["id"]))
                {                    string filePath = Server.MapPath(dt.Rows[i]["filesaddress"].ToString());                    //文件是否存在,存在则删除
                    if (System.IO.File.Exists(filePath))
                    {
                        System.IO.File.Delete(filePath);
                    }                }
            }
        }
    }

解决方案 »

  1.   

    dt重新初始化
    使用ViewSate["dt"]保存数据
      

  2.   

    你确定dt没有在某处被释放?http://topic.csdn.net/u/20090605/11/35452c78-7c81-430d-81ab-031cf21cc2ae.html
      

  3.   

    分清网页的处理过程:
    Page ->Page_Load ->    ShowNoticeInfo (正确处理了DataTable,Rows.Count != 0)
    页面消亡!!!
    点击按钮提交 Page  (完全的一个新实例)->Page_Load ->  (没有执行ShowNoticeInfo )
    -> delFile ( 这里的Rows.Count肯定为0)
      

  4.   

    对于web都不在一个方法里面,肯定是null啦因为都重新初始化过,全局变量也没一点用,跟winform是不同的用viewstate["KeyNames"]=dt;保存,用的时候在取出 dt=(DataTable)viewstate["KeyNams"];
      

  5.   

    public static datatable dt=new datatable();
      

  6.   

    差点给楼上几位蒙了,你们用viewstate保存Datatable是行不通的,
    当datatable大到一定程度的时候,viewstate会截断数据;正确做法是重新提交查询
      

  7.   

     DataTable dt = new DataTable();  
    dt = CommonCS.GetInfo("Notice_ShowAll");
      protected void delFile(int id) 
        { 
            int rowsCount = dt.Rows.Count;//这里就不行了。总是0.dt是全局变量啊。怎么为0?? 
            //是否具有数据行 
            if (rowsCount>0) 
            { 
     
      

  8.   

    方法一:static DataTable dt = new DataTable(); 方法二:
        protected void ShowNoticeInfo() 
        {      
                dt = CommonCS.GetInfo("Notice_ShowAll"); 
                if(dt.Rows.Count!=0)          
                  ViewState["mydt"]=dt; 
       protected void delFile(int id) 
        { 
            if(ViewState["mydt"]!=null)
               dt=ViewState["mydt"] as DataTable;

            int rowsCount = dt.Rows.Count;//这里就不行了。总是0.dt是全局变量啊。怎么为0?? 
            //是否具有数据行 
            if (rowsCount>0)  
      

  9.   

    我的意思是:
    我现在已经把数据绑定到前台的repeater控件中了。控件中有一个字段,
    <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Delete" CommandArgument='<%# Eval("id") %>' OnClientClick="delone(); ">删除</asp:LinkButton>
    执行的删除操作。 #region 删除当前行记录
        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int id = Convert.ToInt32(e.CommandArgument);//这个方法中能不能得到两个参数啊。要是
    能够得到两个就好了。就不用上面的方法了。呵呵
            if (e.CommandName == "Delete")//要和aspx名称一样
            {
                try
                {
                    //删除附件文件
                    delFile(id);                CommonCS.del(id, "Notice_Delete");
                    //ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('删除成功');", true);
                }
                catch (System.Exception ex)
                {
                    string mess = Message.RetMsg("delfailerror", ex.ToString());                ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('" + mess + "');", true);
                }
                ShowNoticeInfo();
            }
        }