In this article I will explain with an example, how to add multiple instances of same UserControl on Page in ASP.Net using C# and VB.Net.
The UserControl will be placed inside a Repeater control and its multiple instances will be added on the same Page in ASP.Net using C# and VB.Net.
HTML Markup
The HTML Markup of the page consists of an ASP.Net Repeater control. The Repeater consists of an ASP.Net UserControl inside its ItemTemplate.
The Repeater control will be populated from code behind and the value of the Country field has been assigned to the UserControl.
Page
<%@ Register Src="~/UserControls/Country.ascx" TagName="Country" TagPrefix="uc" %>
<!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 id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<th>Id</th>
<th>Name</th>
<th>Country</th>
<th></th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><asp:Label ID="lblId" Text='<%# Eval("Id") %>' runat="server" /></td>
<td><asp:Label ID="lblName" Text='<%# Eval("Name") %>' runat="server" /></td>
<td><uc:Country ID="ucCountry" runat="server" Country='<%# Eval("Country") %>' /></td>
<td><asp:Button ID="btnDetails" Text="Get Details" runat="server" OnClick="GetDetails" /></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</form>
</body>
</html>
WebUserControl
The following WebUserControl is used inside the Repeater control discussed above. The HTML Markup of the UserControl consists of an ASP.Net Label control.
<asp:Label ID = "lblCountry" Text="" runat="server" />
Namespaces
You will need to import the following namespace.
C#
VB.Net
Binding the Repeater control
Inside the Page Load event, the Repeater control is populated using a DataTable with some dummy records.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
new DataColumn("Name", typeof(string)),
new DataColumn("Country",typeof(string)) });
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
Repeater1.DataSource = dt;
Repeater1.DataBind();
}
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id", GetType(Integer)), New DataColumn("Name", GetType(String)), New DataColumn("Country", GetType(String))})
dt.Rows.Add(1, "John Hammond", "United States")
dt.Rows.Add(2, "Mudassar Khan", "India")
dt.Rows.Add(3, "Suzanne Mathews", "France")
dt.Rows.Add(4, "Robert Schidner", "Russia")
Repeater1.DataSource = dt
Repeater1.DataBind()
End If
End Sub
Property to get and set from the WebUserControl
The following Public property is used inside the UserControl which allows one to get or set the value of the Label control.
C#
public string Country
{
get
{
return lblCountry.Text;
}
set
{
lblCountry.Text = value;
}
}
VB.Net
Public Property Country() As String
Get
Return lblCountry.Text
End Get
Set(value As String)
lblCountry.Text = value
End Set
End Property
Find and access WebUserControl inside the Repeater control on Button Click
When the Button inside the Repeater control is clicked, first the reference of the Button which is clicked is determined and then using the reference of the Button, the reference of the Repeater Item is determined.
Then the values of the Id and Name are fetched by finding the Label controls while the value of the Country is fetched by finding the UserControl inside the Repeater Item.
Finally the fetched values of the Id, Name and Country are displayed using JavaScript Alert message box.
C#
protected void GetDetails(object sender, EventArgs e)
{
RepeaterItem item = (sender as Button).NamingContainer as RepeaterItem;
string id = "Id: " + (item.FindControl("lblId") as Label).Text;
string name = "Name: " + (item.FindControl("lblName") as Label).Text;
string country = "Country: " + (item.FindControl("ucCountry") as UserControls_Country_CS).Country;
string message = string.Format("{0}\\n{1}\\n{2}", id, name, country);
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('" + message + "');", true);
}
VB.Net
Protected Sub GetDetails(sender As Object, e As EventArgs)
Dim item As RepeaterItem = TryCast(TryCast(sender, Button).NamingContainer, RepeaterItem)
Dim id As String = "Id: " + TryCast(item.FindControl("lblId"), Label).Text
Dim name As String = "Name: " + TryCast(item.FindControl("lblName"), Label).Text
Dim country As String = "Country: " + TryCast(item.FindControl("ucCountry"), UserControls_Country_VB).Country
Dim message As String = String.Format("{0}\n{1}\n{2}", id, name, country)
ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('" & message & "');", True)
End Sub
Screenshot
Demo
Downloads