UPDATE: More recent information located at the end of this post.
I have implemented an Update Panel on a page that triggers a postback using the __doPostBack function.
When accessing the page directly at /path/page.aspx
, everything functions as expected.
However, if the page is accessed through a different route such as /otherpath/page
, the postback does not initiate.
Any ideas or suggestions for resolving this issue?
Below is the JavaScript code:
/// <reference name="MicrosoftAjax.js"/>
function Check() {
// Invokes the static page method.
PageMethods.GetLatestHeadlineTick(OnSucceeded, OnFailed);
}
function OnSucceeded(result, userContext, methodName) {
// Converting the results from the page method and the hidden value to integers for comparison.
var LatestTick = parseInt(result);
var LatestDisplayTick = parseInt($get('LatestDisplayTick').value);
// If the result from the page method is greater than the latest display tick, trigger a panel refresh.
if (LatestTick > LatestDisplayTick)
__doPostBack('UpdatePanel1', '');
// Otherwise, recheck in five seconds.
else
setTimeout("Check()", 5000);
}
// Placeholder function to handle failed calls to page method.
function OnFailed(error, userContext, methodName) { }
function pageLoad() {
// For initial load and partial postbacks, check for new articles every five seconds.
setTimeout("Check()", 5000);
}
And the Markup section:
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="True">
<Scripts>
<asp:ScriptReference Path="/resources/js/bus-times.js" />
</Scripts>
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" ClientIDMode="Static">
<ContentTemplate>
<asp:GridView ID="gvSchedule" runat="server" AutoGenerateColumns="False" Width="80%">
<AlternatingRowStyle CssClass="altrowstyle" />
<HeaderStyle CssClass="headerstyle" />
<RowStyle CssClass="rowstyle" />
<Columns>
<asp:BoundField DataField="RouteName" HeaderText="Route" />
<asp:BoundField DataField="TimeTillDeparture" HeaderText="Departs In" />
<asp:BoundField DataField="ScheduledDepartureTime" HeaderText="Est. Departure Time" />
</Columns>
<EmptyDataTemplate>
Data is currently unavailable.
</EmptyDataTemplate>
</asp:GridView>
<div class="updatedstyle">
Last updated:
<asp:Label ID="updated_time" runat="server" ></asp:Label></div>
<asp:HiddenField runat="server" ID="LatestDisplayTick" ClientIDMode="Static" />
<asp:HiddenField runat="server" ID="hf_stopID" ClientIDMode="Static" />
</ContentTemplate>
</asp:UpdatePanel>
Lastly, the AJAX Method in the code-behind:
<WebMethod()> _
Public Shared Function GetLatestHeadlineTick() As Long
Dim stopID As String
If HttpContext.Current.Request.QueryString("stop_id") <> Nothing Then
stopID = HttpContext.Current.Request.QueryString("stop_id")
Else
stopID = "678036"
End If
' Retrieve the cached DataTable.
Dim dt_added As DateTime = CType(BusScheduleService.GetBusDataDateAdded(stopID), DateTime)
' Return the timestamp of that bus data in ticks.
Return dt_added.Ticks
End Function
UPDATE:
A Fiddler screenshot displaying a successful version at the top and an error at the bottom has been added. The bottom one shows a 405 request error. It seems like the Ajax request is mistakenly being interpreted as a route when resolved, which leads to it not working. How can I overcome this? It appears that when Ajax calls a function, it appends a /functionName after the URL, resembling a route syntax...
Thus, when trying to call the GetLatestHeadLineTick via /path/page.aspx/GetLatestHeadLineTick, it works fine. However, with a route, it goes to /otherpath/page/GetLatestHeadLineTick, causing my site to treat it as a route rather than an AJAX request.
I also noticed that on the successful request, the content type is JSON, while on the failed request, it's interpreted as HTML.