4

我有一个网页,在页面上的 radgrid 控件中有一个 Telerik RadComboBox,我有一个 SqlserverCe 数据库。我的问题是如何编写代码以在 asp.net 中的页面加载事件中绑定 RadCombobox 请帮助我... ..

4

1 回答 1

11

项基本上以与 ASP.NET DropDownList 相同的方式绑定到 RadComboBox。

您可以将 RadComboBox 绑定到ASP.NET 2.0 数据源ADO.NET DataSet/DataTable/DataView数组和 ArrayLists或对象的 IEnumerable。当然,您可以自己一个一个地添加项目。使用 RadComboBox,您将使用 RadComboBoxItems 而不是 ListItems。

以一种或另一种方式,您必须告诉组合框每个项目的文本和值是什么。

使用服务器端代码中的项目

protected void Page_Load(object sender, EventArgs e)
{  
    if (!Page.IsPostBack)  
    {    
        RadComboBoxItem item1 = new RadComboBoxItem();    
        item1.Text = "Item1";   
        item1.Value = "1";    
        RadComboBox1.Items.Add(item1);    
        RadComboBoxItem item2 = new RadComboBoxItem();   
        item2.Text = "Item2";    
        item2.Value = "2";   
        RadComboBox1.Items.Add(item2);    
        RadComboBoxItem item3 = new RadComboBoxItem();    
        item3.Text = "Item3";   
        item3.Value = "3";   
        RadComboBox1.Items.Add(item3);  
    }
}

绑定到 DataTable、DataSet 或 DataView

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        SqlConnection con = new SqlConnection("Data Source=LOCAL;Initial Catalog=Combo;Integrated Security=True");

        SqlDataAdapter adapter = new SqlDataAdapter("SELECT [Text], [Value] FROM [Links]", con);
        DataTable links = new DataTable();

        adapter.Fill(links);

        combo.DataTextField = "Text";
        combo.DataValueField = "Value";
        combo.DataSource = links;
        combo.DataBind();
    }
}

编辑: 网格中的 RadComboBox

在 RadGrid 中,通过设置EnableLoadOnDemand="True"并处理OnItemsRequested事件,使用按需加载可能是最简单的。

protected void RadComboBox1_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
        {
            string sql = "SELECT [SupplierID], [CompanyName], [ContactName], [City] FROM [Suppliers] WHERE CompanyName LIKE @CompanyName + '%'";
            SqlDataAdapter adapter = new SqlDataAdapter(sql,
                ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
            adapter.SelectCommand.Parameters.AddWithValue("@CompanyName", e.Text);

            DataTable dt = new DataTable();
            adapter.Fill(dt);

            RadComboBox comboBox = (RadComboBox)sender;
            // Clear the default Item that has been re-created from ViewState at this point.
            comboBox.Items.Clear();

            foreach (DataRow row in dt.Rows)
            {
                RadComboBoxItem item = new RadComboBoxItem();
                item.Text = row["CompanyName"].ToString();
                item.Value = row["SupplierID"].ToString();
                item.Attributes.Add("ContactName", row["ContactName"].ToString());

                comboBox.Items.Add(item);

                item.DataBind();
            }
        } 

您还可以在网格的OnItemDataBoundHandler事件中手动绑定组合框:

protected void OnItemDataBoundHandler(object sender, GridItemEventArgs e)
        {
            if (e.Item.IsInEditMode)
            {
                GridEditableItem item = (GridEditableItem)e.Item;

                if (!(e.Item is IGridInsertItem))
                {
                    RadComboBox combo = (RadComboBox)item.FindControl("RadComboBox1");

                    // create and add items here
                    RadComboBoxItem item = new RadComboBoxItem("text","value");
                    combo.Items.Add(item);

                }
            }
        }
于 2011-06-07T07:30:33.777 回答