Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Script registration labyrinth – startup scripts and $find (asynchronous postback)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AsynchronousPostback.aspx.cs" Inherits="ScriptRegistrationLabyrinth.AsynchronousPostback" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager runat="server" ID="ScriptManager1">
</asp:ScriptManager>
<asp:UpdatePanel runat="server" ID="UpdatePanel1">
<ContentTemplate>
<asp:Timer runat="server" ID="BretonTimer" OnTick="BretonTimer_Tick" Interval="2000" />
<asp:Label runat="server" ID="Label1" /><br />
<asp:Button runat="server" ID="StopBretonButton" Text="Stop!" OnClick="StopBretonButton_Click" />
<asp:Button runat="server" ID="JustAnotherPostbackButton" Text="Just another postback" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="BretonTimer" />
<asp:AsyncPostBackTrigger ControlID="StopBretonButton" />
</Triggers>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ScriptRegistrationLabyrinth
{
public partial class AsynchronousPostback : System.Web.UI.Page
{
protected void BretonTimer_Tick(object sender, EventArgs e)
{
Label1.Text += "*";
}
protected void StopBretonButton_Click(object sender, EventArgs e)
{
string script = string.Format(
"(function() {{" +
"var fn = function() {{" +
"$find(\"{0}\")._stopTimer();" +
"Sys.Application.remove_load(fn);" +
"}};" +
"Sys.Application.add_load(fn);" +
"}})();", BretonTimer.ClientID);
ScriptManager.RegisterStartupScript(this, this.GetType(), "key1", script, true);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment