if(!Page.IsPostBack)
{
  ArrayList arrPersonList=new ArrayList();
}

解决方案 »

  1.   

    将ArrayList arrPersonList;
    改为ArrayList arrPersonList=new ArrayList();
      

  2.   

    你写在方法的外面就要加上作用范围private ArrayList arrPersonList;
      

  3.   

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Data.OleDb;
    using System.Data;
    using System.IO;namespace upload
    {
    /// <summary>
    /// write_ 的摘要说明。
    /// </summary>
    public class write_net : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.TextBox txtTo;
    protected System.Web.UI.WebControls.TextBox txtCent;
    protected System.Web.UI.WebControls.DropDownList listType;
    protected System.Web.UI.WebControls.DropDownList listDept;
    protected System.Web.UI.WebControls.DropDownList listPerson;
    protected System.Web.UI.WebControls.Button Button1;
    protected System.Web.UI.WebControls.CheckBoxList CheckBoxList1;
    protected System.Web.UI.WebControls.Button Button2;
    protected System.Web.UI.WebControls.Button btnAddFile;
    protected System.Web.UI.WebControls.Button btnDelFile;
    protected System.Web.UI.HtmlControls.HtmlInputFile upFile;
    protected System.Web.UI.WebControls.TextBox txtInfo;
    protected System.Web.UI.WebControls.CheckBox CheckBox1;
    protected System.Web.UI.WebControls.Button btnSend;
    protected System.Web.UI.WebControls.Button btnReset; private string fileDirPath;
    private string tempfileDirPath;


    private System.Collections.ArrayList arrPersonList=new ArrayList();
    protected System.Web.UI.WebControls.Button Button4;
    protected System.Web.UI.WebControls.Button btnRemoveRec;
    private System.Collections.ArrayList arrDeptList=new ArrayList(); private System.Data.OleDb.OleDbCommand comm;
    private System.Data.OleDb.OleDbConnection conn;

    private System.Data.DataSet DS_User;
    //private System.Data.DataSet DS_Dept; private string mailFrom;

    private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    if(!Page.IsPostBack)
    {
    this.OnLoad();
    }
    } #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.listType.SelectedIndexChanged += new System.EventHandler(this.listType_SelectedIndexChanged);
    this.listDept.SelectedIndexChanged += new System.EventHandler(this.listDept_SelectedIndexChanged);
    this.listPerson.SelectedIndexChanged += new System.EventHandler(this.listPerson_SelectedIndexChanged);
    this.btnAddFile.Click += new System.EventHandler(this.btnAddFile_Click);
    this.btnDelFile.Click += new System.EventHandler(this.btnDelFile_Click);
    this.btnSend.Click += new System.EventHandler(this.btnSend_Click);
    this.btnReset.Click += new System.EventHandler(this.btnReset_Click);
    this.btnRemoveRec.Click += new System.EventHandler(this.btnRemoveRec_Click);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion
    #region  功能代码 private void OnLoad()
    {
    string strConn="Provider=Microsoft.Jet.OleDb.4.0;Data Source=";
    //strConn=strConn+Server.MapPath("upload")+"\\db\\system.asa";
    strConn=strConn+"D:\\shi\\e\\mydocument\\My studio\\Project\\upload\\src\\db\\system.asa;User Id=;Password=;";
    this.conn=new System.Data.OleDb.OleDbConnection(strConn); this.DS_User=new System.Data.DataSet();


    conn.Open();
    System.Data.OleDb.OleDbDataAdapter da_depts=new System.Data.OleDb.OleDbDataAdapter("select  type from bumen",this.conn);
    System.Data.OleDb.OleDbDataAdapter da_persons=new System.Data.OleDb.OleDbDataAdapter("select 部门,姓名,用户名 from [user]",this.conn);
    da_depts.Fill(this.DS_User,"depts");
    da_persons.Fill(this.DS_User,"persons");
    conn.Close();
    this.fileDirPath="FILE\\";
    this.tempfileDirPath="TEMPFILE\\";
    this.arrPersonList=new ArrayList();
    this.arrDeptList=new ArrayList(); if(Request.Form["op"]!=null)
    {
    if(this.Request.Form["op"]=="1")//回复
    {
    }
    } if(Request.QueryString["from"]!=null)
    {
    this.mailFrom=Request.QueryString["from"];
    }
    else 
    {
    //Response.Redirect("error3.htm");
    } }
    //
    private string MakeNewName(string str0)
    {

    System.Random myRandom =new System.Random();
    int ii=myRandom.Next(10000);
    ii++;

    string str1=System.DateTime.Now.ToShortDateString();
    str1+=ii.ToString();
    string ex="";
    ex=str0.Substring(str0.LastIndexOf(".")); return str1+ex;
    } private string MakeOldName(string str)
    {
    string str1="";
    if(str.IndexOf("\\")>-1)
    {
    int start=str.LastIndexOf("\\");
    str1=str.Substring(start+1);
    }
    else
    {
    str1=str;
    } return str1; }
    private string Make_SQL_FileNames()
    { string str1=""; if(this.CheckBoxList1.Items.Count>0)
    {
    for(int i=0;i<this.CheckBoxList1.Items.Count;i++)
    {
    //System.Web.UI.WebControls.ListItem item=
    //mail_AddFilesName+=this.CheckBoxList1.Items[i].Value.ToString();
    string newFileName=this.MakeNewName(this.CheckBoxList1.Items[i].Value.ToString());
    string newFullName=this.fileDirPath+newFileName;
    System.IO.File.Copy(this.CheckBoxList1.Items[i].Value.ToString(),newFullName); str1+=newFileName;
    str1+=",";
    }
    }
    else
    str1="  ";
    return str1; }
    //得到原来的名字;去掉路径名;
      

  4.   

    //上传附件
    private void UploadFile()
    {
    if(this.upFile.PostedFile!=null)
    {
    try
    {
    string oldName=this.MakeOldName(this.upFile.PostedFile.FileName);
    string newName=this.MakeNewName(oldName);
    this.upFile.PostedFile.SaveAs(this.tempfileDirPath+"\\"+newName);
    this.CheckBoxList1.Items.Add(new System.Web.UI.WebControls.ListItem(oldName,newName)); }
    catch
    { } } } //删除文件
    private void DeleteFile()
    {
    for(int i=0;i<this.CheckBoxList1.Items.Count;i++)
    {
    System.Web.UI.WebControls.ListItem item =this.CheckBoxList1.Items[i];

    if(item.Selected)
    {
    string fileName=item.Value.ToString();
    if(File.Exists(fileName))
    {
    File.Delete(fileName);
    }

    this.CheckBoxList1.Items.Remove(item); item=null;
    }
    } } private bool SendMail()
    {

    string mail_DateTime="";//System.DateTime.Now.ToShortTimeString();
    string mail_from=this.mailFrom;
    string mail_cent=this.txtCent.Text.Trim();
    string mail_info=this.txtInfo.Text.Trim();
    string mail_read="f"; string mail_to=""; string ilevel="";

    //得到附件名
    string mail_AddFilesName="";

    string connMail="Provider=Microsoft.Jet.OleDb.4.0;Data Source=";
    connMail=connMail+"D:\\shi\\e\\mydocument\\My studio\\Project\\upload\\src\\db\\mail.mdb";
    //connMail=connMail+Server.MapPath("db\\mails.mdb");//+"\\db\\mails";
    System.Data.OleDb.OleDbConnection mailConn=new System.Data.OleDb.OleDbConnection(connMail);


    //生成sql
    string sql1="",sql="";

    //sql="INSERT INTO "+MailBox+TempName+"(iDateTime,iaddfile,"+FT+",iinfo,ilevel,cent,iread) VALUES ('"
    //sql=sql+iDateTime+"','"+iaddfile+"','"+ifrom+"','"+info+"','"+ilevel+"','"+cent+"','"+iread+"')"

    if(this.listType.SelectedIndex==1)
    {
    ilevel="0";
    //sql="insert into Message "
    //sql="INSERT INTO message(iDateTime,iaddfile,ifrom,iinfo,ilevel,cent,iread,dept) VALUES ('"
    //sql=sql+iDateTime+"','"+iaddfile+"','"+ifrom+"','"+info+"','"+ilevel+"','"+cent+"','"+iread
    //sql=sql+"','全体员工')" //先处理文件 mail_AddFilesName=this.Make_SQL_FileNames(); sql="insert into message(iDateTime,iaddfile,ifrom,iinfo,ilevel,cent,iread,dept) VALUES('";
    //sql+=System.DateTime.Now.to
    sql+=mail_DateTime+",'"+mail_AddFilesName+"','"+mail_from+"','"+ilevel+"','"+mail_cent+"','"+mail_read+",";
    sql+="','全体员工')"; this.conn.Open();
    this.comm.CommandText=sql;
    this.comm.Connection=this.conn;
    this.comm.ExecuteNonQuery();
    if(this.CheckBox1.Checked)
    {
    mail_AddFilesName=this.Make_SQL_FileNames(); //sql="INSERT INTO sendout"+Session("id")+"(iDateTime,iaddfile,ito,iinfo,ilevel,cent,iread) VALUES ('"
    //sql=sql+iDateTime+"','"+iaddfile+"','全体员工','"+info+"','"+ilevel+"','"+cent+"','"+iread+"')"
    sql1="insert info sendout"+mail_from+"(iDateTime,iaddfile,ito,iinfo,ilevel,cent,iread) values ('";
    sql1=sql+mail_DateTime+"','"+mail_AddFilesName+"','全体员工','"+mail_info+"','"+ilevel+"','"+mail_cent+"','"+mail_read+"')"; this.comm.CommandText=sql1;
    this.comm.Connection=this.conn;
    this.comm.ExecuteNonQuery(); }
    conn.Close(); }
    else if(this.listType.SelectedIndex==2)// 部门文件
    {
    ilevel="1";//部门不公开 this.conn.Open(); string mail_tos=""; for(int i=0;i<this.arrDeptList.Count;i++)
    {
    mail_to=this.arrDeptList[i] as string;
    mail_tos+=mail_to;
    mail_tos+=",";
    sql=""; mail_AddFilesName=this.Make_SQL_FileNames(); sql="insert into message(iDateTime,iaddfile,ifrom,iinfo,ilevel,cent,iread,dept) VALUES('";
    //sql+=System.DateTime.Now.to
    sql+=mail_DateTime+",'"+mail_AddFilesName+"','"+mail_from+"','"+ilevel+"','"+mail_cent+"','"+mail_read+",";
    sql+="','"+mail_to+"')";

    this.comm.CommandText=sql;
    this.comm.Connection=this.conn;
    this.comm.ExecuteNonQuery();
    } if(this.CheckBox1.Checked)
    {
    mail_AddFilesName=this.Make_SQL_FileNames(); mail_tos=this.txtTo.Text.Trim(); //sql="INSERT INTO sendout"+Session("id")+"(iDateTime,iaddfile,ito,iinfo,ilevel,cent,iread) VALUES ('"
    //sql=sql+iDateTime+"','"+iaddfile+"','全体员工','"+info+"','"+ilevel+"','"+cent+"','"+iread+"')"
    sql1="insert info sendout"+mail_from+"(iDateTime,iaddfile,ito,iinfo,ilevel,cent,iread) values ('";
    sql1=sql+mail_DateTime+"','"+mail_AddFilesName+"','"+mail_tos+"','"+mail_info+"','"+ilevel+"','"+mail_cent+"','"+mail_read+"')"; this.comm.CommandText=sql1;
    this.comm.Connection=this.conn;
    this.comm.ExecuteNonQuery(); } conn.Close(); }
    else if(this.listType.SelectedIndex==3)
    {
    ilevel="4";//个人,ilevel无意义
    string mail_tos="";
    this.conn.Open();
    for(int i=0;i<this.arrPersonList.Count;i++)
    {
    mail_AddFilesName=this.Make_SQL_FileNames();
    mail_to=this.arrPersonList[i] as string;
    mail_tos+=mail_to;
    mail_tos+=",";

    sql="insert into recived"+mail_to+"(iDateTime,iaddfile,ifrom,iinfo,ilevel,cent,iread) VALUES ('";
    sql=sql+mail_DateTime+"','"+mail_AddFilesName+"','"+this.mailFrom+"','"+mail_info+"','"+ilevel+"','";
    sql=sql+mail_cent+"','"+mail_read+"')"; this.comm.CommandText=sql;
    this.comm.Connection=this.conn;
    this.comm.ExecuteNonQuery(); } if(this.CheckBox1.Checked)
    {
    mail_tos=this.txtTo.Text.Trim();
    mail_AddFilesName=this.Make_SQL_FileNames(); sql="insert into sendout"+this.mailFrom+"(iDateTime,iaddfile,ito,iinfo,ilevel,cent,iread) VALUES ('";
    sql=sql+mail_DateTime+"','"+mail_AddFilesName+"','"+mail_tos+"','"+mail_info+"','"+ilevel+"','";
    sql=sql+mail_cent+"','"+mail_read+"')"; this.comm.CommandText=sql;
    this.comm.Connection=this.conn;
    this.comm.ExecuteNonQuery(); } } //comm.Excute sql;
    return true;
    }
      

  5.   




    private void OnSelectType()
    {
    if(this.listType.SelectedItem==null)
    return;
    /*
    if(this.listType.SelectedIndex==0)
    {
    this.Init_DeptItems();
    this.Init_PersonItems(); return;
    }*/
    this.arrPersonList.Clear();
    this.arrDeptList.Clear();
    this.CheckBoxList1.Items.Clear();

    this.txtTo.Text=""; this.Init_DeptItems();
    this.Init_PersonItems(); if(this.listType.SelectedIndex>1)//个人,部门;
    {
    this.Add_listDetpItem();
    }
    else if(this.listType.SelectedIndex==1)
    {
    this.txtTo.Text="公共文件";
    } } private void OnSelectDept()
    { if(this.listType.SelectedItem==null)
    return;
    if(this.listType.SelectedIndex==0)
    return; if(this.listDept.SelectedItem==null)
    return;
    if(this.listDept.SelectedIndex==0)
    return;
    if(this.listType.SelectedIndex==2)//部门
    {
    string dept=this.listDept.SelectedItem.Text;
    if(this.arrDeptList.IndexOf(dept)<0)
    {
    this.arrDeptList.Add(dept);
    this.txtTo.Text+=(dept+",");
    }
    }
    else if(this.listType.SelectedIndex==3)//个人文件
    {
    this.Add_listPersonItem();
    } }
    private void OnSelectPerson()
    {
    if(this.listType.SelectedIndex!=3)
    return;
    if(this.listPerson.SelectedItem==null)
    return;
    if(this.listPerson.SelectedIndex<1)
    return;

    string personId=this.listPerson.SelectedValue;
    string personName=this.listPerson.SelectedItem.Text; if(this.arrPersonList.IndexOf(personId)<0)
    {
    this.arrPersonList.Add(personId);
    this.txtTo.Text+=(personName+",");
    }
    } private void Init_DeptItems()
    {
    this.listDept.Items.Clear(); this.listDept.Items.Add(new System.Web.UI.WebControls.ListItem("==选择部门==","-1"));

    } private void Init_PersonItems()
    {
    this.listPerson.Items.Clear();

    this.listPerson.Items.Add(new System.Web.UI.WebControls.ListItem("==选择个人==","-1"));

    } private void Add_listDetpItem()
    {
    for(int i=0;i<this.DS_User.Tables["depts"].Rows.Count;i++)
    {
    this.listDept.Items.Add(new System.Web.UI.WebControls.ListItem(this.DS_User.Tables["depts"].Rows[i][0].ToString(),
    this.DS_User.Tables["depts"].Rows[i][0].ToString()));
    }
    }
    private void Add_listPersonItem()
    {
    if(this.listDept.SelectedItem==null)
    return; string dept=this.listDept.SelectedItem.Text.ToString(); for(int i=0;i<this.DS_User.Tables["persons"].Rows.Count;i++)
    {
    string str=this.DS_User.Tables["persons"].Rows[i][0].ToString();
    if(str==dept)
    {
    this.listPerson.Items.Add(new System.Web.UI.WebControls.ListItem(this.DS_User.Tables["persons"].Rows[i][1].ToString(),
    this.DS_User.Tables["persons"].Rows[i][2].ToString()));
    }

    }
    } #endregion private void btnAddFile_Click(object sender, System.EventArgs e)
    {
    this.UploadFile();
    } private void listDept_SelectedIndexChanged(object sender, System.EventArgs e)
    {
    this.OnSelectDept();

    } private void listPerson_SelectedIndexChanged(object sender, System.EventArgs e)
    {
    this.OnSelectPerson();
    } private void btnDelFile_Click(object sender, System.EventArgs e)
    {
    this.DeleteFile();
    } private void listType_SelectedIndexChanged(object sender, System.EventArgs e)
    {
    this.OnSelectType();

    } private void btnRemoveRec_Click(object sender, System.EventArgs e)
    {

    string str=this.txtTo.Text;
    string [] strs=str.Split(','); if(strs.Length>1)
    {
    this.txtTo.Text="";
    for(int i=0;i<strs.Length;i++)
    {
    this.txtTo.Text+=strs[i];
    this.txtTo.Text+=",";
    }
    }


    if(this.listType.SelectedIndex==2)//部门
    {
    int temp=this.arrDeptList.Count;
    if(temp>0)
    {
    this.arrDeptList.RemoveAt(temp-1); } }
    else if(this.listType.SelectedIndex==3)
    {
    int temp=this.arrPersonList.Count;
    if(temp>0)
    {
    this.arrPersonList.RemoveAt(temp-1);
    }
    }
    } private void btnSend_Click(object sender, System.EventArgs e)
    {
    this.SendMail();
    } private void btnReset_Click(object sender, System.EventArgs e)
    {
    this.arrPersonList.Clear();
    this.arrDeptList.Clear();
    this.CheckBoxList1.Items.Clear();
    this.listType.SelectedIndex=0;
    this.txtTo.Text="";
    this.txtCent.Text="";
    this.txtInfo.Text="";
    }

    }
    }