Question

I've run a search for this but none seem to meet the exact requirement that I'd need. Here's the scenario:

  1. I have a list containing the following: File1, File2, File3, and File4. This list gets bound to a repeater.
  2. Each repeater item contains a DropDownList.
  3. I use the repeater's ItemDataBound event to iterate through the list and create and populate the dropdowns for all items stored in my list.
  4. The end result is that it will generate a bunch of dropdowns for me with values specific for that list item in the page.

There's a change in the requirements though, and the change involves the following:

  1. If the current iteration of the list is File1, it will create a dropdown list ONLY for File1. Then for File2 and File3, instead of creating new dropdowns, it will just add the values to File1's dropdown button.
  2. If the current iteration of the list is File4, it creates a new dropdown again.

So is there a way to grab the ID of File1 so that when the ItemDataBound event fires for File2 and File3, it will just update File1's DropDownList? Or I'd have to look at another way to do this?

Was it helpful?

Solution

Inside the ItemDataBound event handler, you can get a reference to a DropDownList and store in a member variable. You can use that variable to retain access to the DropDownList on subsequent ItemDataBound events.

It sounds like you need to do something like this:

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();
    }
}

... where your ASPX page would contain a repeater that looks something like this:

<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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top