Irshad
Irshad

Reputation: 303

How to call user control's(.ascx) method in .aspx page

I have created user control, in that user control i have one method and I want to call this method in .aspx. I have registered this user control in aspx

For example: Below is method in user control.

public void SetGridData()
{
}

I want to call above method in .aspx.cs file. How can we call this method?

Upvotes: 0

Views: 10413

Answers (2)

matt
matt

Reputation: 9401

If this is purely an example, then you can call methods in codefiles with the following syntax:

<%= SetGridData(); %>

However, just be aware of the notes I put in the comments above.

Upvotes: 1

David
David

Reputation: 218847

Somewhere in the ASPX page's code you should have a reference to the user control object. For example, if the user control is called MyUserControl then somewhere at the class level for the page (possibly in a separate partial class designer file) should be:

protected MyUserControl myUserControl1;

or something similar to that. That's the instance of the user control for the page's class. The page life cycle should instantiate it by the time Page_Load is reached, so from then on you can use that object:

myUserControl1.SetGridData();

Upvotes: 1

Related Questions