c# - 如何使用这个用户控件?

标签 c# asp.net class user-controls

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"%>

最佳答案

将源代码保存到项目中的文件中。然后通过在顶部添加一个 register 指令在您要使用它的页面中注册它,如下所示:

<%@ Register TagPrefix="uc" TagName="TunaUpdatePanel" Src="~/[path]"

其中 path 是保存用户控件的文件的路径。

here对于类似的信息 - 只需忘记您自己创建用户控件的部分。

编辑:愚蠢的我,我假设它实际上是用户控件的代码,基于你的问题的标题,并没有仔细查看链接。

好吧,它不是用户控件,因为(正如解析错误所说),它没有扩展 System.Web.UI.UserControl。它扩展了 UpdatePanel,这意味着您必须像使用 UpdatePanel 一样使用它(如网站所述)。通常一个用户控件有一半的 foo.ascx(标记)和一半的 foo.ascx.cs(或 .vb,如果你这样做的话)代码隐藏。然而,这只是代码隐藏。

看看herehere关于如何使用服务器控件扩展程序。我现在没有时间深入研究它们,但我认为它们会让您朝着正确的方向前进。简短版本:看起来您需要将 C# 代码编译成程序集。

关于c# - 如何使用这个用户控件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1301717/

相关文章:

javascript - Jquery Ajax GET方法实现

javascript - 判断是否为asp :Placeholder is visible Jquery

asp.net - 展示我的开发技能的最佳网站是什么?

c# - ...由于其保护级别 c#/asp.net 而无法访问

javascript - 一个类的序列图,与操作符没有直接联系

c# - 他登录MVC后如何显示用户数据

c# - WCF方法未接收参数