Assign an event to a custom control inside a Repeater control

后端 未结 3 599
野的像风
野的像风 2020-12-11 11:10

I have a Repeater control which in some of its cells contains a UserControl which contains a DropDownList. On the ItemDataBound event of the Repeater control I\'m assigning

相关标签:
3条回答
  • 2020-12-11 11:41

    First make sure your databinding is not resetting your dropdowns.

    Here is the code for the control which will nest inside the repeater ItemTemplate

    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ListBoxContainer.ascx.cs" Inherits="OAAF.Common.ListBoxContainer" %>
    
    <asp:ListBox ID="lstFromControl" runat="server" Rows="1" DataTextField="Text" DataValueField="Id" OnSelectedIndexChanged="LstFromControl_SelectedIndexChanged" AutoPostBack="true" />
    

    The code behind for the control which will nest inside the repeater ItemTemplate

    public partial class ListBoxContainer : System.Web.UI.UserControl
    {
        //declare the event using EventHandler<T>
        public event EventHandler<EventArgs> ListBox_SelectedIndexChanged;
    
        protected void Page_Load(object sender, EventArgs e)
        {
        }
    
        protected void LstFromControl_SelectedIndexChanged(object sender, EventArgs e)
        {
            //fire event: the check for null sees if the delegate is subscribed to
            if (ListBox_SelectedIndexChanged != null)
            {
                ListBox_SelectedIndexChanged(sender, e);
            }
        }
    }
    

    Note that this above control uses the listbox change event internally, then fires an event of its own: ListBox_SelectedIndexChanged. You could create custom event args here as well, but this uses the standard EventArgs.

    Your repeater which has the control may look like this

    <asp:Repeater ID="rptTest" runat="server">
    <ItemTemplate>
        <br />
        <ctrl:wucListBox ID="listBoxControl" runat="server" OnListBox_SelectedIndexChanged="ListBoxControl_SelectedIndexChanged" />
    </ItemTemplate>    
    </asp:Repeater>
    

    Register the control at the top of the page the repeater is on, for example

    <%@ Register Src="~/Common/ListBoxContainer.ascx" TagName="wucListBox" TagPrefix="ctrl" %>
    

    It handles the event ListBox_SelectedIndexChanged, and the method which handles this is in the code behind of the page the repeater sits on.

        protected void ListBoxControl_SelectedIndexChanged(object sender, EventArgs e)
        {
            //some code
        }
    
    0 讨论(0)
  • 2020-12-11 11:46

    There are two things you can try to see if it will help:

    1. Try binding your MyRepeater on every page request, not just when !IsPostBack.
    2. Bind MyRepeater inside OnInit.

    For 1) If your dynamically created controls are created the first time the page loads and then again when postback occurs, ASP.NET will notice that the event raised matches and will fire the event.

    For 2) The designer used always place event attachment in OnInit, though it should work fine in OnLoad too.

    0 讨论(0)
  • 2020-12-11 11:47

    Your event is not being raised in a PostBack because your event handler has not been attached (it is only attached during the iteration of the page life-cycle when your repeater is databound).

    If you attach your event handler declaratively in the markup such as:

    <asp:Repeater ID="Repeater1" runat="server">
         <ItemTemplate>
             <asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" />
        </ItemTemplate>
    </asp:Repeater>
    

    Then your event handler will be called during PostBacks.

    0 讨论(0)
提交回复
热议问题