AJAX


UpdatePanel en ASP.NET, attention au piège

2010-05-06 2 Min. lecture .NET ASP.NET C#

En ASP.NET, l’UpdatePanel permet d’intéragir avec le serveur sans avoir à recharcher la page (mode asynchrone).

Le code suivant affiche la date courante dans le label lors du click sur le bouton sans avoir à recharger la page :

UpdatePanel.aspx

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:scriptmanager id="ScriptManager1" runat="server">
</asp:scriptmanager>
        <asp:updatepanel id="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="bt1" runat="server" Text="Button"
onclick="bt1_Click" />
<asp:Label ID="lbl1" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
</asp:updatepanel>
    </div>
    </form>
</body>
</html>

UpdatePanel.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class UpdatePanel : System.Web.UI.Page
{
    protected void bt1_Click(object sender, EventArgs e)
    {
        lbl1.Text = DateTime.Now.ToString();
    }
}

Ce qui donne le résultat suivant lors du click sur le bouton, le tout sans rechargement de page :

continuer la lecture