Files
thinkyu/branches/1150807/Forms/課程企劃/數位課程管理.aspx.cs
T
sryang 577060bc78 chore: 首次簽入 Thinkyu ASP.NET 專案
- 加入 Visual Studio / ASP.NET .gitignore
- 排除建置輸出、IDE 設定、NuGet packages、大型 MSI 安裝檔
2026-09-10 09:42:37 +08:00

334 lines
10 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using WebLib;
namespace Education.Forms.課程企劃
{
public partial class 數位課程管理 : FormBase
{
private DAC_數位課程 Dao數位課程;
private string docPath;
protected void Page_Load(object sender, EventArgs e)
{
FunctionName = "數位課程管理";
Dao數位課程 = new DAC_數位課程(conn);
//Dao數位課程.CurrentUserId = UserId;
docPath = Server.MapPath("~/DocUpload");
if (!Directory.Exists(docPath))
Directory.CreateDirectory(docPath);
Form_View = FormView1;
Grid_View = GridView1;
FormView_DataSource = dsForm;
GridView_DataSource = dsGrid;
AddTabButton(MultiView1, view處理, btn處理);
AddTabButton(MultiView1, view查詢, btn查詢);
if (!IsPostBack)
{
ActivateTab(MultiView1, view處理);
}
else
{
Grid_View.DataBind();
Form_View.DataBind();
if (pnl挑選課程.Visible)
{
gv課程挑選.DataBind();
}
}
}
protected void dsForm_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
{
e.ObjectInstance = Dao數位課程;
}
protected void dsGrid_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
{
e.ObjectInstance = Dao數位課程;
}
protected void btnSearch_Click(object sender, EventArgs e)
{
Grid_View.PageIndex = 0;
Grid_View.DataBind();
}
protected override void FormView_ReadOnlyDataBound(object sender, EventArgs e)
{
base.FormView_ReadOnlyDataBound(sender, e);
RegisterPostbackTrigger(Form_View.FindControl("btnUpload1"));
RegisterPostbackTrigger(Form_View.FindControl("btnUpload2"));
}
protected override void FormView_InsertDataBound(object sender, EventArgs e)
{
base.FormView_InsertDataBound(sender, e);
RegisterPostbackTrigger(Form_View.FindControl("btnSave"));
RegisterPostbackTrigger(Form_View.FindControl("btnCancel"));
Form_View.FindControl("txt課程名稱").Focus();
FindControl<TextBox>(Form_View, "txt長度").Text = "0";
FindControl<ValueCheckBox>(Form_View, "cb開放觀看").Checked = true;
}
protected override void FormView_EditDataBound(object sender, EventArgs e)
{
base.FormView_EditDataBound(sender, e);
RegisterPostbackTrigger(Form_View.FindControl("btnSave"));
RegisterPostbackTrigger(Form_View.FindControl("btnCancel"));
Form_View.FindControl("txt課程名稱").Focus();
}
protected override void GridView_DataSource_Selected(object sender, ObjectDataSourceStatusEventArgs e)
{
base.GridView_DataSource_Selected(sender, e);
if (MultiView1.ActiveViewIndex == 1)
{
if (e.ReturnValue is 數位課程List list && list.Count > 0)
ActivateTab(MultiView1, 1);
else
lbMessage.Text = "找不到合乎條件的資料";
}
}
private bool 儲存上傳檔案(FileUpload fu, int 課程編號, out string fileName)
{
if (fu == null || !fu.HasFile)
{
fileName = "";
return false;
}
fileName = String.Format("{0:000000}_{1}", 課程編號, fu.FileName);
File.WriteAllBytes(docPath + "\\" + fileName, fu.FileBytes);
return true;
}
private void 刪除上傳檔案(int 課程編號, bool del課程文件 = true, bool del評量文件 = true)
{
數位課程List d = Dao數位課程.SelectOne(課程編號);
if (d.Count > 0)
{
if (del課程文件 && d[0].課程文件.Length > 0 && File.Exists(docPath + "\\" + d[0].課程文件))
File.Delete(docPath + "\\" + d[0].課程文件);
if (del評量文件 && d[0].評量文件.Length > 0 && File.Exists(docPath + "\\" + d[0].評量文件))
File.Delete(docPath + "\\" + d[0].評量文件);
}
}
protected override void FormView_ItemInserting(object sender, FormViewInsertEventArgs e)
{
base.FormView_ItemInserting(sender, e);
//fu課程文件 = Form_View.FindControl("fu課程文件") as FileUpload;
//fu評量文件 = Form_View.FindControl("fu評量文件") as FileUpload;
}
protected override void FormView_ItemUpdating(object sender, FormViewUpdateEventArgs e)
{
base.FormView_ItemUpdating(sender, e);
//fu課程文件 = Form_View.FindControl("fu課程文件") as FileUpload;
//fu評量文件 = Form_View.FindControl("fu評量文件") as FileUpload;
}
protected override void FormView_DataSource_Inserted(object sender, ObjectDataSourceStatusEventArgs e)
{
base.FormView_DataSource_Inserted(sender, e);
int id = DAC.GetInt32(e.ReturnValue);
// 處理上傳檔案
/*
數位課程List d = Dao數位課程.SelectOne(id);
if (d.Count > 0)
{
string fileName1;
string fileName2;
if (儲存上傳檔案(fu課程文件, id, out fileName1))
{
d[0].課程文件 = fileName1;
}
if (儲存上傳檔案(fu評量文件, id, out fileName2))
{
d[0].評量文件 = fileName2;
}
Dao數位課程.UpdateOne(d[0]);
}
*/
}
protected override void FormView_DataSource_Updating(object sender, ObjectDataSourceMethodEventArgs e)
{
base.FormView_DataSource_Updating(sender, e);
數位課程 value = e.InputParameters[0] as 數位課程;
/*
string fileName1;
string fileName2;
// 刪除舊上傳檔案
刪除上傳檔案(value.課程編號, fu課程文件.HasFile, fu評量文件.HasFile);
// 儲存新上傳檔案
if (儲存上傳檔案(fu課程文件, value.課程編號, out fileName1))
{
value.課程文件 = fileName1;
}
if (儲存上傳檔案(fu評量文件, value.課程編號, out fileName2))
{
value.評量文件 = fileName2;
}
*/
}
protected override void FormView_DataSource_Deleting(object sender, ObjectDataSourceMethodEventArgs e)
{
base.FormView_DataSource_Deleting(sender, e);
// 刪除上傳檔案
數位課程 value = e.InputParameters[0] as 數位課程;
刪除上傳檔案(value.課程編號);
}
protected void btnClear_Click(object sender, EventArgs e)
{
txt講師_Search.Text = "";
txt課程名稱_Search.Text = "";
txt課程簡介_Search.Text = "";
}
protected void FormView1_ItemCommand(object sender, FormViewCommandEventArgs e)
{
if (e.CommandName == "刪除文件1" || e.CommandName == "刪除文件2")
{
數位課程List d = Dao數位課程.SelectOne(DAC.GetInt32(Form_View.SelectedValue));
if (d.Count > 0)
{
string fileName;
if (e.CommandName == "刪除文件1" && d[0].課程文件.Length > 0)
{
fileName = String.Format("{0}\\{1}", docPath, d[0].課程文件);
if (File.Exists(fileName))
{
File.Delete(fileName);
d[0].課程文件 = "";
}
}
else if (e.CommandName == "刪除文件2" && d[0].評量文件.Length > 0)
{
fileName = String.Format("{0}\\{1}", docPath, d[0].評量文件);
if (File.Exists(fileName))
{
File.Delete(fileName);
d[0].評量文件 = "";
}
}
else
return;
Dao數位課程.UpdateOne(d[0]);
Form_View.DataBind();
}
}
if (e.CommandName == "上傳文件1")
{
FileUpload fu = FindControl<FileUpload>(Form_View, "fu課程文件");
int id = DAC.GetInt32(Form_View.SelectedValue);
數位課程List d = Dao數位課程.SelectOne(id);
if (d.Count > 0 && fu.HasFile)
{
刪除上傳檔案(id, true, false);
string fileName;
if (儲存上傳檔案(fu, id, out fileName))
{
d[0].課程文件 = fileName;
Dao數位課程.UpdateOne(d[0]);
Form_View.DataBind();
}
}
}
if (e.CommandName == "上傳文件2")
{
FileUpload fu = FindControl<FileUpload>(Form_View, "fu評量文件");
int id = DAC.GetInt32(Form_View.SelectedValue);
數位課程List d = Dao數位課程.SelectOne(id);
if (d.Count > 0 && fu.HasFile)
{
刪除上傳檔案(id, false, true);
string fileName;
if (儲存上傳檔案(fu, id, out fileName))
{
d[0].評量文件 = fileName;
Dao數位課程.UpdateOne(d[0]);
Form_View.DataBind();
}
}
}
}
protected void ds課程_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
{
e.ObjectInstance = Dao課程;
}
protected void gv課程挑選_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Select")
{
// 挑選此課程,把課程相關資料抓出來填入底下
課程List d = Dao課程.SelectOne(e.CommandArgument.ToString());
if (d.Count > 0)
{
FindControl<DropDownList>(Form_View, "dd課程類別").SelectedValue = d[0].課程類別;
FindControl<TextBox>(Form_View, "txt課程名稱").Text = d[0].課程名稱;
FindControl<TextBox>(Form_View, "txt課程簡介").Text = d[0].課程內容;
}
}
}
protected void btnSelect課程_Click(object sender, EventArgs e)
{
//DropDownList ddl課程類別Search = Form_View.FindControl("ddl課程類別Search") as DropDownList;
//Label lbMessage課程內容 = Form_View.FindControl("lbMessage課程內容") as Label;
//GridView gv課程挑選 = Form_View.FindControl("gv課程挑選") as GridView;
if (ddl課程類別Search.SelectedValue == null || ddl課程類別Search.SelectedValue.Length == 0)
{
lbMessage課程內容.Text = "請選擇一個課程類別再查詢";
return;
}
gv課程挑選.DataBind();
if (gv課程挑選.Rows.Count == 0)
lbMessage課程內容.Text = "找不到合乎條件的課程";
}
protected override void FormView_ModeChanged(object sender, EventArgs e)
{
base.FormView_ModeChanged(sender, e);
pnl挑選課程.Visible = Form_View.CurrentMode == FormViewMode.Insert;
if (pnl挑選課程.Visible)
gv課程挑選.DataBind();
}
}
}