The masterpagefile and mastertype is set on the page markup. Why is page.Master equal to null, i.e. nothing inside of a Shared WebMethod?
Here is my code below.
<WebMethod>_
Public Shared Function CreateNewTab(tabText As String) As String
Dim page = TryCast(HttpContext.Current.Handler, _Default)
If page IsNot Nothing Then
Dim master As SiteMaster = page.Master
If master IsNot Nothing Then
Dim cph As ContentPlaceHolder = master.FindControl("MainContent")
Dim tabContainer As AjaxControlToolkit.TabContainer = cph.FindControl("TabContainer1")
End If
End If
Return tabText
End Function
Because the page, both master and content page, does not actually exist in the ASP.NET AJAX Page Method. There is no instance of the content page, therefore no reference to the master page. That is what Shared
(static
in C#) means, it is void of an instance of the class, in your case the class is the content page class.
For more information read Why do ASP.NET AJAX page methods have to be static?.
UPDATE:
There is no way to get access to the server controls, because they are part of the instance of the page, you can however get access to the Session
object, by decorating your ASP.NET AJAX Page Method, like this:
Put a value into Session
:
<WebMethod(EnableSession := True)> _
Public Shared Sub StoreSessionValue(sessionValue As String)
HttpContext.Current.Session("TheSessionValue") = sessionValue
End Sub
Retrieve a value from Session
:
<WebMethod(EnableSession := True)> _
Public Shared Function GetSessionValue() As String
Return HttpContext.Current.Session("TheSessionValue").ToString()
End Sub