views:

999

answers:

4

Hello to all,

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 an event to the DropDownList like so:

protected void MyRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
...
MyControl myControl = (MyControl)e.Item.FindControl("MyControl01");
myControl.DataSource = myObject;
myControl.DataBind();
myControl.DropDownList.SelectedItemChange += MyMethod_SelectedIndexChanged;
myControl.DropDownList.AutoPostBack = true;
....
}

protected void MyMethod_SelectedIndexChanged(object sender, EventArgs e)
{
//Do something.
}

The event never fires. Please I need help.

+3  A: 

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.

Ken Browning
A: 

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.

DavGarcia
+1  A: 

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
    }
CRice
A: 

Hi, I was also facing the same problem. So,I tried to attach the event handler(OnSelectedIndexChanged="LstFromControl_SelectedIndexChanged") to the drop down list as mentioned in the first reply of this post. But, if I try to access the page, it is not rendering the page. But, it is giving an error. If I remove that attribute from the control and add it inside the OnItemDataBound method, it is displaying the page properly. But, the event handler is not being triggered as expected. Am I following the proper procedure? Please help me.

Mamatha