c# - 更新在 Repeater 中生成的 DropDownList 的值

标签 c# asp.net drop-down-menu repeater itemdatabound

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

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

虽然要求发生了变化,变化涉及以下内容:

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

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

最佳答案

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

所以在代码隐藏文件的 rptrTest_ItemDataBound 事件处理程序中,有一个 switch 语句检查 File.ID 以查看它绑定(bind)到哪个文件实例。如果 ID 为 1,则将 DropDownList 分配给 file1DropDown。然后在后续事件中,开关 block 其余部分内的逻辑决定我们需要修改哪个 DropDownList。

关于c# - 更新在 Repeater 中生成的 DropDownList 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10263456/

相关文章:

c# - Azure 发布错误 - 无法复制文件 obj\Debug\build.force

c# - Entity Framework 中的嵌套查询

c# - 在登录表单页面上加载 javascript/css?

c# - 如何在aspx中循环传递DataTable值?

c# - 为什么 DropDownList.SelectedIndexChanged 事件没有触发?

c# - 我如何在 Unity 中使用具有不同道路或平台颜色的重复场景以及在前一个场景中使用的相同预制件?

c# - 帮助编写文件夹结构的算法

c# - 如何获取 GridView 的 DataSource 中的记录数

html - 带有子菜单但父链接未保持事件状态的垂直菜单

user-interface - wxPython:如何在状态栏中放置额外的小部件?