ASP.NET中复合控件的设计时错误

时间:2011-11-08 06:58:18

标签: c# asp.net composite

我创建了一个复合控件。它没有构建错误。

在复合控件中,我正在使用实体框架对象

问题是在设计窗口中我收到错误消息,如PIC:

所示

Composite Control Error Image

构建期间没有错误,复合控制效果很好。

我只是想从“设计时间”窗口中删除此错误

编辑:

就像我添加复合控件一样:

 <tr>
        <td colspan="3">
             <cc1:AdditionalColumnsCustomControl  ID="AdditionalColumnsCustomControl1" runat="server"
         MainCssClass="A" ControlMode="New" TableID="1" AreValidatorsStatic="true"
        CustomControlWrapper="TableWrapper" ValidationGroup="vl" />
        </td>
    </tr>

在这种情况下,我如何编码以摆脱此设计表面错误。

复合COntrol SOurce COde:

 public class AddRowCustomControl : CompositeControl
{
    public void Clear()
    {
        Control c = (Control)FindControl(controlID);

        if (c.GetType() == typeof(DropDownList))
        {
            DropDownList dl = c as DropDownList;

            ListItem lsa = dl.SelectedItem;

            lsa.Selected = false;


        }
        else if (c.GetType() == typeof(ListBox))
        {
            ListBox lb = c as ListBox;

            foreach (ListItem ls in lb.Items)
            {
                if (ls.Selected)
                {
                    ls.Selected = false;
                }

            }

        }
        else if (c.GetType() == typeof(RadioButtonList))
        {
            RadioButtonList rl = c as RadioButtonList;

            foreach (ListItem ls in rl.Items)
            {
                if (ls.Selected)
                {
                    ls.Selected = false;
                }

            }

        }
        else if (c.GetType() == typeof(CheckBoxList))
        {
            CheckBoxList cl = c as CheckBoxList;

            foreach (ListItem ls in cl.Items)
            {
                if (ls.Selected)
                {
                    ls.Selected = false;
                }
            }

        }
        else if (c.GetType() == typeof(CheckBox))
        {
            CheckBox chk = c as CheckBox;

            chk.Checked = false;

        }

        else
        {
            TextBox tx = c as TextBox;

            tx.Text = "";

        }

    }

    public void Rebind()
    {
        Control c = (Control)FindControl(controlID);

        fillControl(ref c  , RowID , ColumnID);

    }

    public string SaveControlValuesInDB()
    {
        using (ExtEntities context = new ExtEntities())
        {
            if (RowID != null && ColumnID != null)
            {
                EntityData ed = context.EntityDatas.Where(p => p.ColID == ColumnID && p.RowID == RowID).FirstOrDefault();

                if (ed == null)
                {
                    ed = new EntityData();
                    ed.RowID = Convert.ToInt32(RowID);
                    ed.ColID = ColumnID;
                    ed.ColValue = ControlValue;
                    context.AddToEntityDatas(ed);

                    context.SaveChanges();

                    return "Successfully Added";
                }
                else
                {
                    ed.ColValue = ControlValue;
                    context.SaveChanges();

                    return "Successfully Updated";
                }

            }
            else
            {
                return "Exception Invalid Row ID";
            }

        }


    }

    public int? RowID
    {
        get;
        set;
    }

    public enum Modes
    {
        New = 0,
        Modify = 1,

        ModifyInGrid = 2,

    }

    public Modes? ControlMode
    {
        get;
        set;
    }

    public int ColumnID
    {
        get;
        set;
    }

    public string ValidationGroup
    {
        get;
        set;
    }

    /// <summary>
    /// Specifes the Display mode of the Validators "Static or Dynamic"
    /// </summary>
    public bool? AreValidatorsStatic
    {
        get;
        set;
    }

    /// <summary>
    /// If true, the wrapper will be DIV's else Table
    /// </summary>
    public Wrapper CustomControlWrapper
    {
        get;
        set;
    }

    public enum Wrapper
    {
        DivWrapper = 0,
        TableWrapper = 1,

    }

    /// <summary>
    /// Css Class Name for Custom Control
    /// </summary>
    public string MainCssClass
    {
        get;
        set;
    }

    /// <summary>
    /// Css Class Name for Custom Control Label
    /// </summary>
    public string LabelCssClass
    {
        get;
        set;
    }

    /// <summary>
    /// Css Class Name for Custom Control's Main Control 
    /// </summary>
    public string ControlCssClass
    {
        get;
        set;
    }

    /// <summary>
    /// Css Class Name for Custom Control's Validators
    /// </summary>
    public string ValidatorCssClass
    {
        get;
        set;
    }

    protected override void OnLoad(EventArgs e)
    {
        if (AreValidatorsStatic == null)
        {
            AreValidatorsStatic = false;
        }

        if (CustomControlWrapper == null)
        {
            CustomControlWrapper = Wrapper.DivWrapper;
        }

        if (string.IsNullOrEmpty(MainCssClass))
        {
            MainCssClass = "CustomControlMainClass";
        }
        if (string.IsNullOrEmpty(ControlCssClass))
        {
            ControlCssClass = "ControlCssClass";
        }
        if (string.IsNullOrEmpty(LabelCssClass))
        {
            LabelCssClass = "LabelCssClass";
        }
        if (string.IsNullOrEmpty(ValidatorCssClass))
        {
            ValidatorCssClass = "ValidatorCssClass";
        }

        if (ControlMode == null)
        {
            ControlMode = Modes.New;
        }

        base.OnLoad(e);

    }


    string controlID = "ControlID";

    public string ControlValue
    {
        get
        {
            Control c = (Control)FindControl(controlID);

            StringBuilder sb = new StringBuilder();

            if (c.GetType() == typeof(DropDownList))
            {
                DropDownList dl = c as DropDownList;

                sb.Append(dl.Text);

                return sb.ToString();
            }
            else if (c.GetType() == typeof(ListBox))
            {
                ListBox lb = c as ListBox;
                foreach (ListItem item in lb.Items)
                {
                    if (item.Selected)
                    {
                        sb.Append(item.Text + "`");
                    }
                }
                return sb.ToString().TrimEnd('`');
            }
            else if (c.GetType() == typeof(RadioButtonList))
            {
                RadioButtonList rl = c as RadioButtonList;

                sb.Append(rl.SelectedItem.Value);
                return sb.ToString();


            }
            else if (c.GetType() == typeof(CheckBoxList))
            {
                CheckBoxList cl = c as CheckBoxList;

                foreach (ListItem li in cl.Items)
                {
                    if (li.Selected == true)
                    {
                        sb.Append(li.Text + "`");
                    }
                }

                return sb.ToString().TrimEnd('`');

            }
            else if (c.GetType() == typeof(CheckBox))
            {
                CheckBox chk = c as CheckBox;

                return chk.Checked ? "TRUE" : "";
            }

            else
            {
                TextBox tx = c as TextBox;
                return tx.Text;
            }

        }

    }

    private void AddOptions(string[] options, ref Control c)
    {
        if (c.GetType() == typeof(DropDownList))
        {
            DropDownList dl = c as DropDownList;
            foreach (string item in options)
            {
                dl.Items.Add(item);
            }

            c = dl;
        }
        else if (c.GetType() == typeof(ListBox))
        {
            ListBox lb = c as ListBox;
            foreach (string item in options)
            {
                lb.Items.Add(item);
            }

            c = lb;
        }
        else if (c.GetType() == typeof(RadioButtonList))
        {
            RadioButtonList rl = c as RadioButtonList;
            foreach (string item in options)
            {
                rl.Items.Add(item);
            }

            c = rl;
        }
        else if (c.GetType() == typeof(CheckBoxList))
        {
            CheckBoxList cl = c as CheckBoxList;
            foreach (string item in options)
            {
                cl.Items.Add(item);
            }

            c = cl;
        }

    }

    protected override void CreateChildControls()
    {

        string ts = MainCssClass;

        using (ExtEntities context = new ExtEntities())
        {
            EntityAttribute _Attribute = context.EntityAttributes.Where(p => p.ID == ColumnID).FirstOrDefault();

            if (_Attribute != null)
            {
                Panel pnl = new Panel();

                string[] _options = null;

                if (_Attribute.ControlOptions != null)
                {
                    _options = _Attribute.ControlOptions.Split('`');
                }
                Label lt = new Label();
                lt.ID = "LabelID";
                lt.Text = _Attribute.ColumnLabel;

                Control c = null;

                int? MaxLength = _Attribute.MaxLength;

                bool Invalidate_Validator = false;

                switch (_Attribute.AttributeTypeID)
                {
                    case 1:
                        TextBox sl = new TextBox();
                        if (_Attribute.MaxLength != null)
                            sl.MaxLength = Convert.ToInt32(_Attribute.MaxLength);
                        c = sl;
                        break;
                    case 2:
                        TextBox ml = new TextBox();
                        ml.TextMode = TextBoxMode.MultiLine;
                        if (_Attribute.MaxLength != null)
                            ml.MaxLength = Convert.ToInt32(_Attribute.MaxLength);
                        c = ml;
                        break;
                    case 3:
                        DropDownList dl = new DropDownList();
                        c = dl;
                        break;
                    case 4:
                        ListBox lb = new ListBox();
                        lb.SelectionMode = ListSelectionMode.Multiple;
                        c = lb;
                        break;
                    case 5:
                        TextBox p = new TextBox();
                        p.TextMode = TextBoxMode.Password;
                        if (_Attribute.MaxLength != null)
                            p.MaxLength = Convert.ToInt32(_Attribute.MaxLength);
                        c = p;
                        break;
                    case 6:
                        CheckBox ck = new CheckBox();
                        Invalidate_Validator = true;
                        ck.Text = _options != null ? _options[0] : "N/A";
                        c = ck;
                        break;
                    //case 7:
                    //    RadioButton rb = new RadioButton();
                    //    rb.Text = _options != null ? _options[0] : "N/A";
                    //    c = rb;
                    //    break;
                    case 8:
                        RadioButtonList rb_list = new RadioButtonList();
                        c = rb_list;
                        break;
                    case 9:
                        CheckBoxList ck_list = new CheckBoxList();
                        Invalidate_Validator = true;
                        c = ck_list;
                        break;
                    default:
                        break;
                }

                RequiredFieldValidator req = null;

                RegularExpressionValidator regx = null;

                AddOptions(_options, ref c);

                c.ID = controlID;

                if (!Invalidate_Validator)
                {
                    if (_Attribute.Mandatory)
                    {
                        req = new RequiredFieldValidator();

                        req.ControlToValidate = c.ID;
                        req.ErrorMessage = _Attribute.ValidationMessage;
                        req.Text = "*";
                        req.Display = AreValidatorsStatic == true ? ValidatorDisplay.Static : ValidatorDisplay.Dynamic;
                        req.ValidationGroup = ValidationGroup;
                    }

                    if (_Attribute.RegularExpressionValue != null)
                    {
                        regx = new RegularExpressionValidator();

                        regx.ControlToValidate = c.ID;
                        regx.ErrorMessage = _Attribute.ValidationMessage;
                        regx.Text = "*";
                        regx.ValidationGroup = ValidationGroup;
                        regx.ValidationExpression = _Attribute.RegularExpressionValue;
                        regx.Display = AreValidatorsStatic == true ? ValidatorDisplay.Static : ValidatorDisplay.Dynamic;

                    }
                }


                if (ControlMode == Modes.Modify)
                {
                    fillControl(ref c, RowID, ColumnID);
                }


                if (CustomControlWrapper == Wrapper.DivWrapper)
                {
                    pnl.Controls.Add(new LiteralControl(@"<div class=""" + MainCssClass + @"""><div class=""" + LabelCssClass + @""">"));

                    if (ControlMode != Modes.ModifyInGrid)
                        pnl.Controls.Add(lt);

                    pnl.Controls.Add(new LiteralControl(@"</div><div class=""" + ControlCssClass + @""">"));
                    pnl.Controls.Add(c);
                    pnl.Controls.Add(new LiteralControl(@"</div><div class=""" + ValidatorCssClass + @""">"));

                    if (req != null)
                        pnl.Controls.Add(req);
                    if (regx != null)
                        pnl.Controls.Add(regx);

                    pnl.Controls.Add(new LiteralControl(@"</div>"));

                    pnl.Controls.Add(new LiteralControl(@"</div>"));

                }
                else
                {
                    pnl.Controls.Add(new LiteralControl(@"<table class=""" + MainCssClass + @"""><tr><td class=""" + LabelCssClass + @""">"));

                    if (ControlMode != Modes.ModifyInGrid)
                        pnl.Controls.Add(lt);

                    pnl.Controls.Add(new LiteralControl(@"</td><td class=""" + ControlCssClass + @""">"));
                    pnl.Controls.Add(c);
                    pnl.Controls.Add(new LiteralControl(@"</td><td class=""" + ValidatorCssClass + @""">"));

                    if (req != null)
                        pnl.Controls.Add(req);
                    if (regx != null)
                        pnl.Controls.Add(regx);

                    pnl.Controls.Add(new LiteralControl(@"</td></tr>"));

                    pnl.Controls.Add(new LiteralControl(@"</table>"));

                }


                Controls.Add(pnl);

            }


        }
    }

    private void fillControl(ref Control c, int? RowID, int ColumnID)
    {
        using (ExtEntities context = new ExtEntities())
        {
            EntityData obj = context.EntityDatas.Where(p => p.RowID == RowID && p.ColID == ColumnID).FirstOrDefault();

            if (obj != null)
            {

                string[] values = obj.ColValue.Split('`');

                string value = obj.ColValue;

                if (c.GetType() == typeof(DropDownList))
                {
                    DropDownList dl = c as DropDownList;

                    dl.Items.FindByText(value).Selected = true;

                }
                else if (c.GetType() == typeof(ListBox))
                {
                    ListBox lb = c as ListBox;

                    foreach (var item in values)
                    {
                        lb.Items.FindByText(value).Selected = true;
                    }

                }
                else if (c.GetType() == typeof(RadioButtonList))
                {
                    RadioButtonList rl = c as RadioButtonList;

                    foreach (var item in values)
                    {
                        rl.Items.FindByText(item).Selected = true;
                    }

                }
                else if (c.GetType() == typeof(CheckBoxList))
                {
                    CheckBoxList cl = c as CheckBoxList;

                    foreach (var item in values)
                    {
                        cl.Items.FindByText(value).Selected = true;
                    }


                }
                else if (c.GetType() == typeof(CheckBox))
                {
                    CheckBox chk = c as CheckBox;

                    chk.Checked = value == "TRUE" ? true : false;

                }

                else
                {
                    TextBox tx = c as TextBox;

                    tx.Text = value;

                }
            }
        }

    }

由于

感谢任何帮助。

2 个答案:

答案 0 :(得分:2)

这是因为在设计时EF对象不可用/实例化。可以验证您的控件当前是否显示在具有DesignMode属性(MSDN doc)的设计图面上:

if(this.DesignMode == false)
{
   //do normal instantiations of objects etc.
}
else
{
   //do work related to creating a design time view of your control
}

答案 1 :(得分:1)

听起来你有一个设计师无法解决的依赖。您可以使用DesignMode标志来帮助解决此问题: http://msdn.microsoft.com/en-us/library/system.componentmodel.component.designmode.aspx