Saturday, March 24, 2012

conditional UpdatePanel doesnt work

I have the following simple code:

"UpdatePanel2" runat="server" UpdateMode="Conditional"> "Label3" runat="server" Text="Label"> "Button2" runat="server" Text="Button" onclick="Button2_Click">code behind:protected void Button2_Click(object sender, EventArgs e) { Label3.Text ="waiting..."; UpdatePanel2.Update(); Thread.Sleep(5000); Label3.Text ="done"; UpdatePanel2.Update(); }

But it doesn't update the panel until the end. i.e. you don't get to see the "waiting..." text (in fact, you don't see it at all) and then a 5 second pause, then"done". Why doesn't this work? I followed the video and couldn't find a mistake.

Thanks,
Yoni

Because the code in server.

When the system sleeping for 5 second,the runned client html data couldn't send toou client.so you cann't see "waiting...".

When the code in Button2_Click runned out,the html data just send to client,so you can see "done".

And you could change the code to client code as javascript code.

I hope you can understand the reason.Because I'm Chinese.I write English not well.


heyyonidebest,

the onclick event is a server side event , means the Button2_Click method will run at the server so everything happen inside the method you won't see it

the only way to see the situation you are describing is by using client side code


EnigmaOX:

heyyonidebest,

the onclick event is a server side event , means the Button2_Click method will run at the server so everything happen inside the method you won't see it

the only way to see the situation you are describing is by using client side code

I see. I can't convert this to JS because of other reasons. Is there a way to bypass this? Perhaps instead of trying to update the update pannel, I can call another procedure that does it somehow, via JS or else server side script. Is there such a way?

Thanks,
Yoni


Yep , you can use the trigger inside the update panel , plus a timer control

I'm not sure what are u trying to do but you can use the button event to change the text

and to start the timer and using the timer tick event you can fire another method after couple of seconds.


EnigmaOX:

Yep , you can use the trigger inside the update panel , plus a timer control

I'm not sure what are u trying to do but you can use the button event to change the text

and to start the timer and using the timer tick event you can fire another method after couple of seconds.

I see. I tried to create a hidden field object and write the following ValueChanged event:

protectedvoid HiddenField1_ValueChanged(object sender,EventArgs e)
{
UpdatePanel2.Update();
}

I then tried to call the event like this:

protectedvoid Button2_Click(object sender,EventArgs e)
{
Label3.Text ="waiting...";
HiddenField1.ValueChanged +=newEventHandler(this.HiddenField1_ValueChanged);
Thread.Sleep(5000);
Label3.Text ="done";
}

but still it did not work. I set the trigger to:

<asp:AsyncPostBackTriggerControlID="HiddenField1"EventName="ValueChanged"/>

yet still nothing. What am I missing? I don't need a timer because the update isn't based on time. i.e. I want to decide when to update the update panel.


heyyonidebest ,

well you still doing the same mistake , you will change the text of the label twice in the server so you won't see the waiting message

the idea is to change the text once in every event , for example Label.Text = "waiting..."; in the button2 click event and then start the timer control with 5 seconds tick

in the tick event change the text of the label again like Label.Text = "done...";

<asp:Timer ID="Timer1" runat="server" OnTick="timer1_Tick" Interval="5000" >
</asp:Timer>

privatevoid timer1_Tick(object sender, System.EventArgs e)
{
Label1.Text = "done...";
Timer1.Enabled = false; // to stop the timer

}

and use the tick event also to update panel

<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
 
Read more here : http://asp.net/AJAX/Documentation/Live/tutorials/TimerControlWithUpdatePanelsTutorial.aspx

Thanks,EnigmaOX.

But my code it more complicated that this small example. I have a few procedures that do stuff, and in the mean while I want to let the user know what the server is doing (plus so that I can debug). Thus, I can't see how a timer can help me. I can say one thing for sure - I need to let the user know what I doing - and during that time the server will be continuing it's work. Any other idea's how to refresh an update panel during while the server is working?

Yoni


sure , AjaxWink

you can do everything in ajax: webrequests , wcf services , registering postback scripts ect..


Well, how? the example I gave doen't work. can it be done w/o a timer?

Yoni


Take a look at doing it this way:http://encosia.com/2007/10/03/easy-incremental-status-updates-for-long-requests/

That's the sort of methodology you're going to need to consider. You can't provide incremental updates during a server call. Like a regular postback, a partial postback only outputs HTML once per round trip.


Better way you can use UpdateProgress Control

http://www.asp.net/ajax/documentation/live/overview/UpdateProgressOverview.aspx

No comments:

Post a Comment