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>
mikha eAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kyle AbrahamsSenior .Net DeveloperCommented:
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

0
mikha eAuthor Commented:
@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.
0
Kyle AbrahamsSenior .Net DeveloperCommented:
can I fire the event from the ascx page

You would need some kind of postback to do this.

can i define my own custom event , similar to say , onbuttonclick = callsomemethod in the ascx page?
Yep, that's exactly what you're doing.

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.


The question is when does that ASCX pass the int back to the aspx?  What triggers it?

to pass a parameter you need to modify my original ASCX a little bit.  I'm also going to include say a button click which I'm assuming the user clicked  to get the button_click to fire.
// this is just saying this control has a custom event called MyEvent.  
public event EventHandler<CustomIntEventArgs > OnMyEvent;

  // this is what you call to fire the event.
    private void RaiseMyEvent(int x)
    {
       //if someone is subscribed to my event
        if (OnMyEvent != null)
        {
               //create custom event args (defined below).
            var args = new CustomIntEventArgs(){ myInt= x};    
          
             //raise the event.
            OnMyEvent(this, args);
 
        }
     // if nothing is subscribed to this nothing to do.
    }

  private void btn1_Clicked(Object sender, EventArgs e)
{
   // START HERE.  The user clicked this button, simply increments and saves X in session, fires an event each time.
   int x = 0;
   int.TryParse( Session["checkMe"] , out x);
   x++;
   Session["checkMe"] = x;
   // fire this controls event.
   RaiseMyEvent(x);
}

public class CustomIntEventArgs : EventArgs
{
    public int myInt {get;set;}
}

Open in new window


On the aspx:

//Page_Init.
{
// this subscribes the page to the event.
MyASCX.OnMyEvent += MyASCX_MyEvent;
// other stuff
}


private void MyASCX_MyEvent (object sender, EventArgs e)
{
  // what happens on the page when the event is fired?
  CustomIntEventArgs args = (CustomIntEventArgs) e;
 // args.myInt now is on your aspx.
}

Open in new window


Please note this was done by hand and not in a compiler, but should be enough to get you going.  Hope it helps.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.