Click here to Skip to main content
15,889,462 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everyone,
I been trying for hours to figure out how to update the Label text in an UpdateProgress control in each iteration of a FOR LOOP running on the server?
when the user click send email button , it go through loop to check if the user is inside this location and if its then add the user email to the email list.
I don't just want to show "sending or loading ..."; but rather: 2 out of 100 for example.
VB
If email <> "" Then
                   dcount += 1
<pre> Session("dcount") = dcount
<pre>Dim counterStatus As Label = CType(UpdateProgress1.FindControl("lblCounterStatus"), Label)
counterStatus.Text= Session("dcount") = dcount



the label never shows while it's searching

and here is my aspx
<asp:UpdatePanel ID="UpdatePanel2" runat="server">

                   <ContentTemplate>
                   <asp:Label ID="lblEmailStatus" runat="server"></asp:Label> <br />
                    <asp:Button ID="btnSend" runat="server" Style="text-align: center" Text="Send"
                   Width="101px" />


                                             <asp:Label ID="lblStatus2" runat="server" Text=""></asp:Label>
                                             <br />
                       <asp:Label ID="lblTimer" runat="server"></asp:Label>
                                              <br />

                   </ContentTemplate>
               </asp:UpdatePanel>
               <%-- <asp:Timer ID="Timer1" runat="server" OnTick="Timer1_tick" Interval="500" Enabled="false">
                       </asp:Timer>--%>
               <asp:Label ID="lblsending" Visible="false" runat="server" Text="Please do not close this page till the counter finish"></asp:Label>
               <asp:UpdateProgress ID="UpdateProgress1" runat="server" AssociatedUpdatePanelID="UpdatePanel2">
                   <ProgressTemplate>
                       <asp:Label ID="lblCounterStatus" runat="server" ></asp:Label>
                       <br />
                       <img alt="progress" src="images/anim0003-1_e0.gif"/>
              Sending...
                   </ProgressTemplate>
               </asp:UpdateProgress>
Posted
Updated 9-Oct-12 6:29am
v3

Try this

VB
Dim yourLabel As Label
yourLabel = DirectCast(UpdateProgress1.Controls(0).FindControl("lblCounterStatus"), Label)
yourLabel .Text = "your text"
 
Share this answer
 
You can use a static counter variable and use the label inside another updatepanel to achieve what you intend to do. I have added a dummy button to a second updatepanel whose click will be simulated via javascript in every 100 milliseconds in this example.

Try this

HTML:
XML
<div>
        <asp:UpdatePanel ID="UpdatePanelProcess" runat="server">
            <ContentTemplate>
                <asp:Button runat="server" Text="Process" ID="btnProcess" OnClientClick="Process();"
                    OnClick="btnProcess_Click" />
            </ContentTemplate>
        </asp:UpdatePanel>
        <asp:UpdatePanel ID="UpdatePanel2" runat="server">
            <ContentTemplate>
                <asp:Button runat="server" Text="DummyButton" ID="btnDummy" Style="display: none;"
                    OnClick="btnDummy_Click" />
                <asp:Label ID="lblCounterStatus" runat="server"  ></asp:Label>
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>

    <script type="text/javascript">


        function Process() {
            var btn = document.getElementById('<%=btnDummy.ClientID%>');
            btn.click();
            setTimeout(Process, 100);

        }
    </script>

Code Behind:
C#
protected void btnProcess_Click(object sender, EventArgs e)
        {
            Process();

        }

        static int counter = 1;

        void Process()
        {
            for (counter = 1; counter < 10; counter++)
            {
                Thread.Sleep(1000);
            }

        }

        protected void btnDummy_Click(object sender, EventArgs e)
        {
            lblCounterStatus.Text = counter.ToString();

        }


Hope this helps.
 
Share this answer
 
Comments
TheCoolCoder 10-Oct-12 1:59am    
Please keep in mind that the javascript function will continue executing and posting back to sever even after the counter has stopped working.Also the scope of the static variable is application wide so if there's a scenario involving multiple users this could create problems. I just wrote this as a quick solution. You certainly can find workarounds to these problems if you are intersted, then main part is partial updates to page with two update panels. If you have any doubts feel free to ask..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900