返回ASP DDL或Telerik控件

时间:2009-06-21 20:34:22

标签: asp.net telerik

我正在尝试编写一个更通用的方法,它将填充ASP.NET下拉列表或带有状态的telerik RadComboBox。我想将控件作为参数传递给方法。我有一个DataTable,它包含我循环的所有状态(见下文) - 我想使这适用于Telerik RadComboBox - 所以我需要更改第一个参数,以及我插入一个新的部分ListItem - 对于Telerik RadComboBox,它是新的RadComboBoxItem。我怎么能这样做?

public void PopulateStates(DropDownList ddlStates, string country)
{
    ddlStates.Items.Clear();
    DataLookup dl = new DataLookup();
    DataTable dt = dl.GetStatesByCountry(country);
    if (dt != null)
    {
        if (dt.Rows.Count > 0)
        {
            ddlStates.Items.Insert(0, new ListItem(""));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                ddlStates.Items.Add(new ListItem(dt.Rows[i]["STCD_Descr"].ToString(),
                    dt.Rows[i]["STCD_State_CD"].ToString()));
            }
        }
    }
}  

2 个答案:

答案 0 :(得分:0)

我查了一下telerik文档&amp;似乎没有共同的做法 - 你想做什么。

如果可能,请尝试使用数据绑定(设置DataSource&amp;调用DataBind)。
注意:我没试过。但我认为应该得到两者的支持。

答案 1 :(得分:0)

由于ListBox和RadComboBox没有除“Control”类之外的公共类,因此您需要检查实际类型。

以下代码怎么样?

public void PopulateStates(Control ddl, string country)
{
    object listItem = new object();
    switch (ddl.GetType().Name)
    {
        case "RadComboBox":
            listItem = listItem as RadComboBoxItem;
            ddl = ddl as RadComboBox;
            break;
        case "ListBox":
            listItem = listItem as ListItem;
            ddl = ddl as ListBox;
            break;
        default:
            return;
    }

    // proceed with your code
}