1

我在我的 ASP.NET 4.0 应用程序中使用ext.net 1.3控件。我的 Web 窗体上有几个 ComboBox 控件。该页面应该执行两个任务,插入和更新。保存新记录时没有问题,但是当我尝试用现有数据库值填充 ComboBox 控件时,会弹出各种问题。最麻烦的是这个:

ComboBox 显示数据库中的文本,但它既没有被填充,我也无法选择 ComboBox 值成员。这是因为它没有填充。我已经编写了代码来填充页面加载事件中的组合框。

我正在使用此代码从数据库中选择一个值并将其显示在 ComboBox 上:

string Query = "SELECT CustName FROM CustomerMaster WHERE CustID = " + Session["CustomerID"];

var result = DB.Single<Customer>(Query);
CustomerComboBox.setValue = result.CustName;

此代码成功检索客户名称并显示在 ComboBox 中。它没有做的是它没有从 ComboBox 项目列表中进行选择,也没有填充 ComboBox。

如果我尝试使用以下方法检索文本的值成员:

CustomerComboBox.SelectedItem.Value;

它给出了错误。

为了使它工作,我需要再次单击 ComboBox 以使其填充,然后我从列表中手动选择相同的客户名称来选择值。

如何摆脱这个问题?

-- 已编辑 --

填充 ext.net ComboBox 的代码是这样的:

   public void FillExtComboList(string ParameterFlag, ComboBox  DropDownName)
    {
        string Query = "";
        using (TransactionScope transactionScope = new TransactionScope())
        {
            using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["cncustomer"].ConnectionString.ToString()))
            {
                con.Open();
                SqlDataReader dr;
                try
                {
                    if (ParameterFlag == "Customer")
                    {
                        Query = "SELECT CustName FROM CustomerMaster";
                    }
                    //Check whether the Drop Down has existing items. If YES, empty it.
                    if (DropDownName.Items.Count > 0)
                        DropDownName.Items.Clear();

                    SqlCommand cmd = new SqlCommand(Query, con);
                    dr = cmd.ExecuteReader();

                    while (dr.Read())
                    {
                        Ext.Net.ListItem extLI = new Ext.Net.ListItem();
                        extLI.Text = dr[0].ToString();
                        DropDownName.Items.Add(extLI);
                    }

                    dr.Close();
                    con.Close();
                }
                catch (Exception ex)
                {
                    con.Close();
                   // RunCustomScript("alert('" + ex.Message.ToString() + "')", callingPageObjectName);
                }
            }   /* End of SQL Connection */
            transactionScope.Complete();
        }   /* End of Transaction Scope */
    }

在 Page Load 事件中,ComboBox 控件填充了上述方法。

4

2 回答 2

0

我没有看到填充组合框的指令,只是设置其选定值。您是否缺少 CustomerComboBox.DataSource = someList 或类似的东西?

<-- 编辑-->

抱歉,我认为 setValue 是您页面加载时的代码...好吧,这不是您问题的答案,而是一个重要的性能修复。您应该在加载组合时执行此操作:执行 SQL 查询时:

Query = "SELECT CustID, CustName FROM CustomerMaster"

填充组合时:

Ext.Net.ListItem extLI = new Ext.Net.ListItem();
extLI.Value = dr["CustId"].ToString();                         
extLI.Text = dr["CustName"].ToString();
DropDownName.Items.Add(extLI);

所以当你想选择一个项目时,你只需这样做:

CustomerComboBox.setValue = Session["CustomerID"];

并避免返回数据库获取客户名称。

现在,您可以分享处理组合框点击的代码吗?由于它确实填充了组合,它可能会给我们带来一些光明。另外,尝试添加

CustomerComboBox.DataBind()

而且,想想看,我在 Page_Load 上看到您使用“DropDownName”,然后您使用“CustomerComboBox”。这可能是问题吗?

于 2012-03-14T12:32:45.447 回答
0

如果我理解正确,请尝试以下代码:

protected void Page_Load(object sender, EventArgs e) {
        FillExtComboList(DropDownName);

        // Set value that you want
        DropDownName.SetValueAndFireSelect("Test 3");
    }

    public void FillExtComboList(ComboBox DropDownName) {
        try {

            //Check whether the Drop Down has existing items. If YES, empty it.
            if (DropDownName.Items.Count > 0)
                DropDownName.Items.Clear();


            for (int i = 0; i < 10; i++) {
                Ext.Net.ListItem extLI = new Ext.Net.ListItem();
                extLI.Text = "Test " + i;
                DropDownName.Items.Add(extLI);
            }   
        } catch (Exception ex) {
            // RunCustomScript("alert('" + ex.Message.ToString() + "')", callingPageObjectName);
        } /* End of Transaction Scope */
    }
于 2012-03-15T12:09:36.690 回答