Link to home
Start Free TrialLog in
Avatar of mikha
mikhaFlag for United States of America

asked on

how to call a function in aspx page , from a user control (ascx) page in asp.net application.

I have an asp.net web application, with master pages , aspx pages and also user control (ascx ) pages. sample code below

I have a function in menu.ascx , user control page, which needs to fire a function in default.aspx page.
is there a way to do this ?

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs" Inherits="masterpage.Site1" %>  
<%@Register Src=~/menu.ascx” TagName=”menu” TagPrefix=”uc1” %>
<!DOCTYPE html>  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head runat="server">  
    <asp:ContentPlaceHolder ID="head" runat="server">  
    </asp:ContentPlaceHolder>  
</head>  

<body>  
    <div id="navigaion">
         <uc1:menu ID=”menuControl” runat=”server” />
    </div>
    <div id="maincontent">  
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">   </asp:ContentPlaceHolder>  
    </div>  
</body>  
</html>
Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

Essentially you create an event in your ascx child control that the page can subscribe to.


ASCX:
  public event EventHandler MyEvent;

    private void OnMyEvent()
    {
        if (MyEvent != null)
        {
            MyEvent(this, EventArgs.Empty);
        }
    }

  // calll OnMyEvent from another function when you want to fire the event from within the ASCX.

Open in new window


ASPX:
MyUserControl.MyEvent += new  EventHandler(MyUserControl_MyEventTriggered)

private void MyUserControl_MyEventTriggered(object sender, EventArgs e)
    {
        // ... do something when event is fired
    }

Open in new window

Avatar of mikha

ASKER

@Kyle- Thanks for your comment.  I am new to events and delegates in .NET. I will research on this as well.

but based on your comment.

1. can I fire the event from the ascx page (not from cs code) .  can i define my own custom event , similar to say , onbuttonclick = callsomemethod in the ascx page?
2. my use case is , I have a master page , with both ascx and aspx page that loads with in the master page, and I need to pass an int parameter from ascx page to aspx page.

how can i pass an int parameter  ?
if you have sample of master page, aspx and ascx page , working together , will give me a better picture. right now, i'm not entirely sure on how this events are fired and tied together.
ASKER CERTIFIED SOLUTION
Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial