Repeater中的DropDownList:如何处理SelectedIndexChange并获取DataItem?

时间:2012-03-01 02:36:38

标签: c# asp.net .net repeater

我将带有AutoPostBack的DropDownList放在Repeater中 (ListItems填充在转发器的ItemDataBound上)

<asp:Repeater ID="rptWishlist" OnItemCommand="rptWishlist_ItemCommand" onItemDataBound="rptWishlist_ItemDataBound" runat="server">
  <ItemTemplate>
    ...
    <asp:DropDownList ID="ddlSize" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlSize_SelectedIndexChanged" />
    ...
  1. 首先,这个功能甚至没有在回发后被解雇

    protected void ddlSize_SelectedIndexChanged(object sender, EventArgs e)
    {
    //This function is never called
    }

  2. 如果我在使用DataItem之后如何获取它?

  3. 我这样做是错误的吗?

    提前谢谢。

5 个答案:

答案 0 :(得分:13)

要注册回发的下拉列表,请添加以下代码:

 protected virtual void RepeaterItemCreated(object sender, RepeaterItemEventArgs e)
    {
        DropDownList MyList = (DropDownList)e.Item.FindControl("ddlSize");
        MyList.SelectedIndexChanged += ddlSize_SelectedIndexChanged;
    }

在您的aspx文件中,将其添加到转发器标记:

OnItemCreated="RepeaterItemCreated"

然后,在你的ddlSize_SelectedIndexChanged函数中,你可以像这样访问父控件:

   DropDownList d = (DropDownList)sender;
   (RepeaterItem) d.Parent...

希望这会有所帮助。

答案 1 :(得分:2)

我发布您发布的代码部分没有问题。

当IsPostBack为true且在PageLoad期间,您是否在转发器上调用DataBind()? 如果是这样,您将丢失DDL上的SelectedIndexChanged

您应该存储ID,例如在值或HiddenField中,以在回发处理期间加载特定的DataItems(ListView为此目的使用DataKey)

作为一般准则:

  • 在PreRender
  • 期间调用DataBind()通常会更好
  • 如果基础数据未发生变化,则不应在回发期间调用DataBind()
  • 如果您执行上述两点,则无法使用DataItems item_created(因为只有在调用时,您的DataItem才可用) 的DataBind())

    protected void Page_Load(object sender, EventArgs e)
    {
        this.PreRender += new EventHandler(test_PreRender);
    }
    
    void test_PreRender(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            rptWishlist.DataSource = new int[] { 1, 2, 3, 4 };
            rptWishlist.DataBind();
        }
    }
    
    protected void rptWishlist_ItemCommand(object sender, RepeaterCommandEventArgs e)
    {
        //Command Code Here
    }
    
    protected void rptWishlist_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        var i = (int) e.Item.DataItem;
        var ddl = (DropDownList)e.Item.FindControl("ddlSize");
        for(int j=1; j<=i;j++)
        {
            ddl.Items.Add(new ListItem(){Text = j.ToString()});
    
        }
    }
    
    protected void ddlSize_SelectedIndexChanged(object sender, EventArgs e)
    {
        Response.Write("changed");
    }
    

答案 2 :(得分:1)

如果您只想触发OnSelectedIndexChanged,它应该是这样的:

Page.aspx - 来源

<FooterTemplate>
    <asp:DropDownList ID="ddlOptions"
             runat="server" 
             AutoPostBack="true" 
             onselectedindexchanged="ddlOptions_SelectedIndexChanged">
        <asp:ListItem>Option1</asp:ListItem>
        <asp:ListItem>Option2</asp:ListItem>
    </asp:DropDownList>
</FooterTemplate>

Page.aspx.cs - 代码隐藏

protected void ddlOptions_SelectedIndexChanged(object sender, EventArgs e)
    {
        //Event Code here.
    }

就是这样。您的活动现在将被调用。

答案 3 :(得分:1)

这里的答案很好但缺少关键的检查。如果您想知道为什么您没有将对象引用设置为对象错误的实例,请务必注意转发器将在任何数据项之前首先创建其HEADER。

执行此检查:

protected void rptProjects_ItemCreated(object sender, RepeaterItemEventArgs e)
{
     if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
     {
           ((DropDownList)e.Item.FindControl("yourcontrol")).SelectedIndexChanged += ddlAction_SelectedIndexChanged;
     }
}

答案 4 :(得分:-1)

试试这个

DropDownList drp = sender as DropDownList;
    int RepeaterItemIndex = ((RepeaterItem)drp.NamingContainer).ItemIndex;