Introduction
In this blog you will learn how to create a
Web user control in asp.net4.
Step 1: Open visual studio
Take new web applicarion
Step 2: Add Web user control
Step 3: Add control
webusercontrol1.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs"
Inherits="article.WebUserControl1" %>
<asp:TextBox ID="txtvalue" runat="server"></asp:TextBox>
<asp:Button ID="btnClick" runat="server" OnClick="btnClick_Event" Text="Click" />
Step 4: Create an event,
webusercontrol1.ascx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace article
{
public delegate void click(String str);
public partial class WebUserControl1 : System.Web.UI.UserControl
{
public event click clickEvent;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnClick_Event(object sender, EventArgs e)
{
clickEvent(txtvalue.Text);
}
}
}
Step 5: Rebuild our application then add a web form and drag and drop
created user control to our new web form web form aspx pge webform.aspx
</head>
<body>
<form id="form1" runat="server">
<div>
<uc1:WebUserControl1 ID="WebUserControl11" runat="server" />
<br />
<br />
<asp:TextBox ID="TextBox1" runat="server" ReadOnly="true"></asp:TextBox>
</div>
</form>
</body>
</html>
Call event function in our application
web form aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace article
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.WebUserControl11.clickEvent += new click(WebUserControl11_clickEvent);
}
void WebUserControl11_clickEvent(string str)
{
TextBox1.Text = str;
}
}
}
Summery
This user control use in many forms and application.

aashish mangalPosted Jul 12, 2014, 3:02 AM
can i use multiple user control in single aspx page. like multiple dropdownbox, datetimepicker usercontrol in single aspx page