Before looking at the code, Read about – What is JumpStart?
NOTE –
- The following code demos on how to raise an event of UsercControl which is on Parent page.
- We got a UserControl (TestUserControl.ascx), which got a button in it. On Button click, we use a delegate to call an event from the parent page.
- Even though personally I can’t vote to this approach of linking Page to its UserControl, but its worthy to know the procedure (as some situations may demand it).
================================================================================================
TestUserControl.ascx
This is the UserControl, which we incroporate on the Page (Page.aspx).
================================================================================================
<%@ Control Language="C#" ClassName="TestUserControl" %> <script runat="server"> public event EventHandler UserControlButton_click; protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { if (UserControlButton_click != null) { UserControlButton_click(sender, e); } } </script> <asp:Button ID="Button1" runat="server" Text="UserControl Button" OnClick="Button1_Click" />
================================================================================================
Page.aspx
We use the above mentioned Usercontrol in this page.
================================================================================================
<%@ Page Language="C#" %> <%@ Register Src="~/TestUserControl.ascx" TagName="uc" TagPrefix="uctrl" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> protected void Page_Load(object sender, EventArgs e) { TestUserControl tuc = (TestUserControl)Page.LoadControl("~/TestUserControl.ascx"); tuc.ID = "uc1"; tuc.UserControlButton_click += new EventHandler(Page_UserControlButton_click); Page.Form.Controls.Add(tuc); } protected void Page_UserControlButton_click(object sender, EventArgs e) { Response.Write("Hello!!! This is from UserControl Button Click"); } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> </div> </form> </body> </html>
================================================================================================
OUTPUT
================================================================================================
Before PostBack – After PostBack –







