【问题标题】:How to use this usercontrol?如何使用这个用户控件?
【发布时间】:2023-03-21 06:28:01
【问题描述】:

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 行:

【问题讨论】:

  • 好吧,在你的 div 中,你需要 uc:TunaUpdatePanel,而不是 uc:Tuna
  • 谢谢斯特诺。但是我仍然遇到同样的错误

标签: c# asp.net class user-controls


【解决方案1】:

我遇到了类似的问题(我试图扩展 TextBox 以具有 HTML5 占位符属性)。

一旦我完成了我的 ASCX 文件设置(见下文),我跟随 these instructions 并将新控件拖到我添加的页面(其中 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; }
    }
}

【讨论】:

    【解决方案2】:

    此控件旨在完全用作原始 ASP.NET UpdatePanel,而不是作为用户控件。

    将源代码粘贴到 *.cs 文件中。将该文件添加到您当前的 ASP.NET 项目中。将命名空间更改为您自己的命名空间,以避免修改 web.config。

    然后它应该会在 vs2005 的工具栏中弹出,将它拖到页面中,就像您使用任何其他控件一样。

    【讨论】:

      【解决方案3】:

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

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

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

      查看here 以获取类似信息 - 只需忘记您自己创建用户控件的部分即可。

      编辑:我很傻,我认为它实际上是用户控件的代码,根据您的问题的标题,而不是仔细查看链接。

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

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

      【讨论】:

        【解决方案4】:

        假设您已将此代码剪切并粘贴到项目中的某个文件中(假设您已在 Controls\TunaUpdatePanel.ascx 中获得它),您需要将其添加到您的 web.config 中,如下所示:

        <pages>
           <controls>
              <add src="~/Controls/TunaUpdatePanel.ascx" tagPrefix="uc" tagName="TunaUpdatePanel"/>
           </controls>
        </pages>
        

        编辑:Matt Ball 的回答也是正确的。按照他的方式,您将在使用该控件的任何页面的顶部添加该行。这样做,您基本上将为整个应用程序注册它。您喜欢的选择。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-10-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多