在Repeater控件的DropDownList的选定值 - 用predefined项目 [英] Selected value of dropdownlist in repeater control - with predefined items

查看:556
本文介绍了在Repeater控件的DropDownList的选定值 - 用predefined项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET页面Repeater控件有predefined里面的物品一个DropDownList。我怎么会去的数据绑定到Repeater控件设置后,它的选择价值?

I have a DropDownList inside a repeater control in an ASP.NET page that has predefined items. How would I go about setting the selected value of it after binding the data to the repeater control?

例如,我有这样的code:

For example, I have this code:

<asp:DropDownList ID="cboType" runat="server">
    <asp:ListItem Text="Communication" Value="Communcation" />
    <asp:ListItem Text="Interpersonal" Value="Interpersonal" />
</asp:DropDownList>

由于DropDownList控件不具有的SelectedValue前端物业,我不能把它的控制水平。相反,你必须使用列表项的所选属性。什么是理想的是:

Because the DropDownList control doesn't have a SelectedValue front-end property, I can't set it on the control level. Instead you have to use the selected property of the list item. What would be ideal is:

<asp:DropDownList ID="cboType" runat="server" SelectedValue='<%# Eval("Type_Of_Behavior") %>'>
    <asp:ListItem Text="Communication" Value="Communcation" />
    <asp:ListItem Text="Interpersonal" Value="Interpersonal" />
</asp:DropDownList>

不幸的是这是行不通的,任何想法?

Unfortunately that won't work, any ideas?

推荐答案

从潘卡加尔格使用code(谢谢!)我已经成功地写上去选择正确的值的方法。为答案作为答案是选择一个静态值,却没有一个是从DB来我不能对它进行标记。这里的功能:

Using the code from Pankaj Garg (thank you!) I've managed to write up the method to select the correct value. I couldn't mark it as the answer as that answer is selecting a static value, but not one that is coming from the DB. Here's the function:

protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        DropDownList cbo = (DropDownList)e.Item.FindControl("cboType");
        Behaviour b = (Behaviour)e.Item.DataItem;

        for (int i = 0; i < cbo.Items.Count; i++)
        {
            if (b.Type_of_Behaviour == cbo.Items[i].Value)
                cbo.Items[i].Selected = true;
            else
                cbo.Items[i].Selected = false;
        }
    }
}

感谢大家谁帮助了。

Thanks to everybody who helped out.

这篇关于在Repeater控件的DropDownList的选定值 - 用predefined项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆