http://www.codeproject.com/KB/ajax/TunaUpdatepanel3.aspx
Ссылка выше содержит класс, который расширяет пользовательский контроль UpdatePanel. Как мне импортировать его в проект и использовать его как пользовательский контроль следующим образом:
<uc:TunaUpdatePanel ... runat="server" />
ОБНОВЛЕНИЕ № 1:
Предлагаемое решение перемещения кода в файл ascx не работает.
Ниже приведены два файла, которые я создал для тестирования решения, в котором WebForm.aspx ссылается на 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>
Сообщение об ошибке:
Ошибка парсера
Описание: во время анализа ресурса, необходимого для обслуживания этого запроса, произошла ошибка. Пожалуйста, просмотрите следующую конкретную информацию об ошибке разбора и измените исходный файл соответствующим образом.
Сообщение об ошибке анализатора: «WebApplication1.TunaUpdateUC» здесь не разрешено, поскольку оно не расширяет класс «System.Web.UI.UserControl».
Ошибка источника:
Строка 1: <% @ Control Language = "C #" AutoEventWireup = "true" CodeBehind = "TunaUpdatePanelUC.ascx.cs" Inherits = "WebApplication1.TunaUpdateUC"%>