0

我正在开发一个 ASP.NET 网站,并且正在使用带有 RowExpander 部分的 GridPanel。:

<ext:RowExpander ID="RowExpander1" runat="server">
            <Loader ID="Loader1" runat="server" DirectMethod="#{DirectMethods}.GetGrid" Mode="Component">
                <LoadMask ShowMask="true" />
                <Params>
                    <ext:Parameter Name="id" Value="this.record.getId()" Mode="Raw" />
                </Params>
            </Loader>
        </ext:RowExpander>

在代码隐藏中,名为“GetData”的函数必须动态创建嵌套的 GridPanel,如下所示:

<Ext.Net.DirectMethod()>
Public Function GetGrid(ByVal parameters As Dictionary(Of String, String)) As Object

    Dim data As New List(Of Object)

    For i = 1 To 10
        data.Add(New With {.ID = "P" & i, .Name = "Product " & i})
    Next

    Dim config As New Ext.Net.GridPanel.Config

    config.Height = 50
    config.EnableColumnHide = False
    config.StoreID = "Store2"

    Dim store As New Ext.Net.Store
    Dim model As New Ext.Net.Model

    store.ID = "Store2"
    store.DataSource = data
    store.ModelName = "Model2"

    model.ID = "Model2"
    model.IDProperty = "ID"
    model.Fields.Add("ID")
    model.Fields.Add("Name")

    store.Model.Add(model)
    config.Store.Add(store)
    config.StoreID = "Store2"

    Dim column As New Ext.Net.Column
    column.ID = "ColumnModel2"
    column.Text = "Products's Name"
    column.DataIndex = "Name"
    config.ColumnModel.Columns.Add(column)
    config.ColumnModel.Add(column)

    Dim grid As New Ext.Net.GridPanel(config)

    Return Ext.Net.ComponentLoader.ToConfig(grid)

End Function

当我单击 GridPanel 中的“+”时,它显示一个空网格,即使没有列。其实Ext.Net.ComponentLoader.ToConfig(grid)生成的代码是:

[{"height":50,"xtype":"grid","columns":{},"enableColumnHide":false,"store":"Store2"}]

所以我在 GetGrid 函数中做错了什么。我错过了什么?

我遇到的每个示例都是用 C# 编写的。

4

1 回答 1

0

我知道您正在将此 C# 示例转换为 VB.NET。

好吧,轻声说,我对 VB.NET 并不流利。因此,在这种情况下,我使用 C# 到 VB.NET 转换器。例如,Telerik 之一

似乎,VB.NET 没有只读属性的对象初始化器(虽然,我不是 100% 确定),所以,在转换之前,我必须将 C# 代码更改为:

[DirectMethod]
public static string GetGrid(Dictionary<string, string> parameters)
{
    GridPanel grid = new GridPanel
    {
        Height = 200,
        EnableColumnHide = false
    };

    List<object> data = new List<object>();

    for (int i = 1; i <= 10; i++)
    {
        data.Add(new { ID = "P" + i, Name = "Product " + i });
    }

    Store store = new Store();

    Model model = new Model()
    {
        IDProperty = "ID"
    };

    model.Fields.Add(new ModelField("ID"));
    model.Fields.Add(new ModelField("Name"));


    store.Model.Add(model);

    store.DataSource = data;
    grid.Store.Add(store);

    grid.ColumnModel.Columns.Add(new Column { Text = "Products's Name", DataIndex = "Name" });

    return ComponentLoader.ToConfig(grid);
}

将其放入转换器时,稍微修改输出以使其可编译,最后得到一个 VB.NET 示例。

<%@ Page Language="VB" %>

<%@ Import Namespace="System.Collections.Generic" %>

<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>

<script runat="server">
    Protected Sub Page_Load(sender As Object, e As EventArgs)
        If Not ExtNet.IsAjaxRequest Then
            Dim data As New List(Of Object)()

            For i As Integer = 1 To 10
                data.Add(New With { _
                    .ID = "S" & i.ToString(), _
                    .Name = "Supplier " & i.ToString() _
                })
            Next

            Me.Store1.DataSource = data
        End If

    End Sub

    <DirectMethod> _
    Public Shared Function GetGrid(parameters As Dictionary(Of String, String)) As String
        Dim grid As New GridPanel() With { _
            .Height = 200, _
            .EnableColumnHide = False _
        }

        Dim data As New List(Of Object)()

        For i As Integer = 1 To 10
            data.Add(New With { _
                .ID = "P" & i.ToString(), _
                .Name = "Product " & i.ToString() _
            })
        Next

        Dim store As New Store()

        Dim model As New Model() With { _
            .IDProperty = "ID" _
        }

        model.Fields.Add(New ModelField("ID"))
        model.Fields.Add(New ModelField("Name"))


        store.Model.Add(model)

        store.DataSource = data
        grid.Store.Add(store)

        grid.ColumnModel.Columns.Add(New Column() With { _
            .Text = "Products's Name", _
            .DataIndex = "Name" _
        })

        Return ComponentLoader.ToConfig(grid)
    End Function
</script>

<!DOCTYPE html>

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

        <ext:GridPanel
            runat="server"
            Title="Expander Rows with GridPanel"
            Collapsible="true"
            AnimCollapse="true"
            Icon="Table"
            Width="600"
            Height="450"
            DisableSelection="true">
            <Store>
                <ext:Store ID="Store1" runat="server">
                    <Model>
                        <ext:Model runat="server" IDProperty="ID">
                            <Fields>
                                <ext:ModelField Name="ID" />
                                <ext:ModelField Name="Name" />
                            </Fields>
                        </ext:Model>
                    </Model>
                </ext:Store>
            </Store>
            <ColumnModel runat="server">
                <Columns>
                    <ext:Column runat="server" Text="Supplier" DataIndex="Name" Flex="1" />
                </Columns>
            </ColumnModel>
            <Plugins>
                <ext:RowExpander runat="server">
                    <Loader runat="server" DirectMethod="#{DirectMethods}.GetGrid" Mode="Component">
                        <LoadMask ShowMask="true" />
                        <Params>
                            <ext:Parameter Name="id" Value="this.record.getId()" Mode="Raw" />
                        </Params>
                    </Loader>
                </ext:RowExpander>
            </Plugins>
        </ext:GridPanel>
    </form>
</body>
</html>

PS答案的来源在这里

于 2013-06-26T06:02:45.480 回答