Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用这个用户控件?_C#_Asp.net_Class_User Controls - Fatal编程技术网

C# 如何使用这个用户控件?

C# 如何使用这个用户控件?,c#,asp.net,class,user-controls,C#,Asp.net,Class,User Controls,上面的链接包含扩展UpdatePanel用户控件的类。如何将其导入项目并将其用作usercontrol,如下所示: <uc:TunaUpdatePanel ... runat="server" /> 更新#1: 建议的将代码移动到ascx文件的解决方案不起作用 下面是我创建的两个文件,用于测试WebForm.aspx引用TunaUpdatePanelUC.ascx的解决方案 TunaUpdatePanelUC.ascx using System; using System.Co

上面的链接包含扩展UpdatePanel用户控件的类。如何将其导入项目并将其用作usercontrol,如下所示:

<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);
        }
    }
<%@ 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>
使用系统;
使用System.Collections.Generic;
使用系统文本;
使用System.Web.UI;
使用System.Text.RegularExpressions;
使用System.IO;
公共类TunaUpdatePanelUC:UpdatePanel
{
私有静态只读正则表达式Regex\u CLIENTSCRIPTS=新正则表达式(

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

<pages>
   <controls>
      <add src="~/Controls/TunaUpdatePanel.ascx" tagPrefix="uc" tagName="TunaUpdatePanel"/>
   </controls>
</pages>
<%@ Register TagPrefix="uc" TagName="TunaUpdatePanel" Src="~/[path]"


编辑:马特·鲍尔的答案也是正确的。按照他的方式,你将在使用该控件的任何页面顶部添加该行。按照这种方式,你将为整个应用程序注册该行。你可以选择你喜欢的代码。

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

<pages>
   <controls>
      <add src="~/Controls/TunaUpdatePanel.ascx" tagPrefix="uc" tagName="TunaUpdatePanel"/>
   </controls>
</pages>
<%@ Register TagPrefix="uc" TagName="TunaUpdatePanel" Src="~/[path]"


编辑:马特·鲍尔的答案也是正确的。按照他的方式,你将在使用该控件的任何页面顶部添加该行。按照这种方式,你将为整个应用程序注册它。你可以选择你喜欢的。将源代码保存到项目中的一个文件中。然后在你想使用它的页面中注册它。adding顶部有一个寄存器指令,如下所示:

<pages>
   <controls>
      <add src="~/Controls/TunaUpdatePanel.ascx" tagPrefix="uc" tagName="TunaUpdatePanel"/>
   </controls>
</pages>
<%@ Register TagPrefix="uc" TagName="TunaUpdatePanel" Src="~/[path]"

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

<pages>
   <controls>
      <add src="~/Controls/TunaUpdatePanel.ascx" tagPrefix="uc" tagName="TunaUpdatePanel"/>
   </controls>
</pages>
<%@ Register TagPrefix="uc" TagName="TunaUpdatePanel" Src="~/[path]"

此控件的使用方式与原始ASP.NET UpdatePanel完全相同,而不是用户控件

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


然后,它会在vs2005的工具栏中弹出,并像其他控件一样将其拖动到页面中。

此控件的用途与原始ASP.NET UpdatePanel完全相同,而不是作为usercontrol

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

然后它会在vs2005中的工具栏中弹出,并像其他控件一样将其拖动到页面中。

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

设置完ASCX文件(见下文)后,我跟随并将新控件拖到页面,添加到页面中(其中ProjName是项目名称,NameSpace是包含该控件的命名空间):

或者C版本:
文本框_Plus.ascx.cs

public class TextBox_Plus : System.Web.UI.WebControls.TextBox
{
    public string PlaceHolder {
        get { return Attributes["placeholder"]; }
        set { Attributes["placeholder"] = value; }
    }
}
我有一个类似的问题(我试图扩展文本框以具有HTML5占位符属性)

设置完ASCX文件(见下文)后,我跟随并将新控件拖到页面,添加到页面中(其中ProjName是项目名称,NameSpace是包含该控件的命名空间):

或者C版本:
文本框_Plus.ascx.cs

public class TextBox_Plus : System.Web.UI.WebControls.TextBox
{
    public string PlaceHolder {
        get { return Attributes["placeholder"]; }
        set { Attributes["placeholder"] = value; }
    }
}

嗯,在你的div中,你需要uc:TunaUpdatePanel,而不是uc:tunaThank Sterno。但是我仍然得到相同的错误。嗯,在你的div中,你需要uc:TunaUpdatePanel,而不是uc:tunaThank Sterno。但是我仍然得到相同的错误