Hi,
Please find the sample code for using multiple user control in the same page.
ASPX
~~~~~
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Test Multiple User Control in Same page</title>
<style type="text/css">
.innerTable
{
border: 1px solid black;
}
.outerTable
{
border: 1px solid rgb(209,209,209);
}
.Mandatory
{
border: 1px solid red;
}
</style>
<script type="text/javascript">
function ValidateIsEmpty(ctrl) {
if (ctrl.value != "") {
return true;
}
else {
ctrl.className = "Mandatory";
return true;
}
}
function Validate(obj, ctrl) {
if (document.getElementById(ctrl).value != "") {
alert("The value in " + ctrl + " is " + document.getElementById(ctrl).value);
return true;
}
else { return false; }
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="outerTable">
<tr>
<td>
User Control - 1
</td>
</tr>
<tr>
<td>
<uc1:myusercontrol id="myusercontrol1" runat="server" xmlns:uc1="#unknown" />
</td>
</tr>
<tr>
<td>
User Control - 2
</td>
</tr>
<tr>
<td>
<uc1:myusercontrol id="myusercontrol2" runat="server" xmlns:uc1="#unknown" />
</td>
</tr>
<tr>
<td>
User Control - 3
</td>
</tr>
<tr>
<td>
<uc1:myusercontrol id="myusercontrol3" runat="server" xmlns:uc1="#unknown" />
</td>
</tr>
</table>
</div>
</title></head></html>
Ascx design:
~~~~~~~~~~~~
<pre lang="HTML">
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="myusercontrol.ascx.cs"
Inherits="WebApplication1.myusercontrol" %>
<table class="innerTable">
<tr>
<td>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
</td>
<td>
<asp:Button ID="btnSubmit" Text="Submit" runat="server" OnClick="btnSubmit_Click"
/>
</td>
<td>
<asp:Label ID="lblDisplay" runat="server"></asp:Label>
</td>
</tr>
</table>
ASCX.CS
~~~~~~~~
public partial class myusercontrol : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
txtName.Attributes.Add("onblur", "return ValidateIsEmpty(this);");
btnSubmit.Attributes.Add("onclick", "return Validate(this,'" + txtName.ClientID + "');");
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
lblDisplay.Text = txtName.Text;
}
}
Note:
In the above coding am passing the child control ID ( Text box client ID from Code behind). The ClientID for a control will be unique. This helps in firing the event for the respective control.
Hope it helps.