views:

579

answers:

3

http://www.codeproject.com/KB/ajax/TunaUpdatepanel3.aspx

The link above contains class that extends the UpdatePanel usercontrol. How do I import it to a project and use it as a usercontrol as followed:

<uc:TunaUpdatePanel ... runat="server" />

UPDATE #1:

The proposed solution of moving code into an ascx file does not work.

Below are two files that I have created to test the solution where WebForm.aspx references 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"&gt;

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

The error message:

Parser Error Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.

Parser Error Message: 'WebApplication1.TunaUpdateUC' is not allowed here because it does not extend class 'System.Web.UI.UserControl'.

Source Error:

Line 1: <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="TunaUpdatePanelUC.ascx.cs" Inherits="WebApplication1.TunaUpdateUC" %>

+2  A: 

Assuming you've cut & paste this code into some file in your project (let's say you've got it in Controls\TunaUpdatePanel.ascx", you'd need to add this to your web.config like this:

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

Edit: Matt Ball's answer is also correct. Doing it his way, you'll add that line at the top of any page that uses that control. Doing it this way, you'll essentially be registering it for the entire app. Your choice which you prefer.

Sterno
+3  A: 

Save the source code to a file in your project. Then register it in the page you'd like to use it by adding a register directive at the top, like this:

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

where path is the path to the file that you saved the user control in.

Look here for similar information - just forget the part where you create the user control yourself.

Edit: Silly me, I assumed it was actually code for a user control, based on the title of your question, and not looking too closely at the link.

Well, it's not a user control, since (as the parse error says), it doesn't extend System.Web.UI.UserControl. It extends UpdatePanel, which means you have to use it just like you would an UpdatePanel (as the web site says). Usually a user control has a foo.ascx (markup) half and a foo.ascx.cs (or .vb, if you swing that way) codebehind half. However, this is just the codebehind.

Have a look here and here on how to use a server control extender. I don't have time right now to look through them in depth but I think they'll get you going in the right direction. The short version: it looks like you need to compile the C# code into an assembly.

Matt Ball
A: 

This control is intended to be used exactly as the original ASP.NET UpdatePanel, not as A usercontrol.

Paste the sourcecode into a *.cs file. Add the file to your current ASP.NET project. Change the namespace to your own namespace to aviod tinkering with the web.config.

Then it should popup in the toolbar in vs2005 drag that into the page as you would any other control.