http://www.codeproject.com/KB/ajax/TunaUpdatepanel3.aspx
The link above contains class that extends the UpdatePanel usercontrol. How do I import it to a project and use it as a usercontrol as followed:
<uc:TunaUpdatePanel ... runat="server" />
UPDATE #1:
The proposed solution of moving code into an ascx file does not work.
Below are two files that I have created to test the solution where WebForm.aspx references TunaUpdatePanelUC.ascx.
TunaUpdatePanelUC.ascx
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Text.RegularExpressions;
using System.IO;
public class TunaUpdatePanelUC : UpdatePanel
{
private static readonly Regex REGEX_CLIENTSCRIPTS = new Regex(
"<script\\s((?<aname>[-\\w]+)=[\"'](?<avalue>.*?)[\"']\\s?)*\\s*>(?<script>.*?)</script>",
RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.Compiled |
RegexOptions.ExplicitCapture);
private bool m_RegisterInlineClientScripts = true;
/// <summary>
/// If the updatepanel shall parse and append inline scripts, default true
/// </summary>
public bool RegisterInlineClientScripts
{
get
{
return this.m_RegisterInlineClientScripts;
}
set
{
this.m_RegisterInlineClientScripts = value;
}
}
protected virtual string AppendInlineClientScripts(string htmlsource)
{
if (this.ContentTemplate != null && htmlsource.IndexOf(
"<script", StringComparison.CurrentCultureIgnoreCase) > -1)
{
MatchCollection matches = REGEX_CLIENTSCRIPTS.Matches(htmlsource);
if (matches.Count > 0)
{
for (int i = 0; i < matches.Count; i++)
{
string script = matches[i].Groups["script"].Value;
string scriptID = script.GetHashCode().ToString();
string scriptSrc = "";
CaptureCollection aname = matches[i].Groups["aname"].Captures;
CaptureCollection avalue = matches[i].Groups["avalue"].Captures;
for (int u = 0; u < aname.Count; u++)
{
if (aname[u].Value.IndexOf("src",
StringComparison.CurrentCultureIgnoreCase) == 0)
{
scriptSrc = avalue[u].Value;
break;
}
}
if (scriptSrc.Length > 0)
{
ScriptManager.RegisterClientScriptInclude(this,
this.GetType(), scriptID, scriptSrc);
}
else
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(),
scriptID, script, true);
}
htmlsource = htmlsource.Replace(matches[i].Value, "");
}
}
}
return htmlsource;
}
protected override void RenderChildren(HtmlTextWriter writer)
{
ScriptManager sm = ScriptManager.GetCurrent(Page);
if (this.RegisterInlineClientScripts && sm != null && sm.IsInAsyncPostBack)
{
using (HtmlTextWriter htmlwriter = new HtmlTextWriter(new StringWriter()))
{
base.RenderChildren(htmlwriter);
string html;
int outputSize;
//Get the actual rendering and size
html = htmlwriter.InnerWriter.ToString();
outputSize = html.Length;
//Append inlinescripts and fetch the new markup and size
html = this.AppendInlineClientScripts(html);
outputSize -= html.Length;
//Replace ContentSize if there are any gains
if (outputSize > 0)
{
html = this.SetOutputContentSize(html, outputSize);
}
writer.Write(html);
}
}
else
{
base.RenderChildren(writer);
}
}
private string SetOutputContentSize(string html, int difference)
{
string[] split = html.Split('|');
int size = int.Parse(split[0]);
split[0] = (size - difference).ToString();
return string.Join("|", split);
}
}
WebForm.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm.aspx.cs" Inherits="WebApplication1.WebForm" %>
<%@ Register TagPrefix="uc" TagName="TunaUpdatePanel" Src="~/TunaUpdatePanelUC.ascx" %>
<!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>
<uc:TunaUpdatePanel ID="Test1" runat="server" />
</div>
</form>
</body>
</html>
The error message:
Parser Error Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.
Parser Error Message: 'WebApplication1.TunaUpdateUC' is not allowed here because it does not extend class 'System.Web.UI.UserControl'.
Source Error:
Line 1: <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="TunaUpdatePanelUC.ascx.cs" Inherits="WebApplication1.TunaUpdateUC" %>