填充的DropDownList里面直放站不工作 [英] Populating DropDownList inside Repeater not working

查看:145
本文介绍了填充的DropDownList里面直放站不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图填充一个转发器内的下拉列表中,但我不是很成功。 我可能使用了错误的 EventArgs的

I'm trying to populate a dropdown list inside a repeater, but I'm not being very successful. I'm probably using the wrong EventArgs e.

下面是我的aspx code:

Here's my aspx code:

        <asp:Repeater runat="server" id="criteriaScore">
            <HeaderTemplate>
              <ul>         
                <li class="header"><span class="item">Kriterie</span><span class="value">Poeng</span><span class="description">Beskrivelse</span></li>
            </HeaderTemplate>
            <ItemTemplate>
                <li>
                    <span class="item"> <%# Eval("criteria") %>:</span>
                    <asp:DropDownList id="ddlRating" runat="server" autopostback="true" enableviewstate="false"></asp:DropDownList>
                    <span class="value score<%# Eval("lvl") %>" title="<%# Eval("description") %>"> </span>
                </li>
            </ItemTemplate>
            <FooterTemplate>
                </ul>  
            </FooterTemplate>
        </asp:Repeater>

和后面的code:

    protected void criteriaScore_ItemDataBound(object sender, DataListCommandEventArgs e)
    {
        DropDownList ddl = (DropDownList)e.Item.FindControl("ddlRating");

        for(int i=1; i > 5; i++)
        {
            ddl.Items.Add(new ListItem(i.ToString(), i.ToString()));
        }
    }

有人能指导我沿着正确的道路? :)

Can someone please guide me down the right path? :)

推荐答案

Depsite在这个问题上其他的答案,ItemDataBound事件不应该被用来绑定控制数据,这样做,在控制水平。

Depsite the other answers on this question, the ItemDataBound event should not be used to bind control data, do that at the control level.

在你的DropDownList定义数据绑定事件:

In your dropdownlist define the databinding event:

<asp:DropDownList runat="server" ID="ddlYourDDL"  OnDataBinding="ddlYourDDL_DataBinding">

然后实现OnDataBinding事​​件:

Then implement the OnDataBinding event:

protected void ddlYourDDL_DataBinding(object sender, System.EventArgs e)
{
    DropDownList ddl = (DropDownList)(sender);
    for (int i = 1; i < 5; i++)
    {
         ddl.Items.Add(new ListItem(i.ToString(), i.ToString()));
    }

    // Now that the items are all there, set the selected property
    ddl.SelectedValue = Eval("selectedfieldname").ToString();
 }

您应该尝试做的所有您绑定在控制层面,而不是寻找的东西,让你的网格必须了解它所包含的内容。每个控件可以自己照顾自己。)这样,它是非常容易的添加和删除控件模板,并保持这些变化隔离

You should try and do all your databinding at the control level instead of searching for things and having your grid have to know about what it contains. Each control can take care of itself ;) This way it is much easier to add and remove controls to your template and keep these changes isolated.

这篇关于填充的DropDownList里面直放站不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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