如何动态填充comboBox?

时间:2013-04-22 14:53:39

标签: ext.net

在运行时尝试与store变量绑定时,我无法渲染Combobox。

// Aspx页面。

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

<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data" %>
<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
<!DOCTYPE html>
<html>
<head id="Head1" runat="server">
    <title>Ext.NET Examples</title>

    <script runat="server">
        Store storeTable;
        private void createStoreTable()
        {
            storeTable = new Store();
            storeTable.ID = "storeTable";
            Ext.Net.Model model = new Model();
            ModelField modelField = new ModelField();
            modelField.Name = "name";
            model.Fields.Add(modelField);
            storeTable.Model.Add(model);

        }
        private DataTable GetDataTable()
        {
            DataTable table = new DataTable();

            table.Columns.AddRange(new DataColumn[] {
            new DataColumn("name")   { ColumnName = "name",    DataType = typeof(string) },
            new DataColumn("ID")     { ColumnName = "ID",      DataType = typeof(double) }
        });

            foreach (object[] obj in this.Data)
            {
                table.Rows.Add(obj);
            }

            return table;
        }
        private object[] Data
        {
            get
            {
                DateTime now = DateTime.Now;

                return new object[]
            {
                new object[] { "peter", 1 },
                new object[] { "John", 2},
                new object[] { "Stella", 3}
            };
            }
        }
        private void PopulateEmp()
        {
            try
            {
                createStoreTable();
                this.storeTable.DataSource = GetDataTable();
                cboTable.Store.Add(storeTable);
                cboTable.DisplayField = "name";
                cboTable.ValueField = "ID";
                cboTable.Render();
            }
            catch (Exception ex)
            {
                X.Msg.Show(new MessageBoxConfig
                {
                    Message = ex.Message,
                    Title = "Error Message",
                    Width = 300,
                    Buttons = MessageBox.Button.OKCANCEL
                });
                X.Msg.Show();
                //Response.Write(ex.Message);
            }
        }
        protected void cboTable_Change(object sender, EventArgs e)
        {
            PopulateEmp();
        }

        protected void Page_Load(object sender, EventArgs e)
        {


        }
</script>

</head>
<body>
    <form id="form1" runat="server">
    <ext:ResourceManager ID="ResourceManager1" runat="server" />
    <ext:FormPanel ID="frmPanel1" runat="server">
        <Items>
            <ext:ComboBox runat="server" ID="cboDB" FieldLabel="select comp" Text="delete">
                <Items>
                    <ext:ListItem Text="MS" Value="MS"/>
                    <ext:ListItem Text="Oracle" Value="Oracle" />
                    <ext:ListItem Text="Java" Value="Java" />
                </Items>
                <DirectEvents>
                    <Select OnEvent="cboTable_Change" />
                </DirectEvents>
            </ext:ComboBox>
            <ext:ComboBox ID="cboTable" runat="server" FieldLabel="Select Emp">
            </ext:ComboBox>
            <ext:TextField runat="server" ID="lblTest" FieldLabel="Name" />
            <ext:Button ID="btnSearch" runat="server" Text="Search" />
        </Items>
    </ext:FormPanel>
    </form>
</body>
</html>

在上面的例子中,我有一个TextBox,第二个组合框后面有一个按钮。 当我试图在选择第一个ComboBox时渲染另一个组合框时,它工作正常,但第二个下拉列表在最后渲染。

1 个答案:

答案 0 :(得分:0)

请替换

cboTable.Render();

cboTable.InsertTo(1, this.frmPanel1);

我也会把

cboTable.SuspendScripting();

到try块的开头。否则,这个

cboTable.DisplayField = "name";
cboTable.ValueField = "ID";

生成JavaScript以反映客户端属性。但是,由于您要重新渲染ComboBox,因此不需要它。