Question

I have 4 User Controls inside a Page and the page is inside the Master Page. When a link clicked inside the User control that fires a javascript Ajax call to a web method which resides in the Page. Then that page call's the method of User control.

User Control

function statusImageClick(Key) {
    //ajax call to update the grid with the updated/inserted data.
    $.ajax({
        type: "POST",
        url: 'Page1.aspx/UpdateFRStatus',
        data: '{key : "' + Key + '"}',
        ....
}

public void UpdateFRStatus(int key)
{
    .....
}

Page1.aspx

[WebMethod]
public static void UpdateFRStatus(int key)
{            
    Page1 pageObj = new Page1();
    pageObj.UpdateFRStatusforAjax(key);
}

private void UpdateFRStatusforAjax(int key)
{ 
    ucFR.UpdateFRStatus(key);
}

Question 1: ucFR.UpdateFRSTatus(key) gives and error that ucFR is null.

Question 2: I am not sure what should be the correct approach for this behavior.

I hope have explained my problem.

Thanks in advance.

Was it helpful?

Solution

ASP.NET AJAX Page Methods are static and do not have access to the page's controls.

Even though you are instantiating a Page1 object, it does not have a reference to the controls that are in the page that is hosting the user controls and the page method itself.

Think of ASP.NET AJAX Page Methods as web services hosted by a page, but only hosted in the sense that the logic is written in the page itself. It is not directly connected to the page in context so I would recommend using it only to get data and then raising an event in the user control that the page can subscribe to and react to.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top