in

Problema con eventos de ASCX cargados DINAMICAMENTE

Last post 05-18-2007 21:45 by jfbonnin. 1 replies.
Page 1 of 1 (2 items)
Sort Posts: Previous Next
  • 05-18-2007 14:19

    • gjaume
    • Top 10 Contributor
    • Joined on 03-07-2007
    • Posts 14
    • Points 274

    Problema con eventos de ASCX cargados DINAMICAMENTE

    Hola a tod@s,

    Planteo el siguiente problema a ver si alguno de vosotros sabe porque ocurre.

    Tengo una página ASPX llamada "CargaDinamica.aspx" y un control ASCX llamado "Contenido.ascx". En el ASPX hay un botón "btnMostrar" que al pulsarlo carga el ASCX dinámicamente en un placeHolder.

    El ASCX tiene un botón "btnOk" con un onClick="btnOk_Click". El problema reside en que al cargar este control de forma dinámica en la página este método nunca se ejecuta.

    Dejo el código aquí para que lo podáis probar vosotros mismos a ver si encontramos el motivo.

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="Contenido.ascx.cs" Inherits="Contenido" %>
    <h1>Control con contenido</h1>
    Hola Mundo!
    <br />
    <
    asp:Button ID="btnOk" runat="server" Text="ok" OnClick="btnOk_Click" />

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    public partial class Contenido : System.Web.UI.UserControl
    {
       protected void Page_Load(object sender, EventArgs e)
       {}
       protected void btnOk_Click(object sender, EventArgs e)
       {
          Response.Write(
    "<script>alert('hola mundo!');</script>");
       }
    }

     

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="CargaDinamica.aspx.cs" Inherits="CargaDinamica" %>
    <%
    @ Reference Control="~/ascx/Contenido.ascx" %>
    <!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>Carga dinámica</title>
       </
    head>
       <
    body>
          <form id="form1" runat="server">
          <div>
             <asp:Button ID="btnMostrar" runat="server" Text="mostrar" OnClick="btnMostrar_Click" />
             <asp:PlaceHolder ID="placeHolder1" runat="server">
             </asp:PlaceHolder> 
          </div>
          </form>
    </
    body>
    </
    html>

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    public partial class CargaDinamica : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {}

    protected void btnMostrar_Click(object sender, EventArgs e)
    {
       // Creamos el control "Contenido" de forma dinámica
       Contenido contenido = (Contenido)Page.LoadControl("~/ascx/Contenido.ascx");
       // Agregamos el control a la página introduciéndolo en el objeto PlaceHolder
       this.placeHolder1.Controls.Add(contenido);
    }
    }

    eConcept Consulting
    Internet Business Solutions
    http://econcept.es
    Filed under:
    • Post Points: 22
  • 05-18-2007 21:45 In reply to

    Re: Problema con eventos de ASCX cargados DINAMICAMENTE

    Hola,

    Cuando pulsas el botón "Ok" del control "Contenido.acx" se está haciendo un postback a "CargaDinamica.aspx", ahí "pierdes" el control "Contenido", puesto que dicho control no existe en la página, sino que se carga al pulsar Mostrar. 

    El problema no radica en que no se esté ejecutando el evento, sino que el control ya no existe para manejar el evento cuando se hace el postback en el servidor.

    Podrías arreglarlo con algo parecido al código siguiente:

    1 protected void Page_Load(object sender, EventArgs e) 2 { 3 if (Page.IsPostBack) 4 { 5 object controlName = ViewState["LoadMyControl"]; 6 AddControl(controlName as string); 7 } 8 } 9 protected void btnMostrar_Click(object sender, EventArgs e) 10 { 11 ViewState["LoadMyControl"] = "~/Contenido.ascx"; 12 AddControl("~/Contenido.ascx"); 13 } 14 15 private void AddControl(string controlName) 16 { 17 18 if (controlName != null) 19 { 20 // Creamos el control "Contenido" de forma dináica 21 Contenido contenido = (Contenido)Page.LoadControl(controlName.ToString()); 22 // Agregamos el control a la página introduciéndolo en el objeto PlaceHolder 23 contenido.ID = "contenido"; 24 this.PlaceHolder1.Controls.Add(contenido); 25 } 26 }

    Espero que te ayude.

    Jose Fco Bonnin
    -----------------------
    Coordinador Baleares on .NET
    www.josefcobonnin.com
    • Post Points: 5
Page 1 of 1 (2 items)
Baleares on .NET®
Powered by Community Server (Commercial Edition), by Telligent Systems