Click here to Skip to main content
13,861,088 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


10 bookmarked
Posted 13 May 2011
Licenced CPOL

Invoke a ASP.NET page method from a User Control

, 14 Jun 2011
Rate this:
Please Sign up or sign in to vote.
How to execute ASP.NET method from control's event handler of a web user control.
ASP.NET- User control and Page communication

An idea just clicked in my mind, how can a button event handler code from web user control execute method from ASP.NET page. Action delegate by which I can encapsulates a method from the ASP.NET page and pass that delegate to the User control which will be stored in a delegate variable. In Page_load of ASP.NET page, Action delegate variable getCurrentTime of the User control will be set using SetCurrentTimeExecutor of the user control.

The event handler code from the user control will just run the delegate for example getCurrentTime().

namespace UserControlAndPageCommunication
    using System;
    public partial class CurrentTime : System.Web.UI.UserControl
        private Action getCurrentTime;

        public void SetCurrentTimeExecutor(Action executor)
            getCurrentTime = executor;

        protected void btnCurrentTime_Click(object sender, EventArgs e)

The above code block is the code behind of the CurrentTime web user control. The code behind has a delegate type Action which is getCurrentTime here.

namespace UserControlAndPageCommunication
    using System;
    public partial class _Default : UserControlAndPageCommunicationBase
        protected void Page_Load(object sender, EventArgs e)
        public void DisplayCurrentTime()
            txtCurrentTime.Text = base.WhatIsCurrentTime();

So now when the btnCurrentTime_Click event handler will fire, then it will execute getCurrentTime delegate eventually it will execute DisplayCurrentTime() from the Default page. DisplayCurrentTime will call WhatIsCurrentTime from the base class. Base class contains:

namespace UserControlAndPageCommunication
    using System;
    using System.Web.UI;

    public class UserControlAndPageCommunicationBase : Page
        public string WhatIsCurrentTime()
            return DateTime.Now.TimeOfDay.ToString();

The HTML contents of the page and user control are:

<%@ Page Title="Home Page" Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"
    Inherits="UserControlAndPageCommunication._Default" %>

<%@ Register Src="CurrentTime.ascx" TagName="CurrentTime" TagPrefix="UserControl" %>

    <form id="frmMain" runat="server">
        <legend>User Control and Page communication</legend>Current time
        <asp:TextBox ID="txtCurrentTime" runat="server"></asp:TextBox>
        <UserControl:CurrentTime ID="ucCurrentTime" runat="server" />

and user control:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="CurrentTime.ascx.cs"
    Inherits="UserControlAndPageCommunication.CurrentTime" %>
    To get current time, please click on the following button
    <asp:Button ID="btnCurrentTime" runat="server" Text="Current Time"
        onclick="btnCurrentTime_Click" />


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

You may also be interested in...


Comments and Discussions

GeneralMy vote of 1 Pin
sandeepkumarvemula15-Feb-13 0:11
membersandeepkumarvemula15-Feb-13 0:11 
GeneralRe: My vote of 1 Pin
Mohammad A Rahman15-Feb-13 0:17
memberMohammad A Rahman15-Feb-13 0:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03 | 2.8.190214.1 | Last Updated 14 Jun 2011
Article Copyright 2011 by Mohammad A Rahman
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid