Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is the user control,</p> <p>First Name: <pre><code>&lt;asp:TextBox ID="TextBoxFirstName" runat="server"&gt;&lt;/asp:TextBox&gt; &lt;asp:RequiredFieldValidator ID="RQV1" runat="server" ControlToValidate="TextBoxFirstName" ErrorMessage="*"&gt;&lt;/asp:RequiredFieldValidator&gt;</code></pre></p> <p>Last Name: <pre><code>&lt;asp:TextBox ID="TextBoxLastName" runat="server"&gt;&lt;/asp:TextBox&gt; &lt;asp:RequiredFieldValidator ID="RQV2" runat="server" ControlToValidate="TextBoxLastName" ErrorMessage="*"&gt;&lt;/asp:RequiredFieldValidator&gt;</code></pre></p> <p>Here is the Code-behind for the page exposing public properties,</p> <pre><code>public string GetFirstName { get { return TextBoxFirstName.Text; } set { TextBoxFirstName.Text = value; } } public string GetLastName { get { return TextBoxLastName.Text; } set { TextBoxLastName.Text = value; } }</code></pre> <p>In the code-behind, I am creating two public string variables setting the value to the TextBox control's Text property and returning the Text property of the TextBox controls.</p> <p>Then, in the main page,</p> <pre><code>&lt;form id=&quot;form1&quot; runat=&quot;server&quot;&gt; &lt;div&gt;This is page to add multiple user controls.&lt;/div&gt; &lt;p&gt; Customer1 Details:&lt;br /&gt; &lt;uc1:WebUserControl1 ID=&quot;WebUserControl11&quot; runat=&quot;server&quot; /&gt; &lt;/p&gt; &lt;p&gt; Customer2 Details:&lt;br /&gt; &lt;uc1:WebUserControl1 ID=&quot;WebUserControl12&quot; runat=&quot;server&quot; /&gt; &lt;/p&gt; &lt;asp:Button ID=&quot;Button1&quot; runat=&quot;server&quot; Text=&quot;Continue&quot; OnClick=&quot;GetCustDetails&quot;/&gt; &lt;asp:Button ID=&quot;Button2&quot; runat=&quot;server&quot; Text=&quot;Clear&quot; CausesValidation=&quot;false&quot; OnClick=&quot;ClearFields&quot; /&gt;&lt;br /&gt; Customer1 FirstName: &lt;asp:label ID=&quot;Label1&quot; runat=&quot;server&quot;&gt;&lt;/asp:label&gt;&lt;br /&gt; Customer1 LastName: &lt;asp:label ID=&quot;Label2&quot; runat=&quot;server&quot;&gt;&lt;/asp:label&gt;&lt;br /&gt; Customer2 FirstName: &lt;asp:label ID=&quot;Label3&quot; runat=&quot;server&quot;&gt;&lt;/asp:label&gt;&lt;br /&gt; Customer2 LastName: &lt;asp:label ID=&quot;Label4&quot; runat=&quot;server&quot;&gt;&lt;/asp:label&gt;&lt;br /&gt; &lt;/form&gt;</code></pre> <p>I am adding two instances of the user control to the page. Then there are four Label controls that will display the user control's properties on Button1 click. There is another Button2 that clears all the user control's TextBox controls.</p> <pre><code>protected void GetCustDetails(object sender, EventArgs e) { Label1.Text = WebUserControl11.GetFirstName; Label2.Text = WebUserControl11.GetLastName; Label3.Text = WebUserControl12.GetFirstName; Label4.Text = WebUserControl12.GetLastName; } protected void ClearFields(object sender, EventArgs e) { WebUserControl11.GetFirstName = string.Empty; WebUserControl11.GetLastName = string.Empty; WebUserControl12.GetFirstName = string.Empty; WebUserControl12.GetLastName = string.Empty; }</code></pre>
 

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