Below is a copy of the .cs file for the control and I will past a copy of the .ascx next
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
using CMS.PortalControls;
using CMS.GlobalHelper;
using CMS.CMSHelper;
public partial class CMSWebParts_RohlWebParts_SalesDocMaint : CMSAbstractWebPart
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["action"] = "";
}
//if (QueryHelper.Contains("action"))
//{
string action = QueryHelper.GetString("action", "");
string catId = QueryHelper.GetString("catId", "");
string desc = QueryHelper.GetString("desc", "");
string hasChild = QueryHelper.GetString("hasChild", "");
Session["action"] = action;
PerformAction(action, catId, desc, hasChild);
// ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "showstuff", "alert('Inside of salesdocmaint ... " + Request.Url.AbsoluteUri + "');", true);
//}
}
private void PerformAction(String action, String nodeId, String desc, String hasChild)
{
string script = "";
switch (action)
{
case "add":
script = "document.getElementById('rightDiv').className = 'rightDivStyleVisible';";
txtEditCatName.Text = desc;
txtEditCatName.Enabled = false;
HiddenCatId.Value = nodeId;
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "showstuff", script, true);
break;
//Can only Edit category name
case "edit":
script = "document.getElementById('newCatRight').className = 'newCatStyleVisible';";
if (HiddenCatId.Value.ToString() != nodeId)
{
txtNewCatName.Text = desc;
}
txtNewCatName.Enabled = true;
HiddenCatId.Value = nodeId;
Session["action"] = "edit";
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "showstuff", script, true);
break;
case "replace":
script = "document.getElementById('rightDiv').className = 'rightDivStyleVisible';";
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "showstuff", script, true);
txtEditCatName.Enabled = false;
HiddenCatId.Value = nodeId;
GetDocumentInfo(nodeId);
break;
//Delete document from Category
case "deleteDoc":
script = "document.getElementById('rightDiv').className = 'rightDivStyleVisible';";
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "showstuff", script, true);
btnDeleteDocument.Visible = true;
btnCancelDelDoc.Visible = true;
btnSaveDocument.Visible = false;
txtEditCatName.Enabled = false;
HiddenCatId.Value = nodeId;
GetDocumentInfo(nodeId);
break;
//Delete Category but only if it doesn't have any child nodes
case "delete":
if (hasChild.Trim() != "")
{
LabelErrorList.Text = "Category still has documents and cannot be deleted.";
return;
}
else
{
DeleteCategory(nodeId);
LabelStatus.Text = "Category successfully deleted.";
CategoryRepeater.DataBind();
}
HiddenCatId.Value = "0";
break;
case "download":
txtEditCatName.Enabled = false;
HiddenCatId.Value = nodeId;
string filename = "";
string sqlStr = "Select docName From salesDocuments where childNodeId =" + nodeId;
try
{
string connectionString = ConfigurationManager.ConnectionStrings["dealerextranetConnStr"].ToString();
SqlConnection myConnection = new SqlConnection(connectionString);
SqlCommand mySelectCommand = new SqlCommand(sqlStr, myConnection);
myConnection.Open();
SqlDataReader myReader;
myReader = mySelectCommand.ExecuteReader();
while (myReader.Read())
{
filename = myReader["docName"].ToString();
}
myConnection.Close();
fileDownload(filename, Server.MapPath("~/RohlCMS/DocumentLibrary/" + filename));
}
catch (Exception ex)
{
string strError = ex.Message.ToString();
//"Thread was being aborted. " is an acceptable exception.
}
break;
}
}
protected void CategoryRepeater_ItemDatabound(object sender, RepeaterItemEventArgs e)
{
RepeaterItem item = e.Item;
if ((item.ItemType == ListItemType.Item) || (item.ItemType == ListItemType.AlternatingItem))
{
DataRowView drv = (DataRowView)e.Item.DataItem;
string desc = drv.Row["description"].ToString();
string nodeId = drv.Row["nodeId"].ToString();
string hasChild = drv.Row["hasChild"].ToString();
TreeView mytree = (TreeView)e.Item.FindControl("CategoryTree");
TreeNode myNode = new TreeNode();
myNode.Text = "<span>" + desc + "</span> </a> <a href='" + CMSContext.CurrentAliasPath + ".aspx?caller=docmaint&action=delete&desc=&Cat&catId=" + nodeId + "&hasChild=" + hasChild + "' > Delete </a> | <a href='" + CMSContext.CurrentAliasPath + ".aspx?caller=docmaint&action=edit&desc=" + HttpUtility.UrlEncode(desc) + "&hasChild=&catId=" + nodeId + "' > Edit </a> | <a href='" + CMSContext.CurrentAliasPath + ".aspx?caller=docmaint&action=add&catId=" + nodeId + "&desc=" + HttpUtility.UrlEncode(desc) + "&hasChild=' > Add Document </a>";
mytree.Nodes.Add(myNode);
string sql = "Select * from salesDocuments where nodeId = " + nodeId;
Session["TreeView"] = mytree;
SqlDSDocumentList.SelectCommand = sql;
documentRepeater.DataBind();
}
}
protected void DocumentRepeater_ItemDatabound(object sender, RepeaterItemEventArgs e)
{
RepeaterItem item = e.Item;
if ((item.ItemType == ListItemType.Item) || (item.ItemType == ListItemType.AlternatingItem))
{
DataRowView drv = (DataRowView)e.Item.DataItem;
string docName = drv.Row["docName"].ToString();
string childNodeId = drv.Row["childNodeId"].ToString();
TreeView mytree = (TreeView)Session["TreeView"];
TreeNode childNode = new TreeNode();
TreeNode parentNode = mytree.Nodes[0];
childNode.Text = "<a href='" + CMSContext.CurrentAliasPath + ".aspx?caller=docmaint&action=download&desc=&catId=" + childNodeId + "&hasChild=' >" + docName + " </a> <a href='" + CMSContext.CurrentAliasPath + ".aspx?caller=docmaint&action=deleteDoc&desc=&catId=" + childNodeId + "&hasChild=' > Delete </a> | <a href='" + CMSContext.CurrentAliasPath + ".aspx?caller=docmaint&action=replace&desc=&catId=" + childNodeId + "&hasChild=' > Replace </a>";
parentNode.ChildNodes.Add(childNode);
}
}
private void fileDownload(string fileName, string fileUrl)
{
Page.Response.Clear();
bool success = GenUtils.ResponseFile(Page.Request, Page.Response, fileName, fileUrl, 1024000);
if (!success)
LabelErrorList.Text = "Downloading Error, contact administrator!";
Page.Response.End();
}
protected void btnAddCategory_Click(object sender, EventArgs e)
{
LabelStatus.Text = "";
string script = "document.getElementById('newCatRight').className = 'NewCatStyleVisible';";
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "showstuff", script, true);
}
protected void btnSaveCategoryName_Click(object sender, EventArgs e)
{
string sqlStr = "";
if (Session["action"].ToString() == "edit")
{
sqlStr = "Update documentCategory Set description = '" + txtNewCatName.Text + "' Where nodeId = " + HiddenCatId.Value.ToString();
}
else
sqlStr = "Insert Into documentCategory (description) Values('" + txtNewCatName.Text + "')";
try
{
string connectionString = ConfigurationManager.ConnectionStrings["dealerextranetConnStr"].ToString();
SqlConnection myConnection = new SqlConnection(connectionString);
SqlCommand mySelectCommand = new SqlCommand(sqlStr, myConnection);
myConnection.Open();
SqlDataReader myReader;
myReader = mySelectCommand.ExecuteReader();
myConnection.Close();
string script = "document.getElementById('newCatRight').className = 'NewCatStyleHidden';";
ScriptManager.RegisterStartupScript(this.Page,this.GetType(), "hidestuff", script, true);
LabelStatus.Text = "Category added successfully";
txtNewCatName.Text = "";
CategoryRepeater.DataBind();
}
catch (Exception ex)
{
string strError = ex.Message.ToString();
}
}
protected void btnSaveDocument_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
try
{
if (FileUpload1.PostedFile.ContentLength > 50000000)
{
LabelErrorList.Text = "File being uploaded cannot be larger than 50 MBytes.";
return;
}
string fileName = FileUpload1.FileName.ToString();
int idx = fileName.LastIndexOf(".");
string ext = fileName.Substring(idx).ToLower().Trim();
FileUpload1.SaveAs(Server.MapPath("~/RohlCMS/DocumentLibrary/" + fileName));
SaveNewDocument(fileName, ext);
string script = "document.getElementById('rightDiv').className = 'rightDivStyleHidden';";
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "hidestuff", script, true);
}
catch (Exception ex)
{
string strError = ex.Message.ToString();
}
}
}
private void SaveNewDocument(String FileName, String ext)
{
string sqlStr = "";
try
{
if (Session["action"].ToString() == "add")
{
sqlStr = "Insert Into salesDocuments (nodeId, docName, fileType, createDate, lastUpdate) ";
sqlStr += " Values(" + HiddenCatId.Value.ToString() + ", '" + FileName + "', '" + ext + "', getdate(), getdate() )";
LabelStatus.Text = "Document added successfully";
}
if (Session["action"].ToString() == "replace")
{
sqlStr = "Update salesDocuments set docName = '" + FileName + "', fileType = '" + ext + "', lastUpdate = getdate() Where childNodeId = " + HiddenCatId.Value.ToString();
LabelStatus.Text = "Document replaced successfully";
}
string connectionString = ConfigurationManager.ConnectionStrings["dealerextranetConnStr"].ToString();
SqlConnection myConnection = new SqlConnection(connectionString);
SqlCommand mySelectCommand = new SqlCommand(sqlStr, myConnection);
myConnection.Open();
SqlDataReader myReader;
myReader = mySelectCommand.ExecuteReader();
myConnection.Close();
txtNewCatName.Text = "";
txtEditCatName.Text = "";
HiddenCatId.Value = "0";
CategoryRepeater.DataBind();
string script = "document.getElementById('newCatRight').className = 'NewCatStyleHidden';";
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "hideRightDivstuff", script, true);
}
catch (Exception ex)
{
string strError = ex.Message.ToString();
}
}
private void GetDocumentInfo(String childNodeId)
{
string sqlStr = "Select sd.fileType, sd.docName, sd.createDate, sd.lastUpdate, dc.description From salesDocuments sd, documentCategory dc ";
sqlStr += "Where dc.nodeId = sd.nodeId And sd.childNodeId = " + childNodeId;
try
{
string connectionString = ConfigurationManager.ConnectionStrings["dealerextranetConnStr"].ToString();
SqlConnection myConnection = new SqlConnection(connectionString);
SqlCommand mySelectCommand = new SqlCommand(sqlStr, myConnection);
myConnection.Open();
SqlDataReader myReader;
myReader = mySelectCommand.ExecuteReader();
string filetype = "";
while (myReader.Read())
{
filetype = myReader["fileType"].ToString();
DocNameLabel.Text = myReader["docName"].ToString();
CreatedateLabel.Text = myReader["createDate"].ToString();
LastUpdatedLabel.Text = myReader["lastUpdate"].ToString();
txtEditCatName.Text = myReader["description"].ToString();
}
switch (filetype.ToLower().Trim())
{
case ".xls":
DocTypeImage.ImageUrl = "../../RohlCMS/media/dealer_extranet/Excel-icon.png";
DocTypeImage.AlternateText = "Excel Document";
DocTypeImage.ToolTip = "Excel Document";
break;
case ".xlsx":
DocTypeImage.ImageUrl = "../../RohlCMS/media/dealer_extranet/Excel-icon.png";
DocTypeImage.AlternateText = "Excel Document";
DocTypeImage.ToolTip = "Excel Document";
break;
case ".pdf":
DocTypeImage.ImageUrl = "../../RohlCMS/media/dealer_extranet/Icon_pdf.gif";
DocTypeImage.AlternateText = "PDF Document";
DocTypeImage.ToolTip = "PDF Document";
break;
case ".doc":
DocTypeImage.ImageUrl = "../../RohlCMS/media/dealer_extranet/Word-16.gif";
DocTypeImage.AlternateText = "Word Document";
DocTypeImage.ToolTip = "Word Document";
break;
case ".docx":
DocTypeImage.ImageUrl = "../../RohlCMS/media/dealer_extranet/Word-16.gif";
DocTypeImage.AlternateText = "Word Document";
DocTypeImage.ToolTip = "Word Document";
break;
}
myConnection.Close();
}
catch (Exception ex)
{
string strError = ex.Message.ToString();
}
}
protected void btnDeleteDocument_Click(object sender, EventArgs e)
{
string sqlStr = "Delete salesDocuments Where childNodeId = " + HiddenCatId.Value.ToString(); ;
try
{
string connectionString = ConfigurationManager.ConnectionStrings["dealerextranetConnStr"].ToString();
SqlConnection myConnection = new SqlConnection(connectionString);
SqlCommand mySelectCommand = new SqlCommand(sqlStr, myConnection);
myConnection.Open();
SqlDataReader myReader;
myReader = mySelectCommand.ExecuteReader();
myConnection.Close();
File.Delete(MapPath("~/RohlCMS/DocumentLibrary/") + DocNameLabel.Text);
string script = "document.getElementById('newCatRight').className = 'NewCatStyleHidden';";
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "hidestuff", script, true);
LabelStatus.Text = "Document deleted successfully";
txtNewCatName.Text = "";
Session["action"] = "";
CategoryRepeater.DataBind();
}
catch (Exception ex)
{
string strError = ex.Message.ToString();
}
}
protected void btnCancelDelDoc_Click(object sender, EventArgs e)
{
txtNewCatName.Text = "";
txtEditCatName.Text = "";
HiddenCatId.Value = "0";
Session["action"] = "";
DocTypeImage.ImageUrl = "../../RohlCMS/media/dealer_extranet/filetype.gif";
string script = "document.getElementById('rightDiv').className = 'rightDivStyleHidden';";
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "hideRightDivstuff", script, true);
}
private void DeleteCategory(String NodeId)
{
string sqlStr = "Delete documentCategory Where nodeId = " + NodeId;
try
{
string connectionString = ConfigurationManager.ConnectionStrings["dealerextranetConnStr"].ToString();
SqlConnection myConnection = new SqlConnection(connectionString);
SqlCommand mySelectCommand = new SqlCommand(sqlStr, myConnection);
myConnection.Open();
SqlDataReader myReader;
myReader = mySelectCommand.ExecuteReader();
myConnection.Close();
}
catch (Exception ex)
{
string strError = ex.Message.ToString();
}
}
}