简体   繁体   中英

How to manage multi threading in asp.net to fill the labels?

I have a button and two labels in my aspx page , i would like to display text in label and after few seconds i would like to fill second label with different text on button click My code is source file

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
<asp:Label ID="lblFirst" runat="server" Text=""></asp:Label> 
 <asp:Label ID="lblSecond" runat="server" Text=""></asp:Label>
<asp:Button ID="btnFirst" runat="server" Text="First" 
            onclick="btnFirst_Click" />
 </ContentTemplate>
        </asp:UpdatePanel>

and code file

    protected void btnFirst_Click(object sender, EventArgs e)
            {
                first();
                second();
            }
            private void first()
            {
                I am Calling a method form my class file which returns a string , and assigning to label first
//class1 obj=new class1();
//string result=obj.first()
           // lblFirst.Text =result;
            }
            private void second()
            {
                   I am Calling a method form my class file which returns a string , and assigning to label Second
//class2 obj=new class2();
//string result1=obj.Second()             
                lblSecond.Text = result1;
            }

I am getting two responses, i would like to display which i got first without waiting for second response and after getting second response should display immediately without loosing first response, pls give me any suggestion urgent , Is there any other procedure to get such a output

Thanks hemanth

You can't make a delay like that in the server code. The page is not sent to the browser until the server code has rendered it, and that happens after the control events. The code will just wait for seven seconds, then render the page and send it to the browser.

You have to use client side code to get the experience that you are after. The server can't push changes to the web page after it has been sent to the browser.

protected void btnFirst_Click(object sender, EventArgs e) {
  string code =
    "window.setTimeout(function(){document.getElementById('" + lblFirst.ClientID + "').innerHTML='first filled'},1000);"+
    "window.setTimeout(function(){document.getElementById('" + lblSecond.ClientId + "').innerHTML='Second filled'},7000);";
  Page.ClientScript.RegisterStartupScript(this.GetType(), "messages", code, true);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM