Programmatically Update The UpdatePanel Using ASP
Programmatically Update The UpdatePanel Using ASP
NET
Im going to describe how to update the Asp.net Ajax Update panel control programmatically.
Im using visual studio 2008 with this sample code
Introduction
Sometime we need to update the update panel from another update panel or within the
update panel using programmatically C# code behind. Here I will show you how to
update the update panel from another panel. By clicking the button from one update
panel, which should update the date and time in another update panel.
So we need to have script manager control and update panels as shown below.
Text=""></asp:Label>
</td>
<td>
<asp:Label ID="lblPrintTime" runat="server"
</td>
</tr>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="UpdPnlBtn" runat="server">
<ContentTemplate>
<tr>
<td style="width:100px;height:30px" colspan="2"
align="center">
<asp:Button ID="btnGetTime" runat="server"
Text="GetTime" OnClick="btnGetTime_Click" />
</td>
</tr>
</ContentTemplate>
</asp:UpdatePanel>
</table>
</div>
</form>
</body>
</html>
The ScriptManager will handle the all AJAX related calls and update panel. As shown
above add the ScriptManager and two update panels, the UpdPnlTime contain the label
and UpdPnlBtn contain the btnGetTime button.
We need to set the update panel attribute UpdateMode as Conditional because no need
to update all the postbacks and ChildrenAsTriggers as false to avoid updates.In the
btnGetTime button click event will get current date and time and assign into the
lblPrintTime label as shown below.
Update(); method from UpdatePanel will used to update UpdPnlTime panel after assign
the value into label.
Now if you click the GetTime button, the current date and time details are assigned to
the label and Update() method will refresh the UpdPnlTime update panel.
Conclusion
When UpdatePanel controls are not nested you can update each panel
independently by setting the UpdateMode property to Conditional. (The default
value of theUpdateMode property is Always. This causes the panel to refresh in
response to any asynchronous postback.)
http://www.asp.net/ajax/documentation/live/tutorials/CreatingPageUpdatePanel.aspx