How to set the @Page Async value without an .aspx file?



I have a class file in my App_Code folder called "clsBasePage.vb" that looks
like this:

Public Class clsBasePage
Inherits System.Web.UI.Page
End Class

It's a "page" in that it inherits from, but has no .aspx file. It's
just a class file.

In creating a new page for my website, say mynewpage.aspx, I remove the
IDE's automatic header & form html so all it contains is this:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="mynewpage.aspx.vb"
Inherits="mynewpage" %>

And I make mynewpage.aspx.vb inherit from clsBasePage, like this:

Partial Class mynewpage
Inherits clsBasePage
End Class

I've recently added this method to clsBasePage

Public Sub GetTheTextFromTheOtherWebsite()

Me.objWebClient = New System.Net.WebClient

With objWebClient
.Headers.Item(Net.HttpRequestHeader.Referer) = "localhost"
.Headers.Item(Net.HttpRequestHeader.UserAgent) = "my web app v1.0"
.BaseAddress = ""
.DownloadStringAsync(New System.Uri("cgi/server/textmaker.exe",
End With

End Sub

When I call GetTheTextFromTheOtherWebsite from mynewpage, the
..DownloadStringAsync() method of the WebClient object is throwing this

"Asynchronous operations are not allowed in this context. Page starting an
asynchronous operation has to have the Async attribute set to true and an
asynchronous operation can only be started on a page prior to
PreRenderComplete event."

I believe the typical "fix" for this would be to add the Async="true"
attribute to the @Page directive so that the page can perform an Async task.
Adding this value to mynewpage doesn't change anything, I think I need to
add it to clsBasePage instead. What's got me stumped here is that
clsBasePage doesn't have an .aspx file or a @Page directive at all, so I'm
not sure how to set that Async value to true on my base page class.


Have you tried adding the Async="true" attribute as instructed by the
exception message?

<%@ Page Language="VB" Async="true" AutoEventWireup="false"
Inherits="mynewpage" %>


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question