gpt4 book ai didi

c# - 如何在运行时在 asp.net 中绑定(bind) radcombobox

转载 作者:太空狗 更新时间:2023-10-30 00:16:35 25 4
gpt4 key购买 nike

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

最佳答案

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

您可以将 RadComboBox 绑定(bind)到 ASP.NET 2.0 datasources , ADO.NET DataSet/DataTable/DataView , 至 Arrays and ArrayLists ,或对象的 IEnumerable。当然,您也可以自己一项一项地添加。对于 RadComboBox,您将使用 RadComboBoxItems 而不是 ListItems。

无论以何种方式,您都必须告诉组合框每个项目的文本和值是什么。

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

编辑: RadComboBox in a Grid :

在 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 事件中手动绑定(bind)组合框:

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

}
}
}

关于c# - 如何在运行时在 asp.net 中绑定(bind) radcombobox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6261075/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com