How to handle UserControl events inside a ListView?

Here is the structure I have set up:

<UserControl1>
    <ListView>
        <UserControl2 />
        <UserControl2 />
        <UserControl2 />
        <UserControl2 />
    </ListView>
</UserControl2>

When the user clicks "Edit" on UserControl2 I want to handle that event on UserControl1. So I created an event on UserControl2, added a handler on UserControl1 on ListView DataBind ,and raised the UserControl2 event on clicking "Edit".

But when the user clicks "Edit" the whole thing posts back and the handler is lost on UserControl1. The problem is, I can't rebind that ListView on every postback as it's a very heavy, time-consuming operation to re-add the handler.

So how can I get UserControl1 to see UserControl2's event?

Things I have tried:

  1. Static/Shared Events on the Usercontrol2... bad idea on an asp.net app, evidently those static events will getting mixed up some how, they exist across different webrequests I suppose.
  2. Adding a userControl1 instance property (ParentControl1) to UserControl2... nested UserControls are not allowed in asp.net.

Thanks for your help.

Answers


I haven't done web forms in awhile-- but I was interested in this.

These three files will demonstrate how you can subscribe to events with user controls.

  • Basically my UserControl1 has an event which I can subscribe to.
  • UserControl2 has a ListView which will contain instances of UserControl1. I can subscribe to the event in the instances of UserControl1 while in UserControl2.
  • Default.aspx contains an instance of UserControl2

Clicking on any of the events in UserControl1 will fire the event-- which in this case just sends the text of the button which is in UserControl1.

The code in VB should be pretty similar.

UserControl1

<%@ Control Language="C#" AutoEventWireup="true" %>
<script runat="server">
    public delegate void SillyHandler(object sender, string text);
    public event SillyHandler SomethingHappened;

    public string Name { get; set; }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (SomethingHappened != null)
            SomethingHappened(this.Button1, this.Button1.Text);
    }
</script>
<asp:Button ID="Button1" runat="server" Text="<%#this.Name %>" onclick="Button1_Click" />

UserControl2

<%@ Control Language="C#" AutoEventWireup="true" %>
<%@ Register src="UserControl1.ascx" tagname="UserControl1" tagprefix="uc1" %>
<script runat="server">
    public List<string> DataSource { get; set; }


    protected void SomethingHappened(object sender, string txt)
    {
        MyLabel.Text = txt;
    }
</script>
<asp:ListView runat="server" ID="ListView1" DataSource="<%#DataSource %>">
    <ItemTemplate>
        <uc1:UserControl1 ID="UserControl11"  runat="server" OnSomethingHappened="SomethingHappened" Name="<%#Container.DataItem %>"/>
    </ItemTemplate>
</asp:ListView>

<asp:Label runat="server" ID="MyLabel" />

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" %>

<%@ Register src="UserControl2.ascx" tagname="UserControl2" tagprefix="uc1" %>

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            this.UserControl21.DataSource = new List<string> { "A", "B", "C" };
            this.UserControl21.DataBind();
        }
    }
</script>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <uc1:UserControl2 ID="UserControl21" runat="server" />

    </div>
    </form>
</body>
</html>

Need Your Help

Uploading multiple/large files

asp.net asp.net-mvc-3

I have this page where a user can upload documents (multiple documents, size limit 10MB each). It is a two step process. Step 1 has the input form. Step 2 is the preview page with a submit button. ...

The £ sign is shown as a diamond with a question mark in the middle

jsp spring-mvc utf-8 character-encoding spring-roo

I'm creating a webapp which involves displaying financial data to the user. Being from the UK and using GBP £ for currency, this character is used a lot.