开发者问题收集

c# ext.net:从后面的代码中将项目插入组合框中

2017-09-26
1468

我想创建一个包含 2 个项目的组合框。项目的文本应以不同的格式显示 DateTime.Now 。组合框位于行扩展器中。以下是组合框的代码:

<ext:ComboBox  runat="server" ID="cmbFormatFFC"   DataIndex="DateFormat"          FieldLabel="Date Format"             Width="400" ForceSelection="true" EmptyText="Select date format...">
      <Listeners>
            <BeforeRender Handler="#{DirectMethods}.fillComboDates()" />

以及代码隐藏:

[DirectMethod]
        public void fillComboDates()
        {
            cmbFormatFFC.Items.Insert(0,new Ext.Net.ListItem(DateTime.Now.ToString("d/M/yyyy"), "d/M/yyyy"));
            cmbFormatFFC.Items.Insert(1,new Ext.Net.ListItem(DateTime.Now.ToString("dd-MMM-yyyy"), "dd-MMM-yyyy"));
        }

但是当我在应用程序中展开行时,出现此错误:

Uncaught TypeError: Cannot set property 'component' of null

3个回答

您无法使用 BeforeRender 加载存储数据。这种情况发生在控件生命周期的早期。最好的解决方案是在 RowExpander 上使用 BeforeExpand 事件。此外,由于 ComboBox 中有静态数据,因此您只想执行一次 DirectEvent。以下是示例:

*.aspx

<ext:GridPanel ID="grid" runat="server">
    <ColumnModel>
        <Columns>
            <ext:Column runat="server" DataIndex="Field1" />
        </Columns>
    </ColumnModel>
    <Store>
        <ext:Store runat="server" ID="store">
            <Model>
                <ext:Model runat="server">
                    <Fields>
                        <ext:ModelField Name="Field1" />
                    </Fields>
                </ext:Model>
            </Model>
        </ext:Store>
    </Store>
    <Plugins>
        <ext:RowExpander runat="server">
            <Component>
                <ext:ComboBox runat="server" ValueField="Key" DisplayField="Display">
                    <Store>
                        <ext:Store runat="server" ID="comboStore" OnReadData="Store_ReadData">
                            <Model>
                                <ext:Model runat="server">
                                    <Fields>
                                        <ext:ModelField Name="Key" />
                                        <ext:ModelField Name="Display" />
                                    </Fields>
                                </ext:Model>
                            </Model>
                        </ext:Store>
                    </Store>
                </ext:ComboBox>
            </Component>
            <Listeners>
                <BeforeExpand Handler="if (comboStore.getCount() == 0) comboStore.reload();" />
            </Listeners>
        </ext:RowExpander>
    </Plugins>
</ext:GridPanel>

*.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    if (Page.IsPostBack || X.IsAjaxRequest)
        return;

    store.DataSource = new object[] 
    { 
        new { Field1 = "Row 1" },
        new { Field1 = "Row 2" },
        new { Field1 = "Row 3" }
    };

    store.DataBind();
}

protected void Store_ReadData(object sender, StoreReadDataEventArgs e)
{
    comboStore.DataSource = new object[] 
    { 
        new { Key = DateTime.Now.ToString("d/M/yyyy"), Display = "d/M/yyyy" },
        new { Key = DateTime.Now.ToString("dd-MMM-yyyy"), Display = "dd-MMM-yyyy" }
    };

    comboStore.DataBind();
}
Peska
2017-12-21

确保您的组合已成功初始化并且您的方法 fillComboDates() 正在返回值

Sanjeet Kumar
2017-09-26

可能是 Ext.Net 生成并发送给客户端的 JS 在 cmbFormatFFC 渲染之前执行。为了确保它稍后发生,您可以将监听器从 BeforeRender 更改为 BoxReady

chrisuae
2017-09-29