locked
get return value in button click RRS feed

  • Question

  • User-513628628 posted

    Hi all,

    I have code :

    protected void Button1_Click(object sender, EventArgs e)
     {
    Response.write (....) } public async Task<string> CreateOrder() { string result = GetResultString(); return Task.FromResult(result); }

    How can i get value from createorder function and set it "button click" . Mean that : Respose.write result of "create order".
    I make :
    string x =CreateOrder.

    But i feel code run quite slowly on browser.
    Anyone help me ?
    Thank you so much !

    Sunday, May 27, 2018 2:03 AM

Answers

  • User-369506445 posted

    hi

    you can get the async return value with Result keyword below like :

    CreateOrder().Result

    please try below code :

    protected void button1_Click(object sender, EventArgs e)
            {
    
                Response.Write(CreateOrder().Result);
            }
    
            public async Task<string> CreateOrder()
            {
                return GetResultString();
                
            }
    
            public string GetResultString()
            {
                Thread.Sleep(5000);
                return "test";
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, May 27, 2018 5:13 AM
  • User36583972 posted


    Hi pamyral_279,

    How can i get value from createorder function and set it "button click" . Mean that : Respose.write result of "create order".
    I make :
    string x =CreateOrder.

    But i feel code run quite slowly on browser.
    Anyone help me ?
    Thank you so much !

    Firstly, you need to make ensure that the Page is marked <%@ Page Async="true" %>.

    Then, try the following code.

            protected async void Button1_Click(object sender, EventArgs e)
            {
                string result = await  CreateOrder();
                Response.Write(result);
            }
    
            public async Task<string> CreateOrder()
            {
                string result = "GetResultString";
                return await Task.FromResult(result);
            }
    

    Using Asynchronous Methods in ASP.NET 4.5
    https://docs.microsoft.com/en-us/aspnet/web-forms/overview/performance-and-caching/using-asynchronous-methods-in-aspnet-45

    Best Regards,

    Yong Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 28, 2018 7:20 AM

All replies

  • User-369506445 posted

    hi

    you can get the async return value with Result keyword below like :

    CreateOrder().Result

    please try below code :

    protected void button1_Click(object sender, EventArgs e)
            {
    
                Response.Write(CreateOrder().Result);
            }
    
            public async Task<string> CreateOrder()
            {
                return GetResultString();
                
            }
    
            public string GetResultString()
            {
                Thread.Sleep(5000);
                return "test";
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, May 27, 2018 5:13 AM
  • User36583972 posted


    Hi pamyral_279,

    How can i get value from createorder function and set it "button click" . Mean that : Respose.write result of "create order".
    I make :
    string x =CreateOrder.

    But i feel code run quite slowly on browser.
    Anyone help me ?
    Thank you so much !

    Firstly, you need to make ensure that the Page is marked <%@ Page Async="true" %>.

    Then, try the following code.

            protected async void Button1_Click(object sender, EventArgs e)
            {
                string result = await  CreateOrder();
                Response.Write(result);
            }
    
            public async Task<string> CreateOrder()
            {
                string result = "GetResultString";
                return await Task.FromResult(result);
            }
    

    Using Asynchronous Methods in ASP.NET 4.5
    https://docs.microsoft.com/en-us/aspnet/web-forms/overview/performance-and-caching/using-asynchronous-methods-in-aspnet-45

    Best Regards,

    Yong Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 28, 2018 7:20 AM