locked
Dynamic UserControl Textchange won't fire inside modal popup RRS feed

  • Question

  • User-308948172 posted

    Hi been searching for the whole week to solve my problem but I still haven't fegure it out.

    how to Update my user control without closing the modal popup,

    I'm loading a user control programmatically from my Master Page/Main Page 

    Default.aspx.vb

        Protected Sub img1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles img1.Click
    
            Dim u As Control = LoadControl("~/WebUserControl.ascx")
            Dim txt As New TextBox
            txt = CType(u.FindControl("txtName"), TextBox)
            txt.Text = "Michel"
    
            UcPlaceHolder.Controls.Add(u)
            MPEUC.Show()
        End Sub
    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
    <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>
    <%@ Reference Control="~/WebUserControl.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>Dynamic User Control</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <ajaxToolkit:ToolkitScriptManager ID ="AjaxScriptManager" runat ="server" />
            <asp:HiddenField ID = "hidden" runat="server" />
            <ajaxToolkit:ModalPopupExtender ID="MPEUC" runat="server" PopupControlID="divPopUp"
                CancelControlID="btnClose2"
                TargetControlID="hidden">            
            </ajaxToolkit:ModalPopupExtender>   
            <div id="divPopUp" style="display: none;">
                <asp:panel runat="server" ID="PanelBg">
                    <fieldset>
                        <legend>Update Panel</legend>                
                        <div>
                                <asp:Panel id="UcPlaceHolder" runat="server"></asp:Panel>                                     
                                <asp:Button ID="btnClose2" runat="server" Text="Close" />                                
                        </div>
                    </fieldset>
                </asp:panel>
            </div>         
    <asp:ImageButton ID="img1" runat="server" Height="16px" Width="117px" />
        </form>
    </body>
    </html>
    


    My User Control

    WebUserControl.ascx.vb

    Partial Class WebUserControl
        Inherits System.Web.UI.UserControl
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
        End Sub
    
    
        Protected Sub txtAge_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtAge.TextChanged
            Me.output.Text = String.Format("I`m {0} years old.", Me.txtAge.Text)
        End Sub
    
        Protected Sub txtName_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtName.TextChanged
            Me.output.Text = String.Format("Hi!, my name is {0}. ", Me.txtName.Text)
        End Sub
    End Class
    

    <%@ Control Language="VB" AutoEventWireup="false" CodeFile="WebUserControl.ascx.vb" Inherits="WebUserControl" %>
    <asp:UpdatePanel ID="UpdateBcode" runat="server" UpdateMode="Conditional">    
        <Triggers>        
            <asp:AsyncPostBackTrigger ControlID="txtName"  EventName="TextChanged" />
            <asp:AsyncPostBackTrigger ControlID="txtAge"  EventName="TextChanged" />
        </Triggers>
        <ContentTemplate>
            <div>
                <label>Name:</label>
                <asp:TextBox ID="txtName"  runat="server" AutoPostBack="true" />
                <br />
                <label>Age  :</label>
                <asp:TextBox ID="txtAge" runat="server" AutoPostBack="true" />
                <br />
                <br />
                <asp:Label ID="output" runat="server" Text=""></asp:Label>
            </div>
        </ContentTemplate>
    </asp:UpdatePanel>
    

    Thursday, January 30, 2014 3:29 AM

Answers

All replies

  • User1734617369 posted

    Hi,

    Your problem is due to the fact that you adds the control programmatically which when you are doing a PostBack actually removes the control from the Page and the events of the text boxes will never get fired, to be able to catch those events you will have to readd the control when it is being posted back, like for example in your code:

    Public Class _Default
        Inherits System.Web.UI.Page
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Page.IsPostBack Then
                Dim target As String = Page.Request.Form("__EVENTTARGET")
    
                If target = "ctl02$txtAge" Or target = "ctl02$txtName" Then
                    Dim u As WebUserControl = LoadControl("~/WebUserControl.ascx")
                    UcPlaceHolder.Controls.Add(u)
                End If
            End If
        End Sub
    
        Protected Sub img1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles img1.Click
    
            Dim u As WebUserControl = LoadControl("~/WebUserControl.ascx")
            Dim txt As New TextBox
            txt = CType(u.FindControl("txtName"), TextBox)
            txt.Text = "Michel"
    
            UcPlaceHolder.Controls.Add(u)
            MPEUC.Show()
        End Sub
    
    End Class

    Not the most elegant way but it works.

    Best regards
    Johan

    Thursday, January 30, 2014 6:33 AM
  • User-308948172 posted

    I'll try it on office hour

    by adding

    <%@ Register Src="~/WebUserControl.ascx" TagPrefix="uc" TagName="myUc" %>

    on my Main page things are working fine.

    Thanks

    BTW

    would you mind help me with this scenario

    http://forums.asp.net/p/1964530/5616384.aspx?p=True&t=635266728130866218&pagenum=1

    Thanks :)

    Thursday, January 30, 2014 10:00 AM
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 31, 2014 8:39 AM