级联ASPxComboBox问题

时间:2010-09-05 15:07:34

标签: asp.net c#-3.0 devexpress

我有两个组合和一个按钮。子组合填充基于父组合键值。单击父组合值将在子组合上更改,单击按钮显示那些组合选择的文本。我可以在我的语法中显示它。我使用北风数据库。

<div>
            <dx:ASPxComboBox ID="ASPxComboBoxParent" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ASPxComboBoxParent_SelectedIndexChanged"
                TextField="ShipName" ValueField="OrderID" ValueType="System.Int32">
            </dx:ASPxComboBox>
            <dx:ASPxComboBox ID="ASPxComboBoxChild" runat="server" TextField="ProductID" ValueField="OrderID"
                ValueType="System.Int32">
            </dx:ASPxComboBox>
            <dx:ASPxButton ID="ASPxButton1" runat="server" OnClick="ASPxButton1_Click" Text="ASPxButton">
            </dx:ASPxButton>
            <dx:ASPxLabel ID="ASPxLabelMessage" runat="server">
            </dx:ASPxLabel>
        </div>

C#语法

protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                NorthwindDataContext db=new NorthwindDataContext();
                var r=from p in db.Orders
                      select p;
                ASPxComboBoxParent.DataSource = r;
                ASPxComboBoxParent.DataBind();
            }

        }

        protected void ASPxComboBoxParent_SelectedIndexChanged(object sender, EventArgs e)
        {
            NorthwindDataContext db=new NorthwindDataContext();
            int a= Convert.ToInt32( ASPxComboBoxParent.SelectedItem.Value);
            var r = from p in db.Order_Details
                    where p.OrderID == a
                    select p;

            ASPxComboBoxChild.DataSource = r;
            ASPxComboBoxChild.DataBind();
            ASPxComboBoxChild.SelectedIndex = 1;
        }

        protected void ASPxButton1_Click(object sender, EventArgs e)
        {


            ASPxLabelMessage.Text = "Parent is" + Convert.ToString(ASPxComboBoxParent.SelectedItem.Text) + "And child is" + Convert.ToString(ASPxComboBoxChild.SelectedItem.Text);
        }

单击父组合后,子组合填充但是单击按钮总是得到子组合索引= 0值事件我更改子组合值。为什么不在按钮事件上更改值? 如果有任何疑问,请提前告诉我。谢谢。

1 个答案:

答案 0 :(得分:0)

出现此问题是因为ASPxComboBoxChild.ValueField属性指向包含相同值的值。即当ASPxComboBoxParent选择索引更改时,ASPxComboBoxChild的Items集合将填充其Value属性相同的项目。由于ASPxComboBox按其值区分项目,因此会出现问题。解决方案很简单:

<dx:ASPxComboBox ID="ASPxComboBoxChild" runat="server" TextField="ProductID" ValueField="ProductID"
                ValueType="System.Int32">
            </dx:ASPxComboBox>