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.

<li>
   <% =ListItemWithPrice("Widget")%> 
</li>

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

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

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

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

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 http://www.asp.net/web-forms/tutorials/aspnet-45/using-asynchronous-methods-in-aspnet-45 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 stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply