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