我的任务是处理带有添加注释选项的注释部分。 另外,我有文件部分,可以选择下载文件。 在从PageLoad上的数据库读取信息时,正在动态设置“表格”,“行”,“单元格”和“下载”按钮。
下载文件后,页面回发后可以正确加载(我猜这是因为“ onInit”功能),但是,在添加注释或文件之后,旧表信息以及带有新信息(这是因为我们在Click处理程序中调用了“ loadUpView”,并且在OnInit中也调用了“ loadUpView”。
如果我们从addCommentClick处理程序中删除“ loadUpView”调用,则在PostBack之后,我们将获得旧表信息。
我们应该怎么做才能在回发后获取更新的信息,以及正确生成下载文件。
我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Teva.classes;
using Teva.classes.ObjectTypes;
using Teva.classes.Utils;
using WebApplication1.classes.ObjectTypes;
using WebApplication1.classes.Utils;
namespace WebApplication1
{
public partial class MissionMain : System.Web.UI.Page
{
User user;
Mission mission;
protected void Page_Load(object sender, EventArgs e)
{
if (Session["user"] == null)
{
Response.Redirect("Login2.aspx");
}
user = (User)(Session["user"]);
int missionID = Int32.Parse(Request.QueryString["missionID"]);
mission = handleMissions.getMission(missionID);
if (mission != null)
{
Label13.Text = mission.mission_description;
}
//if (!IsPostBack)
//{
// loadupView(missionID);
//}
}
private void loadupView(int missionID)
{
showMissionTable(mission);
showCommentsTable(mission, handleMissions.getCommentsOfMission(missionID));
showFilesTable(handleMissions.getFilesOfMission(missionID));
generateDropDowns();
}
private void loadupView()
{
int missionID = Int32.Parse(Request.QueryString["missionID"]);
mission = handleMissions.getMission(missionID);
showMissionTable(mission);
showCommentsTable(mission, handleMissions.getCommentsOfMission(missionID));
showFilesTable(handleMissions.getFilesOfMission(missionID));
generateDropDowns();
}
private void showMissionTable(Mission miss)
{
Table missionTable = new Table();
missionTable.ID = "missionTable-header";
missionTable.CssClass = "missionTable";
TableRow row = new TableRow();
row.CssClass = "row100 head";
TableCell cell1 = new TableCell();
cell1 = new TableCell();
cell1.Text = "Mission ID";
cell1.CssClass = "cell100 column1";
row.Cells.Add(cell1);
cell1 = new TableCell();
cell1.Text = "Start Time";
cell1.CssClass = "cell100 column2";
row.Cells.Add(cell1);
cell1 = new TableCell();
cell1.Text = "End Time";
cell1.CssClass = "cell100 column3";
row.Cells.Add(cell1);
cell1 = new TableCell();
cell1.Text = "Manager";
cell1.CssClass = "cell100 column4";
row.Cells.Add(cell1);
cell1 = new TableCell();
cell1.Text = "Quality Control";
cell1.CssClass = "cell100 column5";
row.Cells.Add(cell1);
cell1 = new TableCell();
cell1.Text = "Status";
cell1.CssClass = "cell100 column6";
row.Cells.Add(cell1);
cell1 = new TableCell();
cell1.Text = "Priority";
cell1.CssClass = "cell100 column7";
row.Cells.Add(cell1);
missionDescContainer.Controls.Add(new LiteralControl("<thead>"));
missionTable.Rows.Add(row);
missionDescContainer.Controls.Add(new LiteralControl("</thead>"));
missionDescContainer.Controls.Add(missionTable);
missionTable = new Table();
missionTable.ID = "missionTable-" + miss.mission_id.ToString();
missionTable.CssClass = "missionTable";
row = new TableRow();
row.CssClass = "row100 body";
cell1 = new TableCell();
cell1 = new TableCell();
cell1.Text = miss.mission_id.ToString();
cell1.CssClass = "cell100 column1";
row.Cells.Add(cell1);
cell1 = new TableCell();
cell1.Text = miss.start_time.ToString();
cell1.CssClass = "cell100 column2";
row.Cells.Add(cell1);
cell1 = new TableCell();
cell1.Text = miss.end_time.ToString();
cell1.CssClass = "cell100 column3";
row.Cells.Add(cell1);
cell1 = new TableCell();
cell1.Text = miss.user_id.ToString();
cell1.CssClass = "cell100 column4";
row.Cells.Add(cell1);
cell1 = new TableCell();
cell1.Text = miss.quality_control.ToString();
cell1.CssClass = "cell100 column5";
row.Cells.Add(cell1);
cell1 = new TableCell();
cell1.Text = miss.status;
cell1.CssClass = "cell100 column6";
row.Cells.Add(cell1);
cell1 = new TableCell();
cell1.Text = miss.priority;
cell1.CssClass = "cell100 column7";
row.Cells.Add(cell1);
missionTable.Rows.Add(row);
missionDescRessults.Controls.Add(missionTable);
missionDescRessults.Controls.Add(new LiteralControl("<br />"));
}
private void showFilesTable(List<MissionFile> list)
{
Table filesTable = new Table();
filesTable.ID = "filesTable-header";
filesTable.CssClass = "filesTable";
TableRow row = new TableRow();
row.CssClass = "row100 head";
TableCell cell1 = new TableCell();
cell1 = new TableCell();
cell1.Text = "ID";
cell1.CssClass = "cell100 column1";
row.Cells.Add(cell1);
cell1 = new TableCell();
cell1.Text = "File Name";
cell1.CssClass = "cell100 column1";
row.Cells.Add(cell1);
cell1 = new TableCell();
cell1.Text = "Description";
cell1.CssClass = "cell100 column2";
row.Cells.Add(cell1);
cell1 = new TableCell();
cell1.Text = "User Uploaded";
cell1.CssClass = "cell100 column3";
row.Cells.Add(cell1);
cell1 = new TableCell();
cell1.Text = "Date Uploaded";
cell1.CssClass = "cell100 column4";
row.Cells.Add(cell1);
cell1 = new TableCell();
cell1.Text = "Download";
cell1.CssClass = "cell100 column5";
row.Cells.Add(cell1);
filesTableContainer.Controls.Add(new LiteralControl("<thead>"));
filesTable.Rows.Add(row);
filesTableContainer.Controls.Add(new LiteralControl("</thead>"));
filesTableContainer.Controls.Add(filesTable);
filesTable = new Table();
filesTable.ID = "filesTable-" + mission.mission_id.ToString();
filesTable.CssClass = "filesTable";
if (list != null)
{
foreach (MissionFile f in list)
{
row = new TableRow();
row.CssClass = "row100 body";
cell1 = new TableCell();
cell1.Text = f.file_id.ToString();
cell1.CssClass = "cell100 column1";
row.Cells.Add(cell1);
cell1 = new TableCell();
cell1.Text = f.name.ToString();
cell1.CssClass = "cell100 column2";
row.Cells.Add(cell1);
cell1 = new TableCell();
cell1.Text = f.description.ToString();
cell1.CssClass = "cell100 column3";
row.Cells.Add(cell1);
cell1 = new TableCell();
cell1.Text = f.user.ToString();
cell1.CssClass = "cell100 column4";
row.Cells.Add(cell1);
cell1 = new TableCell();
cell1.Text = f.date.ToString();
cell1.CssClass = "cell100 column5";
row.Cells.Add(cell1);
cell1 = new TableCell();
ImageButton downloadButton = new ImageButton();
downloadButton.Attributes.Add("fileLocation", f.location);
downloadButton.ID = "downloadFile" + f.file_id;
downloadButton.Click += new ImageClickEventHandler(downloadFileClick);
downloadButton.ImageUrl = "~/pictures/downloadIcon.png";
downloadButton.Width = 50;
downloadButton.Height = 50;
cell1.Controls.Add(downloadButton);
cell1.CssClass = "cell100 column6";
row.Cells.Add(cell1);
filesTable.Rows.Add(row);
}
}
filesTableResults.Controls.Add(filesTable);
filesTableResults.Controls.Add(new LiteralControl("<br />"));
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
loadupView();
}
private void showCommentsTable(Mission miss, List<Comment> list)
{
Table commentsTable = new Table();
commentsTable.ID = "commentsTableHeader";
commentsTable.CssClass = "commentsTable";
TableRow row = new TableRow();
row.CssClass = "row100 head";
TableCell cell1 = new TableCell();
cell1 = new TableCell();
cell1.Text = "ID";
cell1.CssClass = "cell100 column1";
row.Cells.Add(cell1);
cell1 = new TableCell();
cell1.Text = "Text";
cell1.CssClass = "cell100 column2";
row.Cells.Add(cell1);
cell1 = new TableCell();
cell1.Text = "User Posted";
cell1.CssClass = "cell100 column3";
row.Cells.Add(cell1);
cell1 = new TableCell();
cell1.Text = "Date Posted";
cell1.CssClass = "cell100 column4";
row.Cells.Add(cell1);
cell1 = new TableCell();
cell1.Text = "Critical Level";
cell1.CssClass = "cell100 column5";
row.Cells.Add(cell1);
cell1 = new TableCell();
cell1.Text = "Status";
cell1.CssClass = "cell100 column6";
row.Cells.Add(cell1);
commentsTableContainer.Controls.Add(new LiteralControl("<thead>"));
commentsTable.Rows.Add(row);
commentsTableContainer.Controls.Add(new LiteralControl("</thead>"));
commentsTableContainer.Controls.Add(commentsTable);
commentsTable = new Table();
commentsTable.ID = "commentsTable";
commentsTable.CssClass = "commentsTable";
if (list != null)
{
foreach (Comment c in list)
{
row = new TableRow();
row.CssClass = "row100 body";
cell1 = new TableCell();
cell1.Text = c.comment_id.ToString();
cell1.CssClass = "cell100 column1";
row.Cells.Add(cell1);
cell1 = new TableCell();
cell1.Text = c.details.ToString();
cell1.CssClass = "cell100 column2";
row.Cells.Add(cell1);
cell1 = new TableCell();
cell1.Text = c.user.ToString();
cell1.CssClass = "cell100 column3";
row.Cells.Add(cell1);
cell1 = new TableCell();
cell1.Text = c.date.ToString();
cell1.CssClass = "cell100 column4";
row.Cells.Add(cell1);
cell1 = new TableCell();
cell1.Text = c.criticLevel.ToString();
cell1.CssClass = "cell100 column5";
row.Cells.Add(cell1);
cell1 = new TableCell();
cell1.Text = c.status.ToString();
cell1.CssClass = "cell100 column6";
row.Cells.Add(cell1);
commentsTable.Rows.Add(row);
}
}
commentsTableResults.Controls.Add(commentsTable);
commentsTableResults.Controls.Add(new LiteralControl("<br />"));
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
protected void add_comment_Click(object sender, EventArgs e)
{
try
{
if (user != null && mission != null)
{
if (description.Text != "")
{
description.Text = description.Text.Replace("\r\n", "<br />");
Comment comment1 = new Comment(-1, mission.mission_id, description.Text, Int32.Parse(user.userID), DateTime.Now.ToString("yyyy-MM-dd"), priority.Text, 1);
Boolean insertedComment = handleMissions.InsertCommenrToMission(comment1);
Response.Write("<script>alert('Successfully added');</script>");
ClearForm1();
loadupView(Int32.Parse(Request.QueryString["missionID"]));
ViewState["Generated"] = "true";
}
}
}
catch (Exception ex)
{
Response.Write("<script>alert('Failed to upload comment');</script>");
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
protected void downloadFileClick(object sender, EventArgs e)
{
try
{
string fileLocation = ((ImageButton)sender).Attributes["fileLocation"];
string remoteUri = Server.MapPath("/UploadedFile");
string fileName = fileLocation;
// Create a new WebClient instance.
WebClient myWebClient = new WebClient();
// Concatenate the domain with the Web resource filename.
string myStringWebResource = remoteUri + "\\" + fileName;
myWebClient.DownloadFile(myStringWebResource, fileName);
//loadupView(Int32.Parse(Request.QueryString["missionID"]));
}
catch (Exception ex)
{
Response.Write("<script>alert('Download File failed');</script>");
}
}
protected void btnUpload_Click(object sender, EventArgs e)
{
try
{
if (user != null && mission != null)
{
Guid g = Guid.NewGuid();
MissionFile mf = new MissionFile(-1, mission.mission_id, fileName.Text, fileDescription.Text
, DateTime.Now.ToString("yyyy-MM-dd"), g.ToString() + '-' + fuSample.FileName, Int32.Parse(user.userID));
if (handleMissions.InsertFileToMission(mf))
{
//Files is folder Name
fuSample.SaveAs(Server.MapPath("/UploadedFile") + "/" + g.ToString() + '-' + fuSample.FileName);
lblMessage.Text = "File Successfully Uploaded";
ClearForm();
loadupView(Int32.Parse(Request.QueryString["missionID"]));
ViewState["Generated"] = "true";
}
}
}
catch
{
Response.Write("<script>alert('File upload failed');</script>");
}
}
protected void generateDropDowns()
{
string[] names = Enum.GetNames(typeof(Enums.Priorities));
for (int i = 0; i < names.Length; i++)
{
priority.Items.Add(
new ListItem(
names[i],
names[i]
)
);
foreach (ListItem item in priority.Items)
{
if (item.Value == "Low")
{
item.Attributes.Add("style", "background-color:greenyellow");
}
if (item.Value == "Medium")
{
item.Attributes.Add("style", "background-color:darkorange");
}
if (item.Value == "High")
{
item.Attributes.Add("style", "background-color:red");
}
}
}
names = Enum.GetNames(typeof(Enums.statusComment));
for (int i = 0; i < names.Length; i++)
{
status.Items.Add(
new ListItem(
names[i],
names[i]
)
);
}
foreach (ListItem item in status.Items)
{
if (item.Value == "Checked")
{
item.Attributes.Add("style", "background-color:greenyellow");
}
if (item.Value == "Unchecked")
{
item.Attributes.Add("style", "background-color:red");
}
}
}
protected void ClearForm()
{
fileName.Text = string.Empty;
fileDescription.Text = string.Empty;
}
protected void ClearForm1()
{
description.Text = string.Empty;
}
}
}