[英]How to use this usercontrol?
http://www.codeproject.com/KB/ajax/TunaUpdatepanel3.aspx
上面的链接包含扩展UpdatePanel用户控件的类。 如何将其导入到项目中,并按如下所示将其用作用户控件:
<uc:TunaUpdatePanel ... runat="server" />
更新#1:
提出的将代码移入ascx文件的解决方案不起作用。
下面是我创建的两个文件,用于测试WebForm.aspx引用TunaUpdatePanelUC.ascx的解决方案。
金枪鱼更新面板UC.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行:<%@控制语言=“ C#” AutoEventWireup =“ true” CodeBehind =“ TunaUpdatePanelUC.ascx.cs” Inherits =“ WebApplication1.TunaUpdateUC”%>
将源代码保存到项目中的文件中。 然后通过在顶部添加一个register指令在您想要使用它的页面中注册它,如下所示:
<%@ Register TagPrefix="uc" TagName="TunaUpdatePanel" Src="~/[path]"
其中path
是您在其中保存用户控件的文件的路径。
在此处查找类似信息-只需忘记自己创建用户控件的部分即可。
编辑 :愚蠢的我,我认为它实际上是一个用户控件的代码,基于您的问题的标题,而不是过于仔细地查看链接。
嗯,它不是用户控件,因为(如解析错误所示),它不扩展System.Web.UI.UserControl
。 它扩展了UpdatePanel
,这意味着您必须像使用UpdatePanel一样使用它(如网站上所述)。 通常,用户控件的一半后面是foo.ascx(标记)一半,而foo.ascx.cs(如果是这样,则是.vb)。 但是,这只是背后的代码。
在这里和这里看看如何使用服务器控件扩展器。 我现在没有时间深入研究它们,但我认为它们会让您朝着正确的方向前进。 简短的版本:看起来您需要将C#代码编译为程序集。
假设您已将此代码剪切并粘贴到项目中的某个文件中(假设您已将其保存在Controls \\ TunaUpdatePanel.ascx中,则需要将其添加到web.config中,如下所示:
<pages>
<controls>
<add src="~/Controls/TunaUpdatePanel.ascx" tagPrefix="uc" tagName="TunaUpdatePanel"/>
</controls>
</pages>
编辑:马特·鲍尔的答案也是正确的。 以他的方式进行操作,您将在使用该控件的任何页面的顶部添加该行。 这样,您基本上就可以为整个应用程序进行注册。 您喜欢的选择。
此控件旨在完全用作原始ASP.NET UpdatePanel,而不用作A用户控件。
将源代码粘贴到* .cs文件中。 将文件添加到当前的ASP.NET项目。 将名称空间更改为您自己的名称空间,以免修改web.config。
然后,它将像在其他任何控件中一样,在vs2005中的工具栏中弹出该对话框并将其拖动到页面中。
我有一个类似的问题(我试图将TextBox扩展为具有HTML5占位符属性)。
设置好ASCX文件后(请参见下文),我按照以下说明进行操作 ,并将新控件拖到添加了该控件的页面上(其中ProjName是项目的名称,NameSpace是包含该控件的名称空间):
<%@ Register assembly="ProjName" namespace="NameSpace" tagprefix="cc1" %>
<cc1:TextBox_Plus ID="TextBox_Plus1" runat="server" />
TextBox_Plus.ascx.vb公共类TextBox_Plus继承System.Web.UI.WebControls.TextBox
Public Property PlaceHolder As String
Get
Return Attributes("placeholder")
End Get
Set(value As String)
Attributes("placeholder") = value
End Set
End Property
End Class
或C#版本:
TextBox_Plus.ascx.cs
public class TextBox_Plus : System.Web.UI.WebControls.TextBox
{
public string PlaceHolder {
get { return Attributes["placeholder"]; }
set { Attributes["placeholder"] = value; }
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.