Tech Off Thread

3 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

ASP.Net - UserControl events in a ListView

Back to Forum: Tech Off
  • User profile image


    I have a page with a ListView that contains a custom UserControl. The usercontrol have events, but I cant figure out how to subscribe to these events.

    I tried to define in the aspx file without any luck. I've also tried to set in in the PreRender event of the usercontrol, but the event are always null in the usercontrol no matter what I do.

    This is what I got now;

    protected void animalCardControl1_OnPreRender(object sender, EventArgs e) {
    animalCardControl a = (animalCardControl)sender;

    a.DetailsClickedEvent += new animalCardControl.DetailsClickedHandler(animalCardControl1_DetailsClicked);
    a.Thumb = IsCheckBoxList;


    Any suggestions ??

  • User profile image

    Use the  FindControl() Method to receive a reference of UserControl you are using and then register an event.

  • User profile image

    It is too late to bind an event handler to a control during the PreRender stage of a page life cycle. Try the Init or InitComplete stage instead.

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"
    EnableViewState="false" Inherits="WebApplication1.WebForm1" %> <%@ Register Src="~/AnimalCardControl.ascx" TagName="AnimalCard" TagPrefix="uc" %> <!DOCTYPE html> <html> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <p><asp:Label ID="Label1" runat="server" /></p> <asp:ListView ID="ListView1" runat="server">
    <ItemTemplate> <p><uc:AnimalCard ID="AnimalCard1" runat="server"
    OnDetailsClicked="AnimalCard1_DetailsClicked" /></p> </ItemTemplate> <LayoutTemplate> <asp:PlaceHolder ID="itemPlaceholder" runat="server" /> </LayoutTemplate> </asp:ListView> </form> </body> </html>

    namespace WebApplication1
        using System;
        using System.Linq;
        using System.Web.UI.WebControls;
        public partial class WebForm1 : System.Web.UI.Page
            protected override void OnInitComplete(EventArgs e)
                ListView1.DataSource = Enumerable.Range(1, 10);
            protected void AnimalCard1_DetailsClicked(object sender, EventArgs e)
                Label1.Text = DateTime.Now.ToString();

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.