无效的Cast异常问题

时间:2014-02-12 06:55:34

标签: c# asp.net

在下面的代码中,我得到了一个无效的强制转换异常由用户代码处理(即)无法将类型为“System.Web.UI.WebControls.DataList”的对象强制转换为在throw ex处键入“usercontrol.Fields”。 请帮我解决这个问题。 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;
        }

0 个答案:

没有答案