📄 picturemanage.aspx.cs
字号:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Linq;
using System.Data.Linq;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
public partial class PictureManage : System.Web.UI.Page
{
int nBookID = -1;
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Params["BookID"] != null)
{
nBookID = Int32.Parse(Request.Params["BookID"].ToString());
}
if (!Page.IsPostBack)
{
if (nBookID > -1)
{
///显示书籍名称
BindBookData(nBookID);
///显示图片列表信息
BindPictureData(nBookID);
}
}
///添加删除确认对话框
DeleteBtn.Attributes.Add("onclick","return confirm('" + ASPNET35System.OPERATIONDELETEMESSAGE + "');");
}
private void BindBookData(int nBookID)
{
BookM book = new BookM();
Book recb = book.GetSingleBook(nBookID);
if (recb!=null)
{
///显示书籍名称
Name.Text = recb.Name;
}
}
private void BindPictureData(int nBookID)
{
///从数据库获取图片信息
PictureM picture = new PictureM();
IList<Picture> recp = picture.GetPictureByBook(nBookID);
///绑定图片列表的数据
PictureList.DataSource = recp;
PictureList.DataTextField = "Desn";
PictureList.DataValueField = "PictureID";
PictureList.DataBind();
}
protected void DeleteBtn_Click(object sender,EventArgs e)
{
if (PictureList.SelectedIndex <= -1)
{
///显示提示信息
Response.Write("<script>window.alert('" + ASPNET35System.LISTBOX_NO_SELECT_ITEM + "')</script>");
return;
}
try
{
///定义类
PictureM picture = new PictureM();
///删除操作
picture.DeletePicture(Int32.Parse(PictureList.SelectedValue));
///显示操作结果信息
Response.Write("<script>window.alert('" + ASPNET35System.OPERATIONDELETESUCCESSMESSAGE + "')</script>");
///重新绑定数据
BindPictureData(nBookID);
}
catch (Exception ex)
{
///显示删除操作中的失败、错误信息
Response.Redirect("~/DesktopModules/ErrorPage.aspx?ErrorUrl="
+ ASPNET35System.RedirectErrorUrl(Request.RawUrl)
+ "&ErrorMessage=" + ex.Message.Replace("\n"," "));
}
}
protected void SetDefaultBtn_Click(object sender,EventArgs e)
{
if (PictureList.SelectedIndex <= -1)
{
///显示提示信息
Response.Write("<script>window.alert('" + ASPNET35System.LISTBOX_NO_SELECT_ITEM + "')</script>");
return;
}
try
{
///定义类
PictureM pictrure = new PictureM();
///更新操作
pictrure.UpdatePictureShow(Int32.Parse(PictureList.SelectedValue),nBookID);
///显示操作结果信息
Response.Write("<script>window.alert('" + ASPNET35System.OPERATIONUPDATESUCCESSMESSAGE + "')</script>");
///重新绑定数据
BindPictureData(nBookID);
}
catch (Exception ex)
{
///显示更新操作中的失败、错误信息
Response.Redirect("~/DesktopModules/ErrorPage.aspx?ErrorUrl="
+ ASPNET35System.RedirectErrorUrl(Request.RawUrl)
+ "&ErrorMessage=" + ex.Message.Replace("\n"," "));
}
}
protected void AddBtn_Click(object sender,EventArgs e)
{
///如果页面输入内容合法
if (Page.IsValid == true)
{
///定义类
PictureM picture = new PictureM();
try
{
///添加新数据
picture.AddPicture(nBookID,Desn.Text.Trim(),UploadPicture(),
PictureFile.PostedFile.ContentType,
Int32.Parse(ShowList.SelectedValue),Remark.Text);
///绑定附件列表的数据
BindPictureData(nBookID);
///显示操作结果信息
Response.Write("<script>window.alert('" + ASPNET35System.OPERATIONADDSUCCESSMESSAGE + "')</script>");
}
catch (Exception ex)
{
///显示添加操作中的失败、错误信息
Response.Redirect("~/DesktopModules/ErrorPage.aspx?ErrorUrl="
+ ASPNET35System.RedirectErrorUrl(Request.RawUrl)
+ "&ErrorMessage=" + ex.Message.Replace("\n"," "));
}
}
}
private string UploadPicture()
{
if ((Desn.Text.Trim() != "") && (PictureFile.PostedFile.ContentLength > 0))
{
String fileName = PictureFile.PostedFile.FileName.Substring(PictureFile.PostedFile.FileName.LastIndexOf("\\"),
PictureFile.PostedFile.FileName.Length - PictureFile.PostedFile.FileName.LastIndexOf("\\"));
String fileTime = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString()
+ DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString()
+ DateTime.Now.Second.ToString() + DateTime.Now.Minute.ToString()
+ DateTime.Now.Millisecond.ToString();
fileName = "\\" + fileTime + GetRandomint() + fileName.Substring(fileName.IndexOf("."),fileName.Length - fileName.IndexOf("."));
string sFileName = Server.MapPath(Request.ApplicationPath) + "\\Pictures" + fileName;
if (File.Exists(sFileName) == false)
{
try
{
//把文件存入磁盘,如果失败,导向提示页面
PictureFile.PostedFile.SaveAs(sFileName);
return ("\\Pictures" + fileName);
}
catch (Exception ex)
{
string sRawURL = Request.RawUrl;
if (sRawURL.IndexOf("?") > -1)
{
sRawURL = sRawURL.Substring(0,sRawURL.IndexOf("?"));
}
Response.Redirect("~/DesktopModules/ErrorPage.aspx?ErrorUrl=" + sRawURL + "&ErrorMessage=" + ex.Message.Replace("\n"," "));
}
}
else
{
Response.Write("<script>alert(\"你上传的文件已经存在!\")</script>");
}
}
else
{
Response.Write("<script>alert(\"你输入的文件描述\\文件名为空,请重新输入!\")</script>");
}
return ("");
}
private String GetRandomint()
{
Random random = new Random();
return (random.Next(10000).ToString());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -