在中继器内填充 DropDownList 不起作用 [英] Populating DropDownList inside Repeater not working

查看:11
本文介绍了在中继器内填充 DropDownList 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在中继器中填充下拉列表,但我并没有取得很大成功.我可能使用了错误的 EventArgs e.

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 代码:

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>

以及背后的代码:

    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? :)

推荐答案

除此问题的其他答案外,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.

在您的下拉列表中定义数据绑定事件:

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天全站免登陆