Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Catching click event inside an UpdatePanel of a User Control

Posted on 2009-07-06
2
Medium Priority
?
575 Views
Last Modified: 2012-05-07
I was experimenting with dynamically loaded user controls.  

For some reason, when I load (from PickListUser) my PickList control, the control is displayed reasonably - with Region.xml as given, the repeater shows two lines with a name and button on each.

However, when I click the button, ONLY the load code on my host page (PickListUser) is called (with IsPostBack==true), neither the load event nor the click event on the PickList code behind are triggered.
Picklist.ascx:
---------------------------------
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Picklist.ascx.cs" Inherits="Picklist" %>
<input type="hidden" runat="server" ID="path" />
<input type="hidden"  runat="server" ID="parent" />
<asp:Repeater ID="Repeater1" runat="server" >
<ItemTemplate>
<asp:UpdatePanel runat="server" ID="mypanel" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label runat="server" ID="name" Text='<%# Eval("Name") %>' />
<asp:Button runat="server" ID="select" Text="select"  /><br />
<asp:Label runat="server" ID="display" />
</ContentTemplate>
</asp:UpdatePanel>
</ItemTemplate>
</asp:Repeater>
 
 
Picklist.ascx.cs:
---------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class Picklist : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string[] elements = path.Value.Split(',');
            XmlDataSource source = new XmlDataSource();
            source.DataFile = "App_Data/" + elements[0] + ".xml";
            Repeater1.DataSource = source;
            Repeater1.DataBind();
        }
        foreach (RepeaterItem x in Repeater1.Items)
        {
            (x.Controls[1].Controls[0].Controls[3] as Button).Click += Command;
        }
    }
 
 
    protected void Command(object source, EventArgs e)
    {
        display.Text = "foo";
    }
}
 
PickListUser.aspx
---------------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PickListUser.aspx.cs" Inherits="PickListUser" %>
 
<!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>
    User!
<asp:ScriptManager runat="server" ID="mgr" />
    </div>
    </form>
</body>
</html>
 
 
PicklistUser.ascx.cs:
---------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
 
public partial class PickListUser : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Control main = LoadControl("~/Picklist.ascx");
            HtmlInputHidden path = main.FindControl("path") as HtmlInputHidden;
            path.Value = "region,country,call";
            FindControl("form1").Controls.Add(main);
            mgr.RegisterAsyncPostBackControl(main);
        }
    }
}
 
 
Region.xml
---------------------------------
<?xml version="1.0" encoding="utf-8" ?>
<regions>
  <region name="america" id="1" parent="0"/>
  <region name="europe" id="2" parent="0"/>
</regions>

Open in new window

0
Comment
Question by:Jens Fiederer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 24787982
If you are going to dynamically load the control, then it needs to be loaded all the time.  You must reload the control even if the page posts back, or none of the events will be wired up.
0
 
LVL 23

Author Closing Comment

by:Jens Fiederer
ID: 31600293
Thanks - reloading the whole kibosh did it for me ...  to my surprise, without losing state in the items I was reloading!
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question