Hello all,

Thanks for looking at my post and thank to anyone that provides me with the help I am looking for.

This is what I am trying to do. From the parent page have a few links that would do a postback and based on the link chosen a user control would then be loaded within a placeholder on the parent page. All that works fine.

The part that I just can’t seem to figure out is how to process the code behind for the user control if a click event is fired from the loaded user control. On any postback from a user control all I get is the parent page reloading to its original state.

I am about 65% in thinking it can’t be done.

Here is my sample code showing what I am trying to do:

Default1.aspx

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default1.aspx.vb" Inherits="Default1" %>  
<!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">  
            <asp:PlaceHolder runat="server" ID="myPlaceHolder">  
                Welcome please select a option.  
            </asp:PlaceHolder>  
            <p>  
                <asp:LinkButton id="LinkButton1" runat="server">Say Hello</asp:LinkButton><br />  
                <asp:LinkButton id="LinkButton2" runat="server">Say Goodbye</asp:LinkButton><br />  
            </p>  
        </form>  
    </body>  
</html>

Default1.aspx.vb

Partial Class Default1  
    Inherits System.Web.UI.Page  
  
    Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButton1.Click  
        myPlaceHolder.Controls.Clear()  
        myPlaceHolder.Controls.Add(LoadControl("Hello.ascx"))  
    End Sub  
  
    Protected Sub LinkButton2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButton2.Click  
        myPlaceHolder.Controls.Clear()  
        myPlaceHolder.Controls.Add(LoadControl("Goodbye.ascx"))  
    End Sub  
End Class

When I click on the link “Say Hello” the user control “Hello.ascx” does load in the placeholder just fine.

Hello.ascx

<%@ Control Language="VB" AutoEventWireup="false" CodeFile="Hello.ascx.vb" Inherits="Hello" %>  
What is your name? <asp:TextBox ID="TextBox1" runat="server"/>  
&nbsp;&nbsp;  
<asp:Button ID="Button1" runat="server" Text="Answer" />  
<br />  
<br />  
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>

Hello.ascx.vb

Partial Class Hello  
    Inherits System.Web.UI.UserControl  
  
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click  
        Label1.Text = "Hello " & TextBox1.Text  
    End Sub  
End Class

When I enter my name and click the “Answer” button the parent page reloading to its original state with no user control loaded. Why is the “Hello.ascx” user control not loaded with “Lable1” populated with the information for the click event from the code behind event?

Can someone tell me why and how to make it?

Thanks,
CroCrew~

hi,
The problem is that you are adding user control dynamically so what happens when you click on answer button default page again gets reloaded because whenever a events get fired the page is reloaded.so at that time you set text to label the user control is not present on default page.

hi,
load user controls in panels making panel display properties true or false using code behind as needed

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.