Calling a function on a page from a control that is contained on the page

I have a page that contains several Web User Controls. Is it possible to call a function/subroutine on the page from inside one of the user controls?

TIA,


Owen
0
ojm37
2/27/2009 4:08:08 PM
asp.net.web-forms 93655 articles. 6 followers. Follow

6 Replies
702 Views

Similar Articles

[PageSpeed] 21

yes you follow the following:

1- add new class call it BasePage which inhirits from System.Web.UI.Page.

2- let the code behind class for the page  that will hold the control inhirits from this BasePage Class.

3-add the function you need to call in the BasePage Class.

4- in the control in the button click event for an example, you will use the following code to get the page which contains this control, cast it to the BasePage then call the function testmethod() from it.

((BasePage)this.Page).testmethod();

I hope this will help,

please do not forget to mark my reply as answer if it's for the others to make use of it..

have a nice day

Haitham ELRefaie

0
haitham
2/28/2009 6:36:49 AM

this much will be sufficient

public void testmethod()

{}

in the user control. call this.Page.testmethod();

 


SHASHANK BHIDE
CAPGEMINI INDIA
"THE ROAD TO SUCCESS IS ALWAYS UNDER CONSTRUCTION MARK MY RESPONSE AS ANSWER TO HELP ME BUILD IT :)"
0
shashankgwl
2/28/2009 7:26:21 AM

 Hi mate,

you just need to declare public functions to access them from outside the control.

Put following in your usercontrol :

public void GetUserControlvalue()

{

 //You code here

}

 

Now you can access it in your aspx page like below:

usercontrol1.GetUserControlvalue();


Please mark it as answer if this post seems to be useful.
0
nishit_123
2/28/2009 11:33:33 AM

dear Owen,

do you want to place the method in the page and call it from the control or do you want to place the method in the control and call it from the page ?

my reply was for the assumption that you want to place a method in the page that holds a specific control and call the method from inside the control..

 Haitham ElRefaie

please do not forget to mark my post as answer if it helps..

0
haitham
3/1/2009 4:18:26 AM

Actually, what I ended up doing in this specific case was to create and raise an event in the control and then handle the event from the page that contains the control.

 Thanks for the suggestions....


Owen
0
ojm37
3/2/2009 2:11:18 PM

ojm37:

Actually, what I ended up doing in this specific case was to create and raise an event in the control and then handle the event from the page that contains the control.

 Thanks for the suggestions....

Based on my experience, I suggest you solve it by declaring a event in User Control and register this event  in the page, please refer to the following code:

    private delegate void UserControlHandler(object obj,EventArgs e);
    public event UserControlHandler UserEvent;
    public void OnUserAction(EventArgs e)
    {
        if (UserEvent != null)
        {
            UserEvent(this, e);
        }
    }

 At the same time, please refer to the follosing links:

This article introduce that how to handle the event of UserControl by page.

1. http://dotnet.org.za/thea/archive/2004/07/15/2834.aspx


Gary yang - MSFT
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
0
Gary
3/4/2009 3:48:06 AM
Reply:

Similar Artilces:

Calling a method from a control page on the axpx page where the control page exists.
Hello,  I have an .aspx page that has a method called grdMain_DataBind().  I have a control page that loads within the aspx page which has its own logic.  I have a button on my control page called "SAVE".  How can I call the method grdMain_DataBind thats in my aspx logic from the control that exists on that page when I click SAVE button.  Is there a way? If I were you, I'd simply create a "Save" Event within your control. When the Button in your control is pressed, in the Click handler for that Button you can raise the event. Then, within t...

Call codebehind method from aspx page OR call javascript function of usercontrol control from page
Hi, I have a page which contains a user control.  When a user clicks on a submit button, I'd like to detect when elements in the user control have changed and popup a message box if changes have been detected to confirm if the user wants to save the changes. Obviously the message box is executed using javascript.  But I'd really like to be able to call a method in the code behind of the page which queries the user control if it has changed.  Is there a way of calling the code behind method from within a javascript function? Otherwise, if I make all the functions execut...

Can my web page with a form submit to other web pages forms?
I was wondering if I created an aspx page that had a form on it that I could use the submit button to submit this form's contents to other web pages forms(servers accepting those form's data). I am thinking this would be a great way to update data on particular web pages without having to actually visit those similar web pages. Any ideas, examples would be much appreciated. Thanks in advance.  Yes you can.  It's called Cross Page Posting: http://msdn2.microsoft.com/en-us/library/ms178139.aspxAlso: http://msdn2.microsoft.com/en-us/library/ms178140.aspx might ...

From a content page, calling function of control on master page
I've got a master page with a user control on it. The user control class has a public function. Is it possible to call this function from a content page? Also, in doing so, I need to pass a bool to said function. And if it is, how?An example would be very helpful. I'm still learning the 2.0 features. Thanks! Reference this article: Passing Information Between Content and Master Pageswww.hopeasp.net...

In a web page(c#), I want to call another page and come back to next instruction of calling page
In a web page(c#), I want to call another page and come back to next instruction of calling page. Called page have no web item , it has only c# codes. after executing of this page (by IExplore), I want the remaining lines of calling page to be executed. How can I do that? What are you trying to do? What is the second page accomplishing? Will the first page stay open and the second page closes? You might be able to use WebRequest and WebResponse to do what you want. Hey bro, since you say that the called page has no web items, why dont you put the code in the called pa...

Paging Paging Paging
I am trying to do paging with my search results and it isnt working very well. Here is what I've tried and I feel I've tried everything, lol, please help me out. Sub DataGrid1_SortCommand(Sender as object, e as DataGridSortCOmmandEventArgs) DataGrid1.DataBind() End Sub Sub Datagrid1_PageIndexChanged(Sender as object, e as DataGridPageChangedEventArgs) Dim ds as dataSet DataGrid1.CurrentPageIndex = e.NewPageIndex if Session("Search") is nothing then 'Session probably has expired, get the datasource from database Datagrid1.DataSource = ...

Can you return .net web controls in a .net page from a web service?
I've got a problem with a project. I've got to compile an entire .net page and return it as part of a Web Service for display. The problem I have is that I need to add WebControls and functionality to the page so that not only the html content and images are displayed (which I've managed) but that textboxes, labels, buttons and datasets can be displayed also. Essentially I need to make a call from one site for a page, populate the page and return it in a state as though it resided on the same server/webspace as the calling page. Code is currently built as follows into a string: strOpenHT...

Custom Control: Calling a function on calling page FROM a custom control ? help !!
I have a custom control placed on many pages, to make things simple, basically what I want to do is Call a function on the calling page from the custom control ieAnyone have any ideas, simple solution would be good !!  This is the code I have myControl.ascx.vbPublic Partial Class myControl    Inherits System.Web.UI.UserControl    Protected Sub testButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles testButton.Click        'call save function on PersonalDetails.aspx.vb  &...

How could a master page control call a content page's function?
Dear all: For example: 1. In master page, place a button named "button1" . 2. Add three content page, ( add some code )3. Run application, in content page 1, click button1, show "this is page1",  in content page2, click button1, show "this is page2", and so on . Besides, these code should be in every content page instead of in master page, because actually action should be very complex, master can't predict everything. Is this possible?  Thanks!  Here's a great tutorial that may get you started: Creating a Common Toolbar in ASP.NET ...

Call public method on aspx page from a web control embedded in the page
I have a nut to crack. How do I call a public method on my aspx page from my web control, the web control is embedded into the page. The markup looks like this:-   1 <asp:UpdatePanel ID="UpdatePanel1" runat="server"> 2 <ContentTemplate> 3 <asp:Xml ID="Xml1" runat="server"></asp:Xml> 4 <uc2:tier3menu ID="menu1" runat="server" /> 5 </ContentTemplate> 6 </asp:UpdatePanel> I wrote an itemcommand thingie in the web control that needs to set the Documentsource a...

how to open a web page in form of a popup web page
Hello, I have two web pages, let say. Page A : With a Button on it. PAge B: with some info. Now I need to: When I click on the Button of page A.Page B must open in a pop up window.  I can do this but problem is this when I clicked on the button for first time it does not show me any thing and when I clicked on the button for second time then It shows me Page B .My code is as follow:  1 protected void Page_Load(object sender, EventArgs e) 2 { 3 if (!this.Page.IsPostBack) 4 { 5 6 } 7 else 8...

Single page with multiple user web controls VS Multiple page with specific user web controls
I have a question regarding single page with multiple user web controls vs multiple page with specific user web controls   Option 1: I am of the opinion to have multiple pages with specific user web controls and use an object to store all the information as the user navigates through the application. And finally at the end, read this object and save all the information to the backend database.   Option 2: My respected team member suggests that we have only one page, with all the specific user web controls and not use an object, instead show/hide the user web controls based ...

Call javascript function on containing page from user control
I have a toolbar in a user control.  I would like to wire the toolbar's onclick event to a javascript function on the page that contains the user control.  What's the easiest way to do this?  What controls are you using for your toolbar? You can add attribute to it link the appropriate function in javascript. Like this:    Button1.Attributes.Add("onClick", "JavascriptOnClick()"); Vincent CalaorSoftware EngineerPhilippines [Please click "mark as answer" If my reply helps you]Learn more from LearnITFromVince.B...

Can I have a web user control call a method on the page it's parent page?
I'm building an application where I'm using serveral different controls on a page and as one of them causes a postback I may want the page to make changes to other controls.  Is there a way to either fire and event off from the control that the page could then capture or simply have the control call a method of it's parent page? Obviously the Page object is there to work with, but I can't seem to figure out how to specifically cast it as a specific page.  For real man, In your user control you need to create a public event which is raised by the event of the...

Web resources about - Calling a function on a page from a control that is contained on the page - asp.net.web-forms

Self-contained breathing apparatus - Wikipedia, the free encyclopedia
A self contained breathing apparatus , or SCBA , sometimes referred to as a Compressed Air Breathing Apparatus (CABA) , air pack , or simply ...

Launch Plan For Mark Zuckerberg’s Political Advocacy Group Contained Several Errors
... a few issues, as Joe Green , the plan’s author and a roommate of Zuckerberg at Harvard University , confirmed to Politico that it contained ...

Haiti Earthship Project: Contained Sewage and Roof Insulation - YouTube
http://www.earthship.com/haiti Michael Reynolds describes the contained sewage treatment system and the roof insulation made out of garbage harvested ...

New self-contained communities will help reduce Dubai traffic
... Municipality’s masterplan for Desert Rose, a housing project for Emiratis that could become a blueprint for new ventures. DUBAI // Self-contained ...

Pinery Fire: Country Fire Service declares fire is contained as residents confront widespread devastation ...
THE deadly Pinery fire — which has been fully contained — destroyed or severely damaged 87 homes that are now considered uninhabitable, Premier ...

Pullenvale explosives home contained 'terror maps': reports
A Pullenvale property filled with explosives also allegedly contained maps with targets drawn in Sydney and Newcastle, according to media reports. ...

Pathways to a climate-contained, affluent 2050
A multi-country group of researchers, led by Jeffrey Sachs, has shown how it is still possible to contain global warming and enjoy strong economic ...

AIDS epidemic in PNG contained
Here is a good news story from Papua New Guinea: Australia's nearest neighbour has dodged the bullet of what the World Health Organisation feared ...

Mars rove Curiosity finds water contained in soil on the surface of the red planet
THE Mars rover Curiosity has had a major breakthrough: It has found water after scraping the dusty surface of the red planet.

World Bank positive 'Ebola can be contained'
World Bank boss Jim Yong Kim has said he is optimistic the Ebola outbreak will be contained within months.

Resources last updated: 11/29/2015 2:30:37 PM