OK below is the code for two composite controls - FolderControl and FolderTaskPane - FolderTaskPane is used within FolderControl. FolderControl also uses another composite control called FolderContentsPane but I haven't included the code for that as it has no events associated with it.
Basically FolderTaskPane contains LinkButtons and ImageButtons whose events bubble up to the composite controls.
When these events eventually reach FolderControl then they can be used in an aspx page.
I have been trying to alter FolderControls appearance according to events fired. So lets say OnTaskAddFolderClick the width of the FolderControl should change. If I try this on postback nothing changes however if I refresh the page the width changes - it seems that the control is being rendered before the event can change the width property. The events always seem to be one postback behind.
FolderControl:
public class FolderControl: Control, INamingContainer {
private FolderTaskPane taskpane;
private FolderContentsPane contentpane;
private ImageButton logoutImage;
public event EventHandler TaskFolderUpClick;
public event EventHandler TaskAddFolderClick;
public event EventHandler TaskUploadFileClick;
public event EventHandler TaskEditUsersClick;
public event EventHandler TaskAddClientFolderClick;
public event EventHandler LogoutClick;
protected override void CreateChildControls()
{
this.Controls.Clear();
Context.Trace.Warn( "Folder Control Child Controls being created");
if ( this.Position != PositionEnum.None ) {
this.Controls.Add( new LiteralControl( "<div style=\"position: " + this.Position.ToString() + "; z-index:100; left: " + Left + "px; top: " + Top + "px; width: " + Width + "px; height: " + Height + "px; border: 2px ridge #D4D0C8; background-color: #FFFFFF;\">\n" ) );
} else {
this.Controls.Add( new LiteralControl( "<div style=\"width: " + Width + "px; height: " + Height + "px; border: 2px ridge #D4D0C8; background-color: #FFFFFF;\">\n" ) );
}
this.Controls.Add( new LiteralControl( "<table cellpadding=\"2\" cellspacing=\"0\" width=\"" + Width + "\">\n" ) );
this.Controls.Add( new LiteralControl( "<tr style=\"background-color: #3A5793;\">\n" ) );
this.Controls.Add( new LiteralControl( "<td height=\"16\" width=\"20\"><img src=\"/images/folder_small.gif\" width=\"16\" height=\"16\"></td>\n" ) );
if ( FolderPath == "/" ) {
this.Controls.Add( new LiteralControl( "<td height=\"16\" width=\"" + (Width - 40).ToString() + "\"><b style=\"color: #FFFFFF;\">Root</b></td>\n" ) );
} else {
this.Controls.Add( new LiteralControl( "<td height=\"16\" width=\"" + (Width - 40).ToString() + "\"><b style=\"color: #FFFFFF;\">" + FolderPath + "</b></td>\n" ) );
}
this.Controls.Add( new LiteralControl( "<td height=\"16\" width=\"20\">\n" ) );
logoutImage = new ImageButton();
logoutImage.ImageUrl = "/images/logout.gif";
logoutImage.ToolTip = "Logout Extranet";
logoutImage.AlternateText = "Logout Extranet";
logoutImage.BorderStyle = BorderStyle.None;
logoutImage.Width = 16;
logoutImage.Height = 16;
logoutImage.CommandName = "LogoutClick";
this.Controls.Add( logoutImage );
this.Controls.Add( new LiteralControl( "</td>\n</tr>\n</table>\n" ) );
taskpane = new FolderTaskPane();
taskpane.FolderPath = FolderPath;
taskpane.NewFolder = NewFolder;
taskpane.EditUsers = EditUsers;
taskpane.UploadFile = UploadFile;
taskpane.AddFolder = AddFolder;
taskpane.Position = PositionEnum.Absolute;
taskpane.Top = 24;
taskpane.Left = 4;
taskpane.Height = Height - 8 - taskpane.Top;
if ( Width < 399 ) {
taskpane.Width = taskpane.Width - (399 - Width) / 2;
}
this.Controls.Add( taskpane );
contentpane = new FolderContentsPane();
contentpane.FolderPath = FolderPath;
contentpane.ViewFiles = ViewFiles;
contentpane.Position = PositionEnum.Absolute;
contentpane.Top = 20;
contentpane.Left = 4 + taskpane.Width;
contentpane.Height = Height - 4 - contentpane.Top;
if ( Width < 399 ) {
contentpane.Width = taskpane.Width - ((399 - Width) / 2) + ((399 - Width) % 2);
} else {
contentpane.Width = Width - taskpane.Width - 4;
}
this.Controls.Add( contentpane );
this.Controls.Add( new LiteralControl( "</div>\n" ) );
}
protected override bool OnBubbleEvent( object source,
EventArgs e )
{
bool handled = false;
if (e is CommandEventArgs) {
CommandEventArgs ce = (CommandEventArgs)e;
if (ce.CommandName == "FolderUpClick") {
OnTaskFolderUpClick(ce);
handled = true;
} else if (ce.CommandName == "AddFolderClick") {
OnTaskAddFolderClick(ce);
handled = true;
} else if (ce.CommandName == "UploadFileClick") {
OnTaskUploadFileClick(ce);
handled = true;
} else if (ce.CommandName == "EditUsersClick") {
OnTaskEditUsersClick(ce);
handled = true;
} else if (ce.CommandName == "AddClientFolderClick") {
OnTaskAddClientFolderClick(ce);
handled = true;
} else if (ce.CommandName == "LogoutClick") {
OnLogoutClick(ce);
handled = true;
}
}
return handled;
}
protected virtual void OnTaskFolderUpClick (EventArgs e)
{
if (TaskFolderUpClick != null) {
TaskFolderUpClick(this,e);
}
}
protected virtual void OnTaskAddFolderClick (EventArgs e)
{
if (TaskAddFolderClick != null) {
TaskAddFolderClick(this,e);
}
}
protected virtual void OnTaskUploadFileClick (EventArgs e)
{
if (TaskUploadFileClick != null) {
TaskUploadFileClick(this,e);
}
}
protected virtual void OnTaskEditUsersClick (EventArgs e)
{
if (TaskEditUsersClick != null) {
TaskEditUsersClick(this,e);
}
}
protected virtual void OnTaskAddClientFolderClick (EventArgs e)
{
if (TaskAddClientFolderClick != null) {
TaskAddClientFolderClick(this,e);
}
}
protected virtual void OnLogoutClick (EventArgs e)
{
if (LogoutClick != null) {
LogoutClick(this,e);
}
}
public string FolderPath {
get
{
string f = (string) ViewState["FolderPath"];
return ( f == null ) ? String.Empty : (string) f;
}
set
{
ViewState["FolderPath"] = value;
this.SplitPath = value.Split( new char[] {'/'} );
}
}
protected string[] SplitPath {
get
{
object obj = ViewState["SplitPath"];
return (obj == null) ? new string[0] : (string[]) ViewState["SplitPath"];
}
set
{
ViewState["SplitPath"] = value;
this.FolderName = value[ value.Length - 1 ];
}
}
protected string FolderName {
get
{
string f = (string) ViewState["FolderName"];
return ( f == null ) ? String.Empty : (string) f;
}
set
{
ViewState["FolderName"] = value;
}
}
public bool NewFolder {
get
{
object obj = ViewState["NewFolder"];
return (obj == null) ? false : (bool) ViewState["NewFolder"];
}
set
{
ViewState["NewFolder"] = value;
}
}
public bool AddFolder {
get
{
object obj = ViewState["AddFolder"];
return (obj == null) ? false : (bool) ViewState["AddFolder"];
}
set
{
ViewState["AddFolder"] = value;
}
}
public bool UploadFile {
get
{
object obj = ViewState["UploadFile"];
return (obj == null) ? false : (bool) ViewState["UploadFile"];
}
set
{
ViewState["UploadFile"] = value;
}
}
public bool EditUsers {
get
{
object obj = ViewState["EditUsers"];
return (obj == null) ? false : (bool) ViewState["EditUsers"];
}
set
{
ViewState["EditUsers"] = value;
}
}
public bool ViewFiles {
get
{
object obj = ViewState["ViewFiles"];
return (obj == null) ? false : (bool) ViewState["ViewFiles"];
}
set
{
ViewState["ViewFiles"] = value;
}
}
public int Width {
get
{
object obj = ViewState["Width"];
return (obj == null) ? 600 : (int) ViewState["Width"];
}
set
{
ViewState["Width"] = value;
}
}
public int Height {
get
{
object obj = ViewState["Height"];
return (obj == null) ? 350 : (int) ViewState["Height"];
}
set
{
ViewState["Height"] = value;
}
}
public int Left {
get
{
object obj = ViewState["Left"];
return (obj == null) ? 0 : (int) ViewState["Left"];
}
set
{
ViewState["Left"] = value;
}
}
public int Top {
get
{
object obj = ViewState["Top"];
return (obj == null) ? 0 : (int) ViewState["Top"];
}
set
{
ViewState["Top"] = value;
}
}
public PositionEnum Position {
get
{
object obj = ViewState["Position"];
return (obj == null) ? PositionEnum.None : (PositionEnum) obj;
}
set
{
ViewState["Position"] = value;
}
}
}
FolderTaskPane:
public class FolderTaskPane: Control, INamingContainer {
private LinkButton upLink;
private LinkButton folderLink;
private LinkButton fileLink;
private LinkButton userLink;
private ImageButton upImage;
private ImageButton folderImage;
private ImageButton fileImage;
private ImageButton userImage;
public event EventHandler FolderUpClick;
public event EventHandler AddFolderClick;
public event EventHandler UploadFileClick;
public event EventHandler EditUsersClick;
public event EventHandler AddClientFolderClick;
protected override void CreateChildControls()
{
this.Controls.Clear();
Context.Trace.Warn( "Folder TaskPane Control Child Controls being created");
if ( this.Position != PositionEnum.None ) {
this.Controls.Add( new LiteralControl( "<div style=\"position: " + this.Position.ToString() + "; z-index:100; left: " + Left + "px; top: " + Top + "px; width: " + Width + "px; height: " + Height + "px; overflow: auto;\">\n" ) );
} else {
this.Controls.Add( new LiteralControl( "<div style=\"width: " + Width + "px; height: " + Height + "px; overflow: auto;\">\n" ) );
}
if ( FolderPath != String.Empty ) {
if ( Directory.Exists( Page.MapPath( "/files" + FolderPath ) ) ) {
if (this.AddFolder || this.UploadFile || this.EditUsers) {
this.Controls.Add( new LiteralControl( "<table cellpadding=\"4\" cellspacing=\"0\" style=\"border: 1px solid #D4D0C8; width: " + (Width - 20) + ";\">\n" ) );
this.Controls.Add( new LiteralControl( "<tr>\n" ) );
this.Controls.Add( new LiteralControl( "<th colspan=\"2\" style=\"font-family: Arial; font-size: 8pt; font-weight: bold; color: black; text-align: left; background-color: #D4D0C8;\">File and Folder Tasks</th>\n" ) );
this.Controls.Add( new LiteralControl( "</tr>\n" ) );
if ( FolderPath != "/" ) {
this.Controls.Add( new LiteralControl( "<tr style=\"background-color: #FFFFFF;\">\n<td>\n" ) );
upImage = new ImageButton();
upImage.ImageUrl = "/images/folderup.gif";
upImage.ToolTip = "Move up one level";
upImage.AlternateText = "Move up one level";
upImage.BorderStyle = BorderStyle.None;
upImage.Width = 16;
upImage.Height = 16;
upImage.CommandName = "FolderUpClick";
this.Controls.Add( upImage );
this.Controls.Add( new LiteralControl( "\n</td>\n<td>\n" ) );
upLink = new LinkButton();
upLink.Text = "Move up one level";
upLink.ToolTip = "Move up one level";
upLink.CommandName = "FolderUpClick";
this.Controls.Add ( upLink );
this.Controls.Add( new LiteralControl( "</td>\n</tr>\n" ) );
}
if ( this.AddFolder ) {
this.Controls.Add( new LiteralControl( "<tr style=\"background-color: #FFFFFF;\">\n<td>\n" ) );
if ( FolderPath == "/" ) {
folderImage = new ImageButton();
folderImage.ImageUrl = "/images/newfolder.gif";
folderImage.ToolTip = "Creates a new client folder";
folderImage.AlternateText = "Creates a new client folder";
folderImage.BorderStyle = BorderStyle.None;
folderImage.Width = 16;
folderImage.Height = 16;
folderImage.CommandName = "AddClientFolderClick";
this.Controls.Add( folderImage );
this.Controls.Add( new LiteralControl( "\n</td>\n<td>\n" ) );
folderLink = new LinkButton();
folderLink.Text = "Make a new client folder";
folderLink.ToolTip = "Creates a new client folder";
folderLink.CommandName = "AddClientFolderClick";
this.Controls.Add ( folderLink );
} else {
folderImage = new ImageButton();
folderImage.ImageUrl = "/images/newfolder.gif";
folderImage.ToolTip = "Creates a new folder";
folderImage.AlternateText = "Creates a new folder";
folderImage.BorderStyle = BorderStyle.None;
folderImage.Width = 16;
folderImage.Height = 16;
folderImage.CommandName = "AddFolderClick";
this.Controls.Add( folderImage );
this.Controls.Add( new LiteralControl( "\n</td>\n<td>\n" ) );
folderLink = new LinkButton();
folderLink.Text = "Make a new folder";
folderLink.ToolTip = "Creates a new folder";
folderLink.CommandName = "AddFolderClick";
this.Controls.Add ( folderLink );
}
this.Controls.Add( new LiteralControl( "</td>\n</tr>\n" ) );
if ( this.NewFolder ) {
this.Controls.Add( new LiteralControl( "<tr style=\"background-color: #FFFFFF;\">\n<td colspan=\"2\">\n" ) );
this.Controls.Add( new LiteralControl( "<input type=text>" ) );
this.Controls.Add( new LiteralControl( "</td>\n</tr>\n" ) );
}
}
if ( this.UploadFile && FolderPath != "/" ) {
this.Controls.Add( new LiteralControl( "<tr style=\"background-color: #FFFFFF;\">\n<td>\n" ) );
fileImage = new ImageButton();
fileImage.ImageUrl = "/images/uploadfile.gif";
fileImage.ToolTip = "Upload a new file";
fileImage.AlternateText = "Upload a new file";
fileImage.BorderStyle = BorderStyle.None;
fileImage.Width = 16;
fileImage.Height = 16;
fileImage.CommandName = "UploadFileClick";
this.Controls.Add( fileImage );
this.Controls.Add( new LiteralControl( "\n</td>\n<td>\n" ) );
fileLink = new LinkButton();
fileLink.Text = "Upload a new file";
fileLink.ToolTip = "Upload a new file";
fileLink.CommandName = "UploadFileClick";
this.Controls.Add ( fileLink );
this.Controls.Add( new LiteralControl( "</td>\n</tr>\n" ) );
}
if ( this.EditUsers && FolderPath != "/" ) {
this.Controls.Add( new LiteralControl( "<tr style=\"background-color: #FFFFFF;\">\n<td>\n" ) );
userImage = new ImageButton();
userImage.ImageUrl = "/images/editusers.gif";
userImage.ToolTip = "Edit user access";
userImage.AlternateText = "Edit user access";
userImage.BorderStyle = BorderStyle.None;
userImage.Width = 16;
userImage.Height = 16;
userImage.CommandName = "EditUsersClick";
this.Controls.Add( userImage );
this.Controls.Add( new LiteralControl( "\n</td>\n<td>\n" ) );
userLink = new LinkButton();
userLink.Text = "Edit user access";
userLink.ToolTip = "Edit user access";
userLink.CommandName = "EditUsersClick";
this.Controls.Add ( userLink );
this.Controls.Add( new LiteralControl( "</td>\n</tr>\n" ) );
}
this.Controls.Add( new LiteralControl( "</table>\n<br />\n" ) );
}
this.Controls.Add( new LiteralControl( "<table cellpadding=\"4\" cellspacing=\"0\" style=\"border: 1px solid #D4D0C8; width: " + (Width - 20) + ";\">\n" ) );
this.Controls.Add( new LiteralControl( "<tr>\n" ) );
this.Controls.Add( new LiteralControl( "<th style=\"font-family: Arial; font-size: 8pt; font-weight: bold; color: black; text-align: left; background-color: #D4D0C8;\">Details</th>\n" ) );
this.Controls.Add( new LiteralControl( "</tr>\n" ) );
this.Controls.Add( new LiteralControl( "<tr style=\"background-color: #FFFFFF;\">\n" ) );
if ( FolderPath == "/" ) {
this.Controls.Add( new LiteralControl( "<td>Root<br />\nFile Folder<br /><br/>Date Modified: " + String.Format( "{0:dd MMMM yyyy, HH:mm}", Directory.GetLastWriteTime( Page.MapPath( "/files" + FolderPath ) ) ) + "</td>\n</tr>\n" ) );
} else {
this.Controls.Add( new LiteralControl( "<td>" + FolderName + "<br />\nFile Folder<br /><br/>Date Modified: " + String.Format( "{0:dd MMMM yyyy, HH:mm}", Directory.GetLastWriteTime( Page.MapPath( "/files" + FolderPath ) ) ) + "</td>\n</tr>\n" ) );
}
this.Controls.Add( new LiteralControl( "</table>\n" ) );
} else {
this.Controls.Add( new LiteralControl( "<table cellpadding=\"4\" cellspacing=\"0\" style=\"border: 1px solid #D4D0C8; width: " + (Width - 20) + ";\">\n" ) );
this.Controls.Add( new LiteralControl( "<tr>\n" ) );
this.Controls.Add( new LiteralControl( "<th style=\"font-family: Arial; font-size: 8pt; font-weight: bold; color: black; text-align: left; background-color: #D4D0C8;\">Details</th>\n" ) );
this.Controls.Add( new LiteralControl( "</tr>\n" ) );
this.Controls.Add( new LiteralControl( "<tr style=\"background-color: #FFFFFF;\">\n" ) );
this.Controls.Add( new LiteralControl( "<td><b style=\"color: red;\">Error: Folder not found!</b></td>\n</tr>\n" ) );
this.Controls.Add( new LiteralControl( "</table>\n" ) );
}
} else {
this.Controls.Add( new LiteralControl( "<table cellpadding=\"4\" cellspacing=\"0\" style=\"border: 1px solid #D4D0C8; width: " + (Width - 20) + ";\">\n" ) );
this.Controls.Add( new LiteralControl( "<tr>\n" ) );
this.Controls.Add( new LiteralControl( "<th style=\"font-family: Arial; font-size: 8pt; font-weight: bold; color: black; text-align: left; background-color: #D4D0C8;\">Details</th>\n" ) );
this.Controls.Add( new LiteralControl( "</tr>\n" ) );
this.Controls.Add( new LiteralControl( "<tr style=\"background-color: #FFFFFF;\">\n" ) );
this.Controls.Add( new LiteralControl( "<td><b style=\"color: red;\">Error: No folder specified!</b></td>\n</tr>\n" ) );
this.Controls.Add( new LiteralControl( "</table>\n" ) );
}
this.Controls.Add( new LiteralControl( "</div>\n" ) );
}
protected override bool OnBubbleEvent( object source,
EventArgs e )
{
bool handled = false;
if (e is CommandEventArgs) {
CommandEventArgs ce = (CommandEventArgs)e;
if (ce.CommandName == "FolderUpClick") {
OnFolderUpClick(ce);
handled = false;
} else if (ce.CommandName == "AddFolderClick") {
OnAddFolderClick(ce);
handled = false;
} else if (ce.CommandName == "UploadFileClick") {
OnUploadFileClick(ce);
handled = false;
} else if (ce.CommandName == "EditUsersClick") {
OnEditUsersClick(ce);
handled = false;
} else if (ce.CommandName == "AddClientFolderClick") {
OnAddClientFolderClick(ce);
handled = false;
}
}
return handled;
}
protected virtual void OnFolderUpClick (EventArgs e)
{
if (FolderUpClick != null) {
FolderUpClick(this,e);
}
}
protected virtual void OnAddFolderClick (EventArgs e)
{
if (AddFolderClick != null) {
AddFolderClick(this,e);
}
}
protected virtual void OnUploadFileClick (EventArgs e)
{
if (UploadFileClick != null) {
UploadFileClick(this,e);
}
}
protected virtual void OnEditUsersClick (EventArgs e)
{
if (EditUsersClick != null) {
EditUsersClick(this,e);
}
}
protected virtual void OnAddClientFolderClick (EventArgs e)
{
if (AddClientFolderClick != null) {
AddClientFolderClick(this,e);
}
}
public string FolderPath {
get
{
string f = (string) ViewState["FolderPath"];
return ( f == null ) ? String.Empty : (string) f;
}
set
{
ViewState["FolderPath"] = value;
this.SplitPath = value.Split( new char[] {'/'} );
}
}
protected string[] SplitPath {
get
{
object obj = ViewState["SplitPath"];
return (obj == null) ? new string[0] : (string[]) ViewState["SplitPath"];
}
set
{
ViewState["SplitPath"] = value;
this.FolderName = value[ value.Length - 1 ];
}
}
protected string FolderName {
get
{
string f = (string) ViewState["FolderName"];
return ( f == null ) ? String.Empty : (string) f;
}
set
{
ViewState["FolderName"] = value;
}
}
public bool NewFolder {
get
{
object obj = ViewState["NewFolder"];
return (obj == null) ? false : (bool) ViewState["NewFolder"];
}
set
{
ViewState["NewFolder"] = value;
}
}
public bool AddFolder {
get
{
object obj = ViewState["AddFolder"];
return (obj == null) ? false : (bool) ViewState["AddFolder"];
}
set
{
ViewState["AddFolder"] = value;
}
}
public bool UploadFile {
get
{
object obj = ViewState["UploadFile"];
return (obj == null) ? false : (bool) ViewState["UploadFile"];
}
set
{
ViewState["UploadFile"] = value;
}
}
public bool EditUsers {
get
{
object obj = ViewState["EditUsers"];
return (obj == null) ? false : (bool) ViewState["EditUsers"];
}
set
{
ViewState["EditUsers"] = value;
}
}
public int Width {
get
{
object obj = ViewState["Width"];
return (obj == null) ? 183 : (int) ViewState["Width"];
}
set
{
ViewState["Width"] = value;
}
}
public int Height {
get
{
object obj = ViewState["Height"];
return (obj == null) ? 400 : (int) ViewState["Height"];
}
set
{
ViewState["Height"] = value;
}
}
public int Left {
get
{
object obj = ViewState["Left"];
return (obj == null) ? 0 : (int) ViewState["Left"];
}
set
{
ViewState["Left"] = value;
}
}
public int Top {
get
{
object obj = ViewState["Top"];
return (obj == null) ? 0 : (int) ViewState["Top"];
}
set
{
ViewState["Top"] = value;
}
}
public PositionEnum Position {
get
{
object obj = ViewState["Position"];
return (obj == null) ? PositionEnum.None : (PositionEnum) obj;
}
set
{
ViewState["Position"] = value;
}
}
}
Any help would be appreciated.
DJ