2012-04-11 41 views
1

我想要做的是做投需要我的控件添加到Ext.Net.Panel鑄有指定某種類型的變量

這是我的代碼示例:

Ext.Net.Panel panel = new Ext.Net.Panel(); 
    Control control = (Control) null;//here can be any Ext.Net type as well as NumeticField for example Ext.Net.TextArea and so on 
    switch (infoAttribute.Type) 
           { 
             case PropertyTypeAttrib.Text: 
              control = (Control)new Ext.Net.TextField(); 
              ((TextField)control).FieldLabel = infoAttribute.HeadLine; 
              control.ID = propertyHelper.Name; 
              //panel.Items.Add((TextField)control); 
             typeToCast = typeof (TextField); 
             break; 
            case PropertyTypeAttrib.FileUrl: 
             control = (Control) new Ext.Net.HyperLink(); 
             control.ID = propertyHelper.Name; 
             ((Ext.Net.Label) control).FieldLabel = infoAttribute.HeadLine; 

             //panel.Items.Add((Ext.Net.HyperLink) control); 
             typeToCast = typeof (Ext.Net.HyperLink); 
             break; 
            case PropertyTypeAttrib.Enum: 
             control = (Control) new MMPControls.Web.ComboBoxEnumExt(); 
             ((MMPControls.Web.ComboBoxEnumExt) control).EnumerationTypeName = 
              propertyHelper.PropertyType.Name; 
             control.ID = propertyHelper.Name; 
             ((MMPControls.Web.ComboBoxEnumExt) control).FieldLabel = infoAttribute.HeadLine; 
             //panel.Items.Add((MMPControls.Web.ComboBoxEnumExt) control); 
             typeToCast = typeof (MMPControls.Web.ComboBoxEnumExt); 
             break; 
            case PropertyTypeAttrib.Date: 
             control = new MMPControls.Web.DateSelect(); 
             control.ID = propertyHelper.Name; 
             //panel.Items.Add(
              //(MMPControls.Web.DateSelect) control); 
             //panel.Items.Add((MMPControls.Web.DateSelect)control); 
             typeToCast = typeof (MMPControls.Web.DateSelect); 
             break; 
            case PropertyTypeAttrib.DateTime: 
             control = new MMPControls.Web.DateSelect(); 
             control.ID = propertyHelper.Name; 
             //panel.Items.Add(
              //(MMPControls.Web.DateSelect)control); 
             typeToCast = typeof (MMPControls.Web.DateSelect); 
             break; 
            case PropertyTypeAttrib.TextInteger: 
             control = (Control)new Ext.Net.NumberField(); 
             ((NumberField)control).AllowDecimals = false; 
             ((NumberField)control).MinValue = 0; 
             ((Ext.Net.NumberField)control).FieldLabel = infoAttribute.HeadLine; 
             control.ID = propertyHelper.Name; 
             //panel.Items.Add(
              //(Ext.Net.NumberField) control); 
             typeToCast = typeof (Ext.Net.NumberField); 
             break; 
            case PropertyTypeAttrib.IList: 
             //TODO: 
             break; 
            case PropertyTypeAttrib.ImageUrl: 
             control = (Control)new Ext.Net.Image(); 
             control.ID = propertyHelper.Name; 
             ((Ext.Net.Image)control).FieldLabel = infoAttribute.HeadLine; 
             //panel.Items.Add((Ext.Net.Image) control); 
             typeToCast = typeof (Ext.Net.Image); 
             break; 
            case PropertyTypeAttrib.TextFractional: 
             control = (Control)new Ext.Net.NumberField(); 
             ((NumberField)control).AllowDecimals = true; 
             ((NumberField)control).DecimalPrecision = infoAttribute.Fractional; 
             ((NumberField)control).MinValue = 0; 
             ((Ext.Net.NumberField)control).FieldLabel = infoAttribute.HeadLine; 
             control.ID = propertyHelper.Name; 
             //panel.Items.Add(
              //(Ext.Net.NumberField) control); 
             typeToCast = typeof (Ext.Net.NumberField); 
             break; 
            case PropertyTypeAttrib.TextLarge: 
             control = (Control)new Ext.Net.TextArea(); 
             ((TextArea)control).FieldLabel = infoAttribute.HeadLine; 
             control.ID = propertyHelper.Name; 
             //panel.Items.Add((TextArea)control); 
             typeToCast = typeof (TextArea); 
             break; 
    } 
    panel.Items.Add((typeToCast)control);//that's what i need to do. 

得到了一致panel.Items.....錯誤無法解決這個符號typeToCast

任何人都做了類似的事情之前?

感謝提前:)

解決:

我所做的就是投我準備控制Component類型。 panel.Items.Add((Component)control);

回答

1

隨着一點點的重構,你應該能夠建立同樣的結果,而不使用太多鑄造。以下示例演示了一種非常簡化的方法。

<%@ Page Language="C#" %> 

<%@ Import Namespace="Panel=Ext.Net.Panel" %> 
<%@ Import Namespace="Button=Ext.Net.Button" %> 

<%@ Register assembly="Ext.Net" namespace="Ext.Net" tagprefix="ext" %> 

<script runat="server"> 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!X.IsAjaxRequest) 
     { 
      this.Form.Controls.Add(this.BuildForm()); 
     } 
    } 

    private Component BuildForm() 
    { 
     var panel = new FormPanel { 
      Title = "Example", 
      Width = 350, 
      Height = 215, 
      Padding = 5, 
      DefaultAnchor = "100%", 
      Buttons = { new Button { Text = "Submit" }} 
     }; 

     panel.Items.Add(this.BuildWidget(new Widget { 
      Name = "text", 
      ID = "TextField1", 
      Label = "My TextField" 
     })); 

     panel.Items.Add(this.BuildWidget(new Widget { 
      Name = "date", 
      ID = "DateField1", 
      Label = "My DateField" 
     })); 

     return panel; 
    } 

    private Field BuildWidget(Widget widget) 
    { 
     Field field = null; 

     switch(widget.Name) 
     { 
      case "text": 
       field = new TextField(); 
       break; 
      case "date": 
       field = new DateField(); 
       break; 
     } 

     field.ID = widget.ID; 
     field.FieldLabel = widget.Label; 

     return field; 
    } 

    public class Widget 
    { 
     public string Name { get; set; } 

     public string ID { get; set; } 
     public string Label { get; set; } 
    } 
</script> 

<!DOCTYPE html> 

<html> 
<head runat="server"> 
    <title>Ext.NET Example</title> 
</head> 
<body> 
    <form runat="server"> 
     <ext:ResourceManager runat="server" /> 

    </form> 
</body> 
</html> 
3

那麼這個錯誤是因爲一個轉換表達式的類型部分必須是一個類型(或類型參數)的名稱 - 而不是表達式的值。

爲什麼你覺得你需要在所有投?爲什麼不只是:

panel.Items.Add(new Ext.Net.NumericField()); 

爲什麼你會把劇組變成ControlNumericField是不是已經從Control派生?你爲什麼使用typeof?基本上,我看不到任何需要在您提供的代碼中進行演員表演的情況。如果你認爲他們有充足的理由,請在問題中增加更多的上下文。

+0

,因爲我讓我的形式動態,我不知道會是什麼類型有 – harry180 2012-04-11 10:07:21

+1

@ harry180:所以,你給出的代碼實際上並不是代表你」會做什麼?我建議你把它改成一個更合適的例子。我可以看到,你可能需要投到'控制',但這應該是所有... – 2012-04-11 10:09:05

+0

我想現在的例子顯示所有我想要做的:) – harry180 2012-04-11 10:17:37