Welcome Guest Search | Active Topics | Sign In | Register

Extending EODatetime Picker. Options
Thowfiq Raja
Posted: Friday, February 25, 2011 6:54:13 AM
Rank: Member
Groups: Member

Joined: 2/18/2011
Posts: 11
Hi,

My requirement is to create server control extending EO datetime picker and adding it dynamically to the page.

these are steps I followed.

1. I've create my own control deriving it from DatePicker class.
2. In my page I 've created a dynamic table and this control to it.
3. now I've added this table to panel. and stored this dynamic table in a session.
4. when a postback occurs on page, I'm adding table(which is stored in session) to the panel.

at point no 4, Iam getting a error 'Unable to cast object of type 'System.String' to type 'EO.Web.DateCollection'.

Pls tell me why this error is coming when I'm not using any datecollection.

Thanks
Thowfiq
eo_support
Posted: Friday, February 25, 2011 8:28:05 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,194
Hi,

Can you provide a test page (make sure it runs) so that we can take a look?

Thanks!
Thowfiq Raja
Posted: Monday, February 28, 2011 2:06:33 AM
Rank: Member
Groups: Member

Joined: 2/18/2011
Posts: 11
Hi,

I have created a test page(Controls.aspx) which depends on 3 other class files which should be placed in App_code folder.

steps for regenerating the issue:
1. just create a website and add controls.aspx file
2. add below 3 code files to app_code folder.
Quote:
IDynamicFilterControl.cs
DropdownServerControl.cs
EODateTimeServerControl.cs

3. now run the page and click on load controls button which loads dynamic control.
4. Change the dropdown value which will cause postback, while reloading the controls it throws a error 'Unable to cast object of type 'System.String' to type 'EO.Web.DateCollection'.

note: code is below for all above listed files.

My sample page is Controls.aspx

Code: HTML/ASPX
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Controls.aspx.cs" Inherits="WebConsole.WCToolsManager.Controls" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:Button ID="btnLoad" runat="server" onclick="btnLoad_Click" 
			Text="Load Controls" />
		<asp:Panel ID="pnlControls" runat="server">
		</asp:Panel>
    <div>
    
    </div>
    </form>
</body>
</html>


following Controls.aspx.cs

Code: C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

namespace WebConsole.WCToolsManager
{
	public partial class Controls : System.Web.UI.Page
	{
		ArrayList list = new ArrayList();
		private Table FilterControlsTable
		{
			get
			{
				return (Table)Session["FilterControlsTable"];
			}
			set
			{
				Session["FilterControlsTable"] = value;
			}
		}
		
		protected void Page_Load(object sender, EventArgs e)
		{
			list.Add("HF");
			list.Add("MF");
			list.Add("KF");

			if (FilterControlsTable != null)
			{
				pnlControls.Controls.Clear();
				pnlControls.Controls.Add(FilterControlsTable);
			}
		}
		protected void btnLoad_Click(object sender, EventArgs e)
		{
			Table tableFilterControls = new Table();
			for (int i = 0; i < 2; i++)
			{
				Control DynamicFilterControl = new Control();

				if (i == 0)
				{
					DynamicFilterControl = new DropdownServerControl();
					IDynamicFilterControl DerivedDynamicFilterControl = (IDynamicFilterControl)DynamicFilterControl;
					DerivedDynamicFilterControl.SetInitialValue(list);					 
				}
				else
				{
					DynamicFilterControl = new EODateTimeServerControl();
				}

				TableRow tr = new TableRow();
				TableCell td1 = new TableCell();
				td1.Width = 350;
				Label objlabel = new Label();
				objlabel.Text = "Control " + i.ToString();
				objlabel.Style.Add("text-align", "Right");
				objlabel.Attributes.Add("Runat", "Server");
				td1.Controls.Add(objlabel);

				TableCell td2 = new TableCell();
				td2.Controls.Add(DynamicFilterControl);

				tr.Cells.Add(td1);
				tr.Cells.Add(td2);

				tableFilterControls.Rows.Add(tr);

				pnlControls.Controls.Clear();
				pnlControls.Controls.Add(tableFilterControls);
				FilterControlsTable = tableFilterControls;
			}
		}
	}
}



Now the 3 files which needs to placed in App_code Folder.

1. IDynamicFilterControl.cs
Code: C#
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace WebConsole.WCToolsManager
{
    public interface IDynamicFilterControl
    {
        string ControlID { get;set;}
        string Title { get;set;}
       
        void ListenToAssociatedControl();
        void SetInitialValue(ArrayList arList);
        void ClearValue();

    }
}


2. DropdownServerControl.cs
Code: C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Collections;

namespace WebConsole.WCToolsManager
{
	public class DropdownServerControl : System.Web.UI.WebControls.DropDownList, IDynamicFilterControl, IPostBackEventHandler
	{
		#region private members

		private string _strControlId;
		private string _strTitle = string.Empty; 

		#endregion private members

		#region IDynamicFilterControl Properties

		public string Title
		{
			get
			{
				return _strTitle;
			}
			set
			{
				_strTitle = value;
			}
		}
		public string ControlID
		{
			get
			{
				return _strControlId;
			}
			set
			{
				_strControlId = value;
			}
		}	
		#endregion IDynamicFilterControl Properties

		public DropdownServerControl()
		{
			try
			{
				this.Attributes.Add("Runat", "Server");
				this.AutoPostBack = true;
				this.EnableViewState = true;
				this.TrackViewState();
				this.Width = 200;
				this.Height = 20;
				this.EnableViewState = true;
			}
			catch
			{
			}
		}

		void VCComboBox_SelectedIndexChanged(object sender, EventArgs e)
		{
			
		}

		#region IDynamicFilterControl methods
				
		public void ListenToAssociatedControl()
		{
			
		}
			
		public void SetInitialValue(ArrayList arList)
		{
			this.DataSource = arList;
			this.DataBind();
			this.SelectedIndexChanged += new EventHandler(VCComboBox_SelectedIndexChanged);
		}

		public void ClearValue()
		{
			this.SelectedIndex = 1; // set the selected index to none
			BindChildControlValues();
		}

		
		void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
		{
			//BindChildControlValues();
		}
		private void BindChildControlValues()
		{
			
		}

		#endregion IDynamicFilterControl methods
	}
}


3. EODateTimeServerControl.cs
Code: C#
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using EO.Web;

namespace WebConsole.WCToolsManager
{
	public class EODateTimeServerControl : DatePicker, IDynamicFilterControl
	{
		#region private members

		private string _strControlId;
		private string _strTitle = string.Empty;

		#endregion private members

		#region IDynamicFilterControl Properties
		public string Title
		{
			get
			{
				return _strTitle;
			}
			set
			{
				_strTitle = value;
			}
		}

		public string ControlID
		{
			get
			{
				return _strControlId;
			}
			set
			{
				_strControlId = value;
			}
		}
		
		#endregion IDynamicFilterControl Properties
		
		public EODateTimeServerControl()
		{
			this.Attributes.Add("Runat", "Server");
			this.Height = 20;
			this.Width = 195;
			this.EnableViewState = true;
		}

		#region IDynamicFilterControl methods


		public void ListenToAssociatedControl()
		{
		}

		public void SetInitialValue(ArrayList arList)
		{
			string str = (arList[0].ToString());
			if (!string.IsNullOrEmpty(str))
				this.SelectedDate = Convert.ToDateTime(str);
			else
				this.SelectedDate = Convert.ToDateTime("001-01-01");

		}
		
		public void ClearValue()
		{
			this.SelectedDate = Convert.ToDateTime("001-01-01");
		}


		#endregion IDynamicFilterControl methods

	}
}




Thanks
Thowfiq
eo_support
Posted: Monday, February 28, 2011 9:48:45 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,194
Hi,

That will not work. You can not save a control from a previous request and reuse it for a new request. As a general rule, you can only use a control for rendering once (just as ASP.NET recreate a new Page object for every request). Using it for rendering multiple times is usually not a problem for simple controls, but for complicated controls you will often run into problems.

Thanks!
Thowfiq Raja
Posted: Monday, February 28, 2011 11:12:36 PM
Rank: Member
Groups: Member

Joined: 2/18/2011
Posts: 11
Hi,

Is there any alternative solution to achieve the same? please guide me.

Thanks
Thowfiq
eo_support
Posted: Tuesday, March 1, 2011 7:26:06 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,194
No. You just have to recreate the controls.

Thanks


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.