Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Raise the event from the user control, and handle the event from the main page. Check out <a href="https://stackoverflow.com/questions/6192739/how-do-i-raise-an-event-in-a-usercontrol-and-catch-it-in-mainpage">this question</a>.</p> <p>//** EDIT **//</p> <p>You are adding the user control dynamically on button click. When you click the button on your user control it first will initiate postback on the main page - now your user control no longer exists (which is why the event is not raised). If you change your main page designer to look like this:</p> <pre><code>&lt;%@ Register Src="SomeUserControl.ascx" tagname="SomeUserControl" tagprefix="uc1" %&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head id="Head1" runat="server"&gt; &lt;title&gt;&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;form id="form1" runat="server"&gt; &lt;asp:ScriptManager ID="ScriptManager1" runat="server"&gt; &lt;/asp:ScriptManager&gt; &lt;div&gt; &lt;asp:UpdatePanel ID="upd1" runat="server" UpdateMode="Conditional"&gt; &lt;ContentTemplate&gt; &lt;asp:TextBox ID="TextBox1" runat="server"&gt;&lt;/asp:TextBox&gt; &lt;asp:Button runat="server" Text="Add User Control" Height="44px" ID="Nims" onclick="Unnamed1_Click" Width="133px" /&gt; &lt;asp:Panel ID="Panel1" runat="server" BackColor="#FFFFCC"&gt; &lt;uc1:SomeUserControl ID="userControl" runat="server" /&gt; &lt;/asp:Panel&gt; &lt;/ContentTemplate&gt; &lt;/asp:UpdatePanel&gt; &lt;/div&gt; &lt;/form&gt; </code></pre> <p> </p> <p>and your code-behind to look like this</p> <pre><code>protected void Page_Load(object sender, EventArgs e) { userControl.StatusUpdated += new EventHandler(userControl_StatusUpdated); } void userControl_StatusUpdated(object sender, EventArgs e) { GetDate(); } private void GetDate() { TextBox1.Text = DateTime.Today.ToString(); } </code></pre> <p>you will see what I mean. Try setting breakpoints on the page load events of your main page and your user control to see exactly the order in which things happen.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload