Ext.Net- 数据源的绑定ComboBox

  Ext.Net对数据的绑定有一些不同的地方. 他在数据方面的绑定还没有ASP.NET做的好. 但他制造出来的控件确实挺漂亮的. 对数据的绑定看一个ComBoBox应该其他的就懂了.

  其实他都是通过一个Store字段对数据进行绑定的. 可是Store字段有两种写法, 如下面, 第一种是不支持从数据库获取数据进行绑定的, 只能自己定义一个OBJECT数组进行绑定, 而第二种是支持从数据库从获取数据进行绑定的. 需要知道这一点区别, 不然你在没发现前会和我一样, 很捉狂, 不知道出了什么情况

        <ext:Store ID="Store1" runat="server">
            <Reader>
                <ext:ArrayReader>
                    <Fields>
                        <ext:RecordField Name="name" />
                        <ext:RecordField Name="id" />
                    </Fields>
                </ext:ArrayReader>
            </Reader>
        </ext:Store>
        <ext:Store ID="Store1" runat="server" OnRefreshData="Store1_RefreshData">
            <Reader>
                <ext:JsonReader IDProperty="Id">
                    <Fields>
                        <ext:RecordField Name="Name" />
                    </Fields>
                </ext:JsonReader>
            </Reader>
        </ext:Store>
第一种情况 第二种情况

 

  下面通过两段代码, 进行数据源绑定,一种是通过OBJECT数组绑定的, 另一种是通过数据库进行绑定的. 有一点需要明确的, 通过数据库进行绑定的, 如果没有传到类里, 必需和数据库字段一样. 如果传到类里, 需要和类的属性字段一样.

  第一段程序:

        <ext:ResourceManager ID="ResourceManager1" runat="server">
        </ext:ResourceManager>
        <ext:Store ID="Store1" runat="server">
            <Reader>
                <ext:ArrayReader>
                    <Fields>
                        <ext:RecordField Name="name" />
                        <ext:RecordField Name="id" />
                    </Fields>
                </ext:ArrayReader>
            </Reader>
        </ext:Store>
        <ext:ComboBox ID="ComboBox1" runat="server" DisplayField="name" ValueField="id" StoreID="Store1">
        </ext:ComboBox>


       protected void Page_Load(object sender, EventArgs e)
        {
            this.Store1.DataSource = new object[]
            {
                new object[] { "AL", "Alabama"},
                new object[] { "AK", "Alaska"},
                new object[] { "AZ", "Arizona" },
                new object[] { "AR", "Arkansas"},
                new object[] { "CA", "California" },
                new object[] { "CO", "Colorado"},
                new object[] { "CT", "Connecticut" },
                new object[] { "DE", "Delaware" }
            };
            this.Store1.DataBind();
        }

  第二段程序(另外要注意 DisplayField="Name" ValueField="Id" 和Store字段显示和取值要相同)

    public class Person
    {
        private int id;
        private string name;

        public int Id
        {
            get { return id; }
            set { id = value; }
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

    }

        <ext:ResourceManager ID="ResourceManager1" runat="server">
        </ext:ResourceManager>
        <ext:Store ID="Store1" runat="server" OnRefreshData="Store1_RefreshData">
            <Reader>
                <ext:JsonReader IDProperty="Id">
                    <Fields>
                        <ext:RecordField Name="Name" />
                    </Fields>
                </ext:JsonReader>
            </Reader>
        </ext:Store>
        <ext:ComboBox ID="ComboBox1" runat="server" DisplayField="Name" ValueField="Id" StoreID="Store1">
        </ext:ComboBox>



        protected void Page_Load(object sender, EventArgs e)
        {

            if (!X.IsAjaxRequest)
            {
                this.Store1.DataSource = GetDataReader();
                this.Store1.DataBind();
            }

        }

        private List<Person> GetDataReader()
        {
            List<Person> lists = new List<Person>();
            using (SqlConnection conn = new SqlConnection("server=.;database=YHM;uid=sa;pwd=123456"))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("select * from comb", conn))
                {
                    SqlDataReader dr = cmd.ExecuteReader();
                    while (dr.Read())
                    {
                        Person person = new Person();
                        person.Id = Convert.ToInt32(dr["id"]);
                        person.Name = Convert.ToString(dr["name"]);
                        lists.Add(person);
                    }
                }
            }
            return lists;
        }

        protected void Store1_RefreshData(object sender, StoreRefreshDataEventArgs e)
        {
            this.Store1.DataSource = GetDataReader();
            this.Store1.DataBind();
        }

 

 

posted @ 2012-12-26 10:35  春天又来了  阅读(473)  评论(0编辑  收藏  举报