In the below code i got a invalid cast exception was handled by user code (ie)Unable to cast object of type 'System.Web.UI.WebControls.DataList' to type 'usercontrol.Fields' at throw ex. Please help me to solve the issue. Field.ascx
protected void cbField_Dependent(object sender, EventArgs e)
{
try
{
if (this.ParentFieldInfo != string.Empty)
{
Fields gForm = (Fields)this.Parent.Parent;
string strParentVal = gForm.GetParentSelection(ParentFieldInfo);
int NoOfItemsAdded = 0;
bool bDefaultSelection = false;
this.cbField.Items.Clear();
for (int iRow = 0; iRow < EnumRows.Length; iRow++)
{
if (EnumRows[iRow]["DependencyEnumText"].ToString().Equals(strParentVal) || EnumRows[iRow]["DependencyEnumText"].ToString().Equals(string.Empty))
{
this.cbField.Items.Add(EnumRows[iRow]["EnumText"].ToString());
if (Boolean.Parse(EnumRows[iRow]["DefaultSelection"].ToString()))
{
this.cbField.SelectedIndex = NoOfItemsAdded;
bDefaultSelection = true;
if (this.strValue == null || this.strValue == string.Empty)
this.strValue = cbField.Items[iRow].ToString();
}
NoOfItemsAdded++;
}
}
if (!bDefaultSelection && this.cbField.Items.Count > 0)
this.cbField.SelectedIndex = 0;
}
}
catch (Exception ex)
{
throw ex;
}
}
feilds.ascx
public string GetParentSelection(string strFieldName)
{
try
{
for (int iItems = 0; iItems < this.Items.Count; iItems++)
{
if (this.Items[iItems].FieldLabel == strFieldName)
{
return this.Items[iItems].strValue;
}
}
}
catch (Exception ex)
{
throw ex;
}
return string.Empty;
}