UpdatePanel refresh on some code conditions

Last post 11-13-2009 4:46 AM by AlohaMora. 16 replies.

Sort Posts:

  • UpdatePanel refresh on some code conditions

    11-11-2009, 9:23 AM
    • Member
      17 point Member
    • AlohaMora
    • Member since 07-25-2006, 6:11 AM
    • Sharma
    • Posts 46

    Hi Friends,

    I am trying to create a very simple counter on a webpage with the click of a button:

        protected void Button1_Click(object sender, EventArgs e)
        {
            for (Int32 i = 0; i <= 100; i++)
            {
                Label1.Text = Convert.ToString(i);
                System.Threading.Thread.Sleep(100);
                <Update/refresh the UpdatePenal Here>
            }
        }


    I have a updatepanel which contains this Label and Button controls. I want after every increment the label should actually show the current value on the webpage.

    I tried to use linkbutton as trigger with update mode conditional. Even with code to fire the linkbutton.click in the above for loop the page is not refresing after each increment.

    Please help
     

    Rajiv Sharma
    Software Consultant, Singapore
    MCSD.net, MCTS, MCSE
    www.rajivsharma.com
  • Re: UpdatePanel refresh on some code conditions

    11-11-2009, 11:10 AM
    • All-Star
      25,837 point All-Star
    • qwe123kids
    • Member since 03-27-2008, 5:49 AM
    • Mumbai
    • Posts 4,388

    Hi,


    have Tried using Update() method in Update panle..

    protected void Button1_Click(object sender, EventArgs e)
        {
            for (Int32 i = 0; i <= 100; i++)
            {
                Label1.Text = Convert.ToString(i);
                System.Threading.Thread.Sleep(100);
               UpdatePanel_Id.Update();          

            }
        }

    Thanks
    Avinash Tiwari

    Remember to click “Mark as Answer” on the post, if it helps you.

    MY Blog

    Hacking Inside .net exe
  • Re: UpdatePanel refresh on some code conditions

    11-11-2009, 1:39 PM
    • Member
      17 point Member
    • AlohaMora
    • Member since 07-25-2006, 6:11 AM
    • Sharma
    • Posts 46

    i have tried update() for updatepanel but it is not working, any idea? 

    Rajiv Sharma
    Software Consultant, Singapore
    MCSD.net, MCTS, MCSE
    www.rajivsharma.com
  • Re: UpdatePanel refresh on some code conditions

    11-11-2009, 2:10 PM

    Are you expecting to see the textbox increment from 0 to 100, gradually?  If so, the code you have won't do it.  It is only after ALL of the code in the code behind has executed that the page is rendered, or partially rendered.  In your case, you are probably seeing 0, and then when you click the button, 100.

  • Re: UpdatePanel refresh on some code conditions

    11-11-2009, 2:55 PM
    • Member
      27 point Member
    • khaliqsyed
    • Member since 12-15-2006, 12:21 PM
    • Posts 16

    to update the onclick event you would have to add your label to your UpdatePanel_Id ContentContainer

    UpdatePanel_Id.ContentTemplateContainer.Controls.Add(Label1)
    


     

  • Re: UpdatePanel refresh on some code conditions

    11-11-2009, 3:01 PM
    • Participant
      1,201 point Participant
    • Mohammed Askar
    • Member since 09-01-2009, 8:23 AM
    • Sri Lanka
    • Posts 290

     set update panel UpdateMode = Conditional

    Please mark as answered, if this help you.
  • Re: UpdatePanel refresh on some code conditions

    11-11-2009, 9:58 PM

    How UpdatePanel Controls Are Refreshed

    The following list describes the property settings of the UpdatePanel control that determine when a panel's content is updated during partial-page rendering.

    • If the UpdateMode property is set to Always, the UpdatePanel control’s content is updated on every postback that originates from anywhere on the page. This includes asynchronous postbacks from controls that are inside other UpdatePanel controls, and postbacks from controls that are not inside UpdatePanel controls.

    • If the UpdateMode property is set to Conditional, the UpdatePanel control’s content is updated when one of the following is true:

      • When the postback is caused by a trigger for that UpdatePanel control.

      • When you explicitly call the UpdatePanel control's Update() method.

      • When the UpdatePanel control is nested inside another UpdatePanel control and the parent panel is updated.

      • When the ChildrenAsTriggers property is set to true and any child control of the UpdatePanel control causes a postback. Child controls of nested UpdatePanel controls do not cause an update to the outer UpdatePanel control unless they are explicitly defined as triggers for the parent panel.

    Chetan Sarode
    Software Engineer,
    Approva Systems Pvt Ltd,
    Pune, India.
  • Re: UpdatePanel refresh on some code conditions

    11-12-2009, 12:37 AM
    • All-Star
      25,837 point All-Star
    • qwe123kids
    • Member since 03-27-2008, 5:49 AM
    • Mumbai
    • Posts 4,388

    AlohaMora:

    i have tried update() for updatepanel but it is not working, any idea? 

     

    Any error???

    Thanks
    Avinash Tiwari

    Remember to click “Mark as Answer” on the post, if it helps you.

    MY Blog

    Hacking Inside .net exe
  • Re: UpdatePanel refresh on some code conditions

    11-12-2009, 2:46 AM
    • Member
      17 point Member
    • AlohaMora
    • Member since 07-25-2006, 6:11 AM
    • Sharma
    • Posts 46

    I am not getting any error. Just the label is not refreshing. Please see the aspx and aspx.cs codes below:

                <asp:ScriptManager ID="ScriptManager1" runat="server">
                </asp:ScriptManager> 
    <asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server"> <ContentTemplate> Count: <asp:Label ID="Label1" runat="server" Text=""></asp:Label><br /> <br /> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Start" /> </ContentTemplate> </asp:UpdatePanel>


     

        protected void Page_Load(object sender, EventArgs e)
        {
            Label1.Text = string.Empty;   
        }
    
        protected void Button1_Click(object sender, EventArgs e)
        {
            for (Int32 i = 0; i <= 100; i++)
            {
                Label1.Text = Convert.ToString(i);
                System.Threading.Thread.Sleep(100);
                UpdatePanel1.Update();
            }
        }


     

    Rajiv Sharma
    Software Consultant, Singapore
    MCSD.net, MCTS, MCSE
    www.rajivsharma.com
  • Re: UpdatePanel refresh on some code conditions

    11-12-2009, 3:13 AM
    • All-Star
      25,837 point All-Star
    • qwe123kids
    • Member since 03-27-2008, 5:49 AM
    • Mumbai
    • Posts 4,388

    Hi,

    Label1.Text = string.Empty;  

    Remove this Line rom onload ..

    Or

     keep in Condition of ispostback== false

     

     

    Thanks
    Avinash Tiwari

    Remember to click “Mark as Answer” on the post, if it helps you.

    MY Blog

    Hacking Inside .net exe
  • Re: UpdatePanel refresh on some code conditions

    11-12-2009, 3:40 AM
    • Member
      17 point Member
    • AlohaMora
    • Member since 07-25-2006, 6:11 AM
    • Sharma
    • Posts 46

    I removed this line from Page_load still not working.

        protected void Page_Load(object sender, EventArgs e)
        {
            //Label1.Text = string.Empty;   
        }
    
        protected void Button1_Click(object sender, EventArgs e)
        {
            for (Int32 i = 0; i <= 100; i++)
            {
                Label1.Text = Convert.ToString(i);
                System.Threading.Thread.Sleep(100);
                UpdatePanel1.Update();
            }
        }


     

    Rajiv Sharma
    Software Consultant, Singapore
    MCSD.net, MCTS, MCSE
    www.rajivsharma.com
  • Re: UpdatePanel refresh on some code conditions

    11-12-2009, 6:20 AM

    Read my previous note.

  • Re: UpdatePanel refresh on some code conditions

    11-12-2009, 6:47 AM
    • All-Star
      25,837 point All-Star
    • qwe123kids
    • Member since 03-27-2008, 5:49 AM
    • Mumbai
    • Posts 4,388

    MetalAsp.Net:

    Read my previous note.

    He is right..

    Thanks
    Avinash Tiwari

    Remember to click “Mark as Answer” on the post, if it helps you.

    MY Blog

    Hacking Inside .net exe
  • Re: UpdatePanel refresh on some code conditions

    11-12-2009, 7:22 AM
    • Member
      17 point Member
    • AlohaMora
    • Member since 07-25-2006, 6:11 AM
    • Sharma
    • Posts 46

    Thanks Guys for your reply.

    @ MetalAsp.Net

    I have read your previous note, Yes on te button click i only see 100.

    But I want the label to show 1 2 3 ......... 100

    i have tried the triggers also, puting the linkbutton as a trigger and firing the click even in the for loop which didn't work.

    any help?

    Rajiv Sharma
    Software Consultant, Singapore
    MCSD.net, MCTS, MCSE
    www.rajivsharma.com
  • Re: UpdatePanel refresh on some code conditions

    11-12-2009, 8:25 AM

    You might make use of a Timer that triggers an async postback every second, and increment the value of the TB by 1 in the Tick event.

    My preferred way of doing counter like this is using javascript, client-side, making use of the setInterval() function.  Don't know if it's the best way, or if it works in your situation...

Page 1 of 2 (17 items) 1 2 Next >