1

给定一个 asp 中继器和一个下拉列表。我正在中继器上进行数据绑定并有一个 ItemDataBound 事件。触发事件的我将下拉列表绑定到某个数据源,并为 SelectedIndexChanged 处理程序分配 autopostback true。

每个下拉菜单都会调用处理程序(在我的情况下我有 5 个),但对于每个下拉菜单,“发件人”始终是第一个。

风景 :

<asp:Repeater ID="OptionsRepeater" runat="server">
 <ItemTemplate>
  <asp:DropDownList ID="ddlOptions" runat="server"> 
 </ItemTemplate>
</asp:Repeater>

中继器数据源代码正常:

OptionsRepeater.DataSource = someDataSource;
OptionsRepeater.ItemDataBound += new RepeaterItemEventHandler(this.OptionsRepeaterItemDataBound);
OptionsRepeater.DataBind();

事件处理程序:

        protected virtual void OptionsRepeaterItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                // Get the view class for the current repeater record
                var dropDownData = e.Item.DataItem;
 DropDownList dropDownList = (DropDownList)e.Item.FindControl("ddlOptions");
                if (dropDownList != null)
                {
dropDownList.DataSource = dataSource;
                    dropDownList.DataTextField = "Title";
                    dropDownList.DataValueField = "Id";
                    dropDownList.AutoPostBack = true;
  dropDownList.DataBind();
                    dropDownList.SelectedIndexChanged += DropDownListSelectedIndexChanged;
                 }  
            }
         }



protected void DropDownListSelectedIndexChanged(object sender, EventArgs e)
        {
        //((DropDownList)sender).UniqueID is always the id of the first combo changed and never on the real one.
        }
4

1 回答 1

2

好的,无论如何它正在工作。它不起作用的原因是在数据绑定之后和选择更改事件处理程序之后,它被分配了我正在触摸其中一些数据上的 .selected=true :)

所以我的错误感谢您的提示。

如果有人有同样的问题,仍然是一个完全有效的解决方案:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DoDataBind();
        }

        private void DoDataBind()
        {
            List <List <Tuple<string, string>>> someDataSource = new List<List<Tuple<string, string>>>();
            someDataSource.Add(new List<Tuple<string, string>>
            {
                new Tuple<string, string>("item1", "item1"),
                new Tuple<string, string>("item2", "item2"),
                new Tuple<string, string>("item3", "item3")
            });
            someDataSource.Add(new List<Tuple<string, string>>
            {
                new Tuple<string, string>("item4", "item4"),
                new Tuple<string, string>("item5", "item5"),
                new Tuple<string, string>("item6", "item6")
            });
            someDataSource.Add(new List<Tuple<string, string>>
            {
                new Tuple<string, string>("item7", "item7"),
                new Tuple<string, string>("item8", "item8"),
                new Tuple<string, string>("item9", "item9")
            });
            OptionsRepeater.DataSource = someDataSource;
            OptionsRepeater.EnableViewState = false;
            OptionsRepeater.ItemDataBound += this.OptionsRepeaterItemDataBound;
            OptionsRepeater.DataBind();
        }

        /// <summary>
        /// Optionses the repeater item data bound.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.RepeaterItemEventArgs" /> instance containing the event data.</param>
        private void OptionsRepeaterItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                // Get the view class for the current repeater record
                var dataSource = e.Item.DataItem;

                DropDownList dropDownList = (DropDownList)e.Item.FindControl("ddlOptions");
                if (dropDownList != null)
                {
                    dropDownList.DataSource = dataSource;
                    dropDownList.DataTextField = "Item1";
                    dropDownList.DataValueField = "Item2";
                    dropDownList.EnableViewState = false;
                    dropDownList.AutoPostBack = true;
                    dropDownList.DataBind();
                    dropDownList.SelectedIndexChanged += DropDownListSelectedIndexChanged;
                }
            }
        }

        /// <summary>
        /// Drops down list selected index changed.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs" /> instance containing the event data.</param>
        private void DropDownListSelectedIndexChanged(object sender, EventArgs e)
        {
            result.Text += ((DropDownList)sender).UniqueID + "<br/>";

        }
    }
}

和观点:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
       <asp:Repeater ID="OptionsRepeater" runat="server">
         <ItemTemplate>
          <asp:DropDownList ID="ddlOptions" runat="server">  </asp:DropDownList>
         </ItemTemplate>
        </asp:Repeater>

        <asp:Literal ID="result" runat="server"></asp:Literal>
    </div>
    </form>
</body>
</html>

PS如果启用了视图状态,则事件未正确触发,因此请禁用转发器和下拉菜单上的状态。

于 2013-06-17T14:12:48.357 回答