2

我已经对此进行了搜索,但似乎没有一个符合我需要的确切要求。这是场景:

  1. 我有一个包含以下内容的列表:File1、File2、File3 和 File4。此列表绑定到中继器。
  2. 每个转发器项都包含一个 DropDownList。
  3. 我使用转发器的 ItemDataBound 事件来遍历列表,并为存储在我的列表中的所有项目创建和填充下拉列表。
  4. 最终结果是它将为我生成一堆下拉列表,其中包含特定于页面中该列表项的值。

但是,要求发生了变化,变化涉及以下内容:

  1. 如果列表的当前迭代是 File1,它将仅为 File1 创建一个下拉列表。然后对于 File2 和 File3,它不会创建新的下拉菜单,而是将值添加到 File1 的下拉按钮。
  2. 如果列表的当前迭代是 File4,它会再次创建一个新的下拉列表。

那么有没有办法获取 File1 的 ID,这样当 File2 和 File3 的 ItemDataBound 事件触发时,它只会更新 File1 的 DropDownList?或者我必须寻找另一种方法来做到这一点?

4

1 回答 1

1

在 ItemDataBound 事件处理程序中,您可以获得对 DropDownList 的引用并存储在成员变量中。您可以使用该变量来保留对后续 ItemDataBound 事件的 DropDownList 的访问。

听起来你需要做这样的事情:

public partial class _Default : System.Web.UI.Page
{
    // Dummy data class. We'll bind a list of these to the repeater.
    class File
    {
        public string Name { get; set; }
        public int ID { get; set; }
        public List<string> AListOfStrings { get; set; }
    }

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        rptrTest.ItemDataBound +=
            new RepeaterItemEventHandler(rptrTest_ItemDataBound);
    }

    private DropDownList file1DropDown;

    void rptrTest_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        // Find the DropDownList in the repeater's ItemTemplate
        // so we can manipulate it.
        DropDownList ddlSelect =
            e.Item.FindControl("ddlSelect") as DropDownList;
        File dataItem = (File)e.Item.DataItem;

        DropDownList currentDropDownList;
        switch (dataItem.ID)
        {
            case 1:
                // Store the current item's DropDownList for later...
                file1DropDown = ddlSelect;
                currentDropDownList = file1DropDown;
                break;
            case 2:
            case 3:
                currentDropDownList = file1DropDown;
                break;
            default:
                currentDropDownList = ddlSelect;
                break;
        }

        // Get all of the strings starting with the current ID and
        // add them to whichever DropDownList we need to modify.
        currentDropDownList.Items.AddRange((
            from s
            in dataItem.AListOfStrings
            where s.StartsWith(dataItem.ID.ToString())
            select new ListItem(s)).ToArray());
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        // Just build up a list of strings which we can filter later.
        List<string> stringList = new List<string>();
        foreach (string number in (new[] { "1", "2", "3", "4" }))
        {
            foreach (string letter in (new[] { "a", "b", "c", "d", "e" }))
            {
                stringList.Add(number + " " + letter);
            }
        }

        List<File> myObjects = new List<File>(new[]
        {
            new File { ID = 1, Name = "Foo", AListOfStrings = stringList },
            new File { ID = 2, Name = "Bar", AListOfStrings = stringList },
            new File { ID = 3, Name = "Baz", AListOfStrings = stringList },
            new File { ID = 4, Name = "Quux", AListOfStrings = stringList }
        });

        rptrTest.DataSource = myObjects;
        rptrTest.DataBind();
    }
}

...您的 ASPX 页面将包含一个看起来像这样的转发器:

<asp:Repeater runat="server" ID="rptrTest">
    <ItemTemplate>
        ID: <%#DataBinder.Eval(Container.DataItem, "ID")%>
        <br />
        Name: <%#DataBinder.Eval(Container.DataItem, "Name")%>
        <br />
        Select: <asp:DropDownList runat="server" ID="ddlSelect" />
        <br /><br />
    </ItemTemplate>
</asp:Repeater>

So in the rptrTest_ItemDataBound event handler in the code-behind file, there's a switch statement that checks File.ID to see which file instance it's binding to. If the ID is 1, the DropDownList gets assigned to file1DropDown. Then on subsequent events, the logic inside the rest of the switch block decides which DropDownList we need to modify.

于 2012-04-22T04:45:25.083 回答