Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>User Controls essentially should be unaware of any pages outside the control. The better approach would be to have the control expose properties and events that the page itself (master page or normal) will use to set and retrieve values. Take this simple example:</p> <pre><code> class PassValueEventArgs : EventArgs { public string Value { get; set; } } public event EventHandler&lt;PassValueEventArgs&gt; RequestingValue; public void ControlDoingWork() { PassValueEventArgs e = new PassValueEventArgs(); if (RequestingValue != null) { RequestingValue(this, e); } string fromHandlingPage = "Received " + e.Value + " from a handling page."; } </code></pre> <p>Then whenever the user control should have a value, the page containing the user control can just handle the RequestingValue event and send the value to the user control. Otherwise just expose a public property of the user control, which you can even make databound, for an even easier solution. <br /> Adding a complete example of the event-driven approach: <br /> WebUserControl1EventArgs.cs</p> <pre><code>public class WebUserControl1EventArgs : EventArgs { public double ValueToSquare { get; set; } } </code></pre> <p>WebUserControl1.ascx</p> <pre><code>&lt;%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="WebApplicationCS1_net20.WebUserControl1" %&gt; Text below will display "Nothing passed from parent page." if the event is unhandled, else will display the square of the number passed if handled.&lt;br /&gt;&lt;br /&gt; &lt;asp:Label runat="server" ID="Label1" Font-Bold="true" Font-Size="Larger" Text="Nothing passed from parent page."&gt;&lt;/asp:Label&gt; </code></pre> <p>WebUserControl1.ascx.cs</p> <pre><code>public partial class WebUserControl1 : System.Web.UI.UserControl { public event EventHandler&lt;WebUserControl1EventArgs&gt; RequestingNumber; protected void Page_Load(object sender, EventArgs e) { ControlDoingWork(); } private void ControlDoingWork() { if (RequestingNumber != null) { WebUserControl1EventArgs e = new WebUserControl1EventArgs(); RequestingNumber(this, e); Label1.Text = (e.ValueToSquare * e.ValueToSquare).ToString(); } } } </code></pre> <p>WebForm1.aspx</p> <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplicationCS1_net20.WebForm1" %&gt; &lt;%@ Register src="WebUserControl1.ascx" tagname="WebUserControl1" 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 runat="server"&gt; &lt;title&gt;&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;form id="form1" runat="server"&gt; &lt;div&gt; &lt;uc1:WebUserControl1 ID="WebUserControl11" runat="server" OnRequestingNumber="WebUserControl11_RequestingNumber" /&gt; &lt;/div&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>WebForm1.aspx.cs</p> <pre><code>public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void WebUserControl11_RequestingNumber(object sender, WebUserControl1EventArgs e) { e.ValueToSquare = 3.3; } } </code></pre> <p>WebForm2.aspx</p> <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplicationCS1_net20.WebForm2" %&gt; &lt;%@ Register src="WebUserControl1.ascx" tagname="WebUserControl1" 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 runat="server"&gt; &lt;title&gt;&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;form id="form1" runat="server"&gt; &lt;div&gt; &lt;uc1:WebUserControl1 ID="WebUserControl11" runat="server" /&gt; &lt;/div&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>WebForm2.aspx.cs</p> <pre><code>public partial class WebForm2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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