2012-03-14 59 views
1

我在我的ASP.NET 4.0应用程序中使用ext.net 1.3控件。我的Web窗体上有几个ComboBox控件。该页面应该执行两个任务,插入和更新。保存新记录时没有问题,但是当我尝试用现有数据库值填充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中客户名称和显示。它没有做的是它不是从组合框项目列表中选择,也没有填充组合框。

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

CustomerComboBox.SelectedItem.Value; 

提示错误。

为了使它工作,我需要再次点击组合框使其填充,而不是从列表中手动选择相同的客户名称来选择值。

如何摆脱这个问题?

- 编辑 -

的代码来填充ext.net组合框是这样的:

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 */ 
    } 

在页加载事件,组合框控件填充有上述方法。

回答

0

我看不到填充组合框的指令,只能设置其选定值。 Arent你错过了一个CustomerComboBox.DataSource = someList或类似的东西?

< - 编辑 - >

对不起,我以为是的setValue在您的网页加载代码... OK,这是不是会回答你的问题,但一个重要的性能修复。 装载组合时,你应该这样做:在执行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”。这可能是问题吗?

+0

正如我所说的组合框填充在pageLoad的。用代码编辑帖子。 – RKh 2012-03-14 12:39:05

+0

请忽略DropDownName和CustomerComboBox的最后一行混淆。前者是参数,而后者是ComboBox的名称。 – RKh 2012-03-15 04:53:47

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 */ 
    } 
+0

setValue无法正常工作。虽然它设定了价值,但列表消失了。 – RKh 2012-03-17 10:06:45

+0

尝试使用SetValueAndFireSelect。列出ComboBox的值?你能否展示一些清晰的例子,没有数据库交互和单个ASPX页面。我会尽力重现这种奇怪的行为。 – Baidaly 2012-03-18 09:07:00

相关问题