⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 accessorymodule.ascx.cs

📁 asp。net 2.0宝典一书源码 全书源码给大家共享
💻 CS
字号:
namespace qminoa.Webs.Msg.module
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;

	public abstract class AccessoryModule : System.Web.UI.UserControl
	{
		protected System.Web.UI.WebControls.TextBox txttAccessoryID;
		protected System.Web.UI.WebControls.TextBox txtAccessoryID;
		protected System.Web.UI.WebControls.Button btndelete;
		protected System.Web.UI.WebControls.DropDownList dopAccessory;
		protected System.Web.UI.WebControls.Label lblrace;
		protected System.Web.UI.WebControls.Label lblcont;
		protected System.Web.UI.WebControls.TextBox txtAccessory;
		protected System.Web.UI.WebControls.Label lblAccessory;
		protected System.Web.UI.WebControls.Button btnup;
		protected System.Web.UI.WebControls.Label lbltitle;
		protected System.Web.UI.WebControls.Label lblhead;
		protected System.Web.UI.HtmlControls.HtmlInputFile upaccessory;

		public string Accessory
		{
			get
			{
				return txtAccessory.Text;
			}
			set
			{
				txtAccessory.Text = value;
			}
		}

		public string AccessoryID
		{
			get
			{
				return txtAccessoryID.Text;
			}
			set
			{
				txtAccessoryID.Text = value;
				char [] seprator = new char [1];
				seprator[0] = ',';
				//得到各个附件的文件名
				string [] acc = txtAccessory.Text.Split(seprator,10);
				//得到保存在隐藏控件中的各个附件ID
				string [] accid = txtAccessoryID.Text.Split(seprator,10);
				for(int i = 0 ; i < acc.Length; i ++)
				{
					dopAccessory.Items.Add(new ListItem(acc[i],accid[i]));
				}
			}
		}
		private void Page_Load(object sender, System.EventArgs e)
		{
		}


		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		///		设计器支持所需的方法 - 不要使用
		///		代码编辑器修改此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.btnup.Click += new System.EventHandler(this.btnup_Click);
			this.btndelete.Click += new System.EventHandler(this.btndelete_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void btnup_Click(object sender, System.EventArgs e)
		{
			if(upaccessory.PostedFile.FileName != "")
			{
				string convert;
				string strFullPath=upaccessory.PostedFile.FileName;
				string filename=strFullPath.Substring(strFullPath.LastIndexOf("\\") + 1);
				//扩展名
				string extent=filename.Substring(filename.LastIndexOf(".")+1);
				//判断该文件是否为用户此次上传的第一个附件
				//附件名间以逗号隔开
				if(dopAccessory.Items.Count==1)
				{
					txtAccessory.Text+=filename;
					convert = DateTime.Now.ToString()+"."+extent;
					convert = convert.Replace(":","-");
					txtAccessoryID.Text += convert;
					//添加附件列表
					dopAccessory.Items.Add(new ListItem(filename,convert.ToString()));
					//将附件ID做为上传后的文件名
					filename=txtAccessoryID.Text;
				}
				else
				{
					txtAccessory.Text+=","+filename;
					convert = DateTime.Now.ToString()+"."+extent;
					convert = convert.Replace(":","-");
					txtAccessoryID.Text+=","+convert;
					dopAccessory.Items.Add(new ListItem(filename,convert.ToString()));
					filename=txtAccessoryID.Text.Substring(txtAccessoryID.Text.LastIndexOf(",")+1);
				}
				string path=Server.MapPath("file");
				upaccessory.PostedFile.SaveAs(path+"\\"+filename);
			}
		}

		private void btndelete_Click(object sender, System.EventArgs e)
		{
			//判断选中的项是否是提示项
			if(dopAccessory.SelectedItem.Value != "-1")
			{
				//得到所有附件的文件名和ID
				string fileString=txtAccessory.Text;
				string fileStringID = txtAccessoryID.Text;
				//得到待删除项的索引
				int index = dopAccessory.SelectedIndex;
				//得到选中项在附件列表中对应的位置
				int pos   = fileString.IndexOf(dopAccessory.SelectedItem.Text,0);
				int posid = fileStringID.IndexOf(dopAccessory.SelectedItem.Value,0);
				int length =dopAccessory.SelectedItem.Text.Length;
				//得到选中项的ID长度
				int lengthid = dopAccessory.SelectedItem.Value.Length;
				//判断列表中的数量是否大于2,如果是表示上传的附件大于等于两个
				if(dopAccessory.Items.Count>2)
				{
					//判断删除的项是否为第一项
					if ( 0 != pos)
					{
						//去掉前面的逗号
						fileString   = fileString.Remove(pos-1,length);
						fileStringID = fileStringID.Remove(posid-1,lengthid); 
					}
					else
					{
						fileString   = fileString.Remove(pos,length);
						fileStringID = fileStringID.Remove(posid,lengthid); 
					}
				}
				else
				{
					fileString   = fileString.Remove(pos,length);
					fileStringID = fileStringID.Remove(pos,lengthid); 
				}
				txtAccessory.Text   = fileString;
				txtAccessoryID.Text = fileStringID;
				dopAccessory.Items.Remove(new ListItem(dopAccessory.SelectedItem.Text,
					dopAccessory.SelectedItem.Value));
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -