如何在asp.net运行时绑定radcombobox

发布于 2024-11-14 08:21:35 字数 133 浏览 2 评论 0原文

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

I have a webpage that has a Telerik RadComboBox in radgrid control on the page,and i have a SqlserverCe database.My issue is how can i write the code to bind the RadCombobox at page load event in asp.net please help me.....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜雨飘雪 2024-11-21 08:21:35

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

您可以将 RadComboBox 绑定到 ASP.NET 2.0 数据源ADO.NET DataSet/DataTable/DataView,到 数组和 ArrayList,或对象的 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);

                }
            }
        }

Items are bound to RadComboBox basically in the same way as to an ASP.NET DropDownList.

You can bind the RadComboBox to ASP.NET 2.0 datasources, ADO.NET DataSet/DataTable/DataView, to Arrays and ArrayLists, or to an IEnumerable of objects. And of course you can add the items one by one yourself. With RadComboBox, you'll use RadComboBoxItems instead of ListItems.

In one way or other, you'll have to tell the combobox what is each item's text and value.

Working with Items in Server Side Code:

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);  
    }
}

Binding to DataTable, DataSet, or 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();
    }
}

EDIT: RadComboBox in a Grid:

Inside a RadGrid, it is perhaps easiest to use load on demand, by setting EnableLoadOnDemand="True" and handling the OnItemsRequested event.

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();
            }
        } 

You can also bind the combobox manually in the grid's OnItemDataBoundHandler event:

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);

                }
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文