hallizh
hallizh

Reputation: 150

Changing a ascx content from different code-behind

Say I have tvo pages, page1.ascx and page2.ascx. Both pages have code-behind(page1.ascx.cs and page2.ascx.cs respectively).

So page1 and page2 are rendered at the same time in the browser, side by side.

Now page1.ascx has a ListView and its code-behind has a method to populate it(PopulateListbox()). How can I call PopulateListbox() from the page2.ascx code-behind?

page1 p1 = new page1();
p1.PopulateListbox(); 

...does not work, and findController to find the ID of the listbox returns a null value.

Any guidance would be of great help, thanks.

Upvotes: 0

Views: 678

Answers (2)

Adrian Iftode
Adrian Iftode

Reputation: 15673

var p1 = this.Page.FindControl("page1Id") as page1;
if (p1 != null)
   p1.PopulateListbox();

You can do this in a different way though. Create an event on the first control for a specific action. In the parent page add an event handler and that event handler will contain the following call

p1.PopulateListbox();

Here a link for how to create your own events

Upvotes: 2

Dante
Dante

Reputation: 3891

Accessing one user control method directly from another user control doesn't sound like a great design.

What you could do is create a Delegate in your page2.ascx that gets called when your refresh action in the other user control needs to happen.

Your aspx page subscribes to that delegate and makes the call to the page1.ascx PopulateListBox method.

So, your page orchestrates the interaction between both user controls and they don't know about each other.

Upvotes: 0

Related Questions