?? file.aspx.cs
字號:
using System;
using System.IO;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace Web.SysAdmin
{
public class File : System.Web.UI.Page
{
protected System.Web.UI.WebControls.ImageButton BtnQuery;
protected System.Web.UI.WebControls.Label LabRowCount;
protected System.Web.UI.WebControls.Label LabRowID;
protected System.Web.UI.WebControls.Label LabID;
private DataSet OleDs;
private Web.Common.Common Cmon = new Common.Common();
private Web.DataAccess.DataAccess Das = new DataAccess.DataAccess();
protected System.Web.UI.WebControls.TextBox TxtState;
protected System.Web.UI.WebControls.ImageButton IbtQuery;
protected System.Web.UI.WebControls.Panel PalArticle;
protected System.Web.UI.WebControls.Button BtnAdd;
protected System.Web.UI.WebControls.Button BtnDel;
protected System.Web.UI.WebControls.Button BtnSave;
protected System.Web.UI.WebControls.Button BtnCancel;
protected System.Web.UI.HtmlControls.HtmlTable Table1;
protected System.Web.UI.WebControls.DataGrid GrdFile;
protected System.Web.UI.WebControls.Panel PalView;
protected System.Web.UI.HtmlControls.HtmlTable Table4;
protected System.Web.UI.HtmlControls.HtmlInputText TxtFileName;
protected System.Web.UI.HtmlControls.HtmlInputFile UploadFile;
const int nGrdRows = 10;
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
BindGrdFile();
TxtState.Text = "0";
PageInit();
}
GrdFile.Attributes.Add("onclick","javascript:Click('GrdFile');");
GrdFile.Attributes.Add("ondblclick","javascript:DbClick('GrdFile');");
BtnSave.Attributes.Add("OnClick","javascript:return CheckInput();");
BtnDel.Attributes.Add("OnClick","javascript:return Del('GrdFile');");
}
#region Web 窗體設計器生成的代碼
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 該調用是 ASP.NET Web 窗體設計器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void InitializeComponent()
{
this.BtnAdd.Click += new System.EventHandler(this.BtnAdd_Click);
this.BtnDel.Click += new System.EventHandler(this.BtnDel_Click);
this.BtnSave.Click += new System.EventHandler(this.BtnSave_Click);
this.BtnCancel.Click += new System.EventHandler(this.BtnCancel_Click);
this.GrdFile.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.GrdFile_PageIndexChanged);
this.GrdFile.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.GrdFile_SortCommand);
this.ID = "Form1";
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
//初始化頁面控件狀態
//LabState:操作狀態,0--查詢,1--瀏覽,2--增加,3--修改,4--刪除
private void PageInit()
{
int nState = int.Parse(TxtState.Text);
PalView.Visible = (nState != 0) ? true:false;
PalArticle.Visible = (nState == 0) ? true:false;
BtnAdd.Enabled = (nState == 0 || nState == 1) ? true:false;
BtnDel.Enabled = (nState == 0 || nState == 1) ? true:false;
BtnSave.Enabled = (nState == 2 || nState == 3) ? true:false;
BtnCancel.Enabled = (nState == 1 || nState == 2 || nState == 3) ? true:false;
}
//刷新GrdFile
private void BindGrdFile()
{
OleDs = new DataSet();
OleDs=Das.GetDataSet("select id,filename,ContentType,datetime from uploadfile");
GrdFile.DataSource=OleDs;
int nPage=0;
nPage = GrdFile.CurrentPageIndex;
GrdFile.CurrentPageIndex =0;
GrdFile.DataBind();
if ((nPage >= GrdFile.PageCount) && (GrdFile.PageCount > 0))
{
nPage = GrdFile.PageCount - 1;
}
LabRowCount.Text=OleDs.Tables[0].Rows.Count.ToString();
GrdFile.CurrentPageIndex = nPage;
GrdFile.DataBind();
}
private void BtnAdd_Click(object sender, System.EventArgs e)
{
TxtState.Text = "2";
PageInit();
Clear();
}
private void Clear()
{
TxtFileName.Value = "";
}
private void BtnCancel_Click(object sender, System.EventArgs e)
{
TxtState.Text = "0";
PageInit();
Clear();
}
//保存信息
private void BtnSave_Click(object sender, System.EventArgs e)
{
Stream imgStream;
int imgLen;
string imgName_value;
string imgContentType;
string imgUploadedName;
imgStream = UploadFile.PostedFile.InputStream;
imgLen = UploadFile.PostedFile.ContentLength;
imgUploadedName = UploadFile.PostedFile.FileName;
byte[] imgBinaryData=new byte[imgLen];
imgContentType = UploadFile.PostedFile.ContentType;
imgName_value = TxtFileName.Value;
try
{
if(imgName_value.Length < 1)
{
imgName_value = GetLastRightOf("\\",imgUploadedName );
}
}
catch(Exception myEx)
{
Response.Write("<script language='javascript'>alert('錯誤!');</script>");
}
int n = imgStream.Read(imgBinaryData, 0, imgLen);
int NumRowsAffected = MyDatabaseMethod(imgName_value, imgBinaryData, imgContentType);
if(NumRowsAffected > 0)
{
Response.Write("<script language='javascript'>alert('文件上傳成功!');</script>");
BindGrdFile();
}
else
Response.Write ("<script language='javascript'>alert('文件上傳不成功,請重新上傳!');</script>");
}
public string GetLastRightOf(string LookFor,string myString)
{
int StrPos;
StrPos = myString.LastIndexOf(LookFor);
return myString.Substring(StrPos + 1);
}
public int MyDatabaseMethod(string imgName,byte[] imgbin,string imgcontenttype)
{
// OleDbConnection conn = new OleDbConnection("Provider=SQLOLEDB;user id=sa;password=;initial catalog=web;data source=localhost;Connect Timeout=30");
// string SQL="INSERT INTO uploadfile (filename,data,contenttype,datetime) VALUES ( @img_name, @img_data,@img_contenttype,@datetime)";
// OleDbCommand command=new OleDbCommand(SQL,conn);
// command.Parameters.Add("@img_name", OleDbType.VarChar, 50).Value = imgName;
// command.Parameters.Add("@img_data", OleDbType.Binary).Value = imgbin;
// command.Parameters.Add("@img_contenttype", OleDbType.VarChar,50).Value = imgcontenttype;
// command.Parameters.Add("@datetime", OleDbType.Date).Value = DateTime.Now;
SqlConnection conn = new SqlConnection("user id=sa;password=;initial catalog=web;data source=localhost;Connect Timeout=30");
string SQL="INSERT INTO uploadfile (filename,data,contenttype,datetime) VALUES ( @img_name, @img_data,@img_contenttype,@datetime)";
SqlCommand command=new SqlCommand ( SQL,conn );
// SqlParameter param0=new SqlParameter ( "@img_name", SqlDbType.VarChar,50 );
// param0.Value = imgName;
// command.Parameters.Add( param0 );
command.Parameters.Add("@img_name", SqlDbType.VarChar, 50).Value = imgName;
SqlParameter param1=new SqlParameter ( "@img_data", SqlDbType.Image );
param1.Value = imgbin;
command.Parameters.Add( param1 );
SqlParameter param2 =new SqlParameter ( "@img_contenttype", SqlDbType.VarChar,50 );
param2.Value = imgcontenttype;
command.Parameters.Add( param2 );
SqlParameter param3 =new SqlParameter ( "@datetime",SqlDbType.DateTime);
param3.Value = DateTime.Now;
command.Parameters.Add( param3);
conn.Open();
int numRowsAffected = command.ExecuteNonQuery();
conn.Close();
return numRowsAffected;
}
//檢查用戶選擇DataGrid中記錄的總數,返回DG中單選的記錄號,多選的記錄號保存LabDeptID中。
private int CheckCbx()
{
int nRow = -1;
LabID.Text = "";
for (int i = 0;i < GrdFile.Items.Count;i++)
{
CheckBox CbxTable = (CheckBox)GrdFile.Items[i].FindControl("CbxSelect");
if(CbxTable.Checked == true)
{
if(LabID.Text == "")
{
LabID.Text = GrdFile.Items[i].Cells[4].Text.Trim();
nRow = i;
}
else
{
LabID.Text = LabID.Text + "," + GrdFile.Items[i].Cells[4].Text.Trim();
}
}
}
return nRow;
}
//設置復選框狀態。
private void SetCbxState(int nRow,bool bState)
{
CheckBox CbxDept;
CbxDept = (CheckBox)GrdFile.Items[nRow].FindControl("CbxSelect");
CbxDept.Checked = bState;
}
//換頁
private void GrdFile_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
GrdFile.CurrentPageIndex = e.NewPageIndex;
BindGrdFile();
}
//排序
private void GrdFile_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
DataView dv = new DataView();
BindGrdFile();
dv.Table = OleDs.Tables[0];
dv.Sort = e.SortExpression;
GrdFile.DataSource = dv;
GrdFile.DataBind();
GrdFile.EditItemIndex = -1;
GrdFile.SelectedIndex = 0;
}
//刪除信息
private void BtnDel_Click(object sender, System.EventArgs e)
{
int nRow = 0;
string strTableID,strSql;
nRow = CheckCbx();
strTableID = LabID.Text.Trim();
strSql = "delete from uploadfile where id in(" + strTableID + ")";
if (Das.ExecSql(strSql))
{
Response.Write("<script>alert('刪除成功!');</script>");
Clear();
BindGrdFile();
}
else
{
Response.Write("<script>alert('刪除不成功,請稍后再試!');</script>");
if(TxtState.Text=="1")
{
PageInit();
}
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -