Using await in ASP.NET webform pages inline code

If I have some inline code in my ASP.NET webforms page (in .NET 4.5), e.g.

   <% =ListItemWithPrice("Widget")%> 

and I want to make ListItemWithPrice async, is it possible to await it on the page? I know I can use

   <% =ListItemWithPrice("Widget").Result%> 

but that’ll just block while waiting for the Task to complete, which isn’t really any help (and might even be worse). Ideally, I’d like to use

   <% =await ListItemWithPrice("Widget")%> 

but that doesn’t seem to work.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

You can’t have inline await statements in a Page, but you can do it from code-behind using Page.RegisterAsyncTask. See for an example.

Solution 2

The short answer is no, await cannot be used in this way. This would work if the await keyword would generate an ajax request and the corresponding callback function to call for when the request is done.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply