开发者

AjaxControlToolkit is undefined - Error when bringing an extender up to date

I am looking for some way to warn the user if a form is 'dirty' and they try and navigate away from it.

This project seemed to do exactly what I needed.

However, it was written back in 2007, and when I tried to use it "as-is" in my .NET 4.0 app with the latest version of the Ajax Control Toolkit it didn't work.

So, I downloaded the source (available from the CodeProject article) and tried to bring it up-to-date. But this is the first time I've attempted such a task and I'm a bit lost.

The error I'm now getting is as follows:

Microsoft JScript runtime error: 'AjaxControlToolkit' is undefined

The line in the DirtyPanelExtenderBehaviour.js which gives the error is: DirtyPanelExtender.DirtyPanelExtenderBehavior.registerClass('DirtyPanelExtender.DirtyPanelExtenderBehavior', AjaxControlToolkit.BehaviorBase);

Here's what I've done so far:

1. Downloaded the source from the Codeproject article

2. Opened in Visual Studio 2010 and completed the upgrade wizard

3. Deleted references to AjaxControlToolkit.dll and replaced with a new reference to the latest version of the toolkit 4. Amended references to ScriptManager with ToolkitScriptManager (Not sure if this was needed)

5. Amended the sample website to use a ToolkitScriptManager on the page in place of a ScriptManager

Sooo, any ideas what I've done wrong? Or what I'm missing? So that I can recompile the DirtyPanelExtender.dll and use it in my .NET 4.0 project?

The full code is below that I am using. The original author wrote this with exception of my amendments as detailed above - I'm not taking credit!

DirtyPanelExtender.cs:

using System;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections.Generic;
using System.Text;
using AjaxControlToolkit;

[assembly: System.Web.UI.WebResource("DirtyPanelExtender.DirtyPanelExtenderBehavior.js", "text/javascript")]

namespace DirtyPanelExtender
{
    [Designer(typeof(DirtyPanelExtenderDesigner))]
    [ClientScriptResource("DirtyPanelExtender.DirtyPanelExtenderBehavior", "DirtyPanelExtender.DirtyPanelExtenderBehavior.js")]
    [TargetControlType(typeof(Panel))]
    [TargetControlType(typeof(UpdatePanel))]
    public class DirtyPanelExtender : ExtenderControlBase
    {
        [ExtenderControlProperty]
        [DefaultValue("Data has not been saved.")]
        public string OnLeaveMessage
        {
            get
            {
                return GetPropertyValue("OnLeaveMessage", "");
            }
            set
            {
                SetPropertyValue("OnLeaveMessage", value);
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            string values_id = string.Format("{0}_Values", TargetControl.ClientID);
            string values = (Page.IsPostBack ? Page.Request.Form[values_id] : String.Join(",", GetValuesArray()));
            ToolkitScriptManager.RegisterHiddenField(this, values_id, values);
            base.OnPreRender(e);
        }

        private string[] GetValuesArray()
        {
            return GetValuesArray(TargetControl.Controls);
        }

        private string[] GetValuesArray(ControlCollection coll)
        {
            List<string> values = new List<string>();
            foreach (Control control in coll)
            {
                if (control is RadioButtonList)
                {
                    for (int i = 0; i < ((RadioButtonList)control).Items.Count; i++)
                    {
                        values.Add(string.Format("{0}_{1}:{2}", control.ClientID, i, ((RadioButtonList)control).Items[i].Selected.ToString().ToLower()));
                    }
                }
                else if (control is ListControl)
                {
                    StringBuilder data = new StringBuilder();
                    String开发者_运维问答Builder selection = new StringBuilder();
                    foreach (ListItem item in ((ListControl) control).Items)
                    {
                        data.AppendLine(item.Text);
                        selection.AppendLine(item.Selected.ToString().ToLower());
                    }
                    values.Add(string.Format("{0}:data:{1}", control.ClientID, Uri.EscapeDataString(data.ToString())));
                    values.Add(string.Format("{0}:selection:{1}", control.ClientID, Uri.EscapeDataString(selection.ToString())));
                }
                else if (control is IEditableTextControl)
                {
                    values.Add(string.Format("{0}:{1}", control.ClientID, Uri.EscapeDataString(((IEditableTextControl)control).Text)));
                }
                else if (control is ICheckBoxControl)
                {
                    values.Add(string.Format("{0}:{1}", control.ClientID, ((ICheckBoxControl)control).Checked.ToString().ToLower()));
                }

                values.AddRange(GetValuesArray(control.Controls));
            }
            return values.ToArray();
        }

        public void ResetDirtyFlag()
        {
            ToolkitScriptManager.RegisterClientScriptBlock(TargetControl, TargetControl.GetType(),
                string.Format("{0}_Values_Update", TargetControl.ClientID), string.Format("document.getElementById('{0}').value = '{1}';",
                    string.Format("{0}_Values", TargetControl.ClientID), String.Join(",", GetValuesArray())), true);
        }
    }
}

DirtyPanelExtenderBehaviour.js:

Type.registerNamespace('DirtyPanelExtender');

DirtyPanelExtender.DirtyPanelExtenderBehavior = function(element) {

    DirtyPanelExtender.DirtyPanelExtenderBehavior.initializeBase(this, [element]);
    this._OnLeaveMessageValue = null;
}

DirtyPanelExtender.DirtyPanelExtenderBehavior.prototype = {

    isDirty : function() {
        var values_control = document.getElementById(this.get_element().id + "_Values");
        var values = values_control["value"].split(",");
        for (i in values) {
            var namevalue = values[i];
            var namevaluepair = namevalue.split(":");
            var name = namevaluepair[0];
            var value = (namevaluepair.length > 1 ? namevaluepair[1] : "");
            var control = document.getElementById(name);
            if (control == null) continue;
            // alert(control.id + " -> " + control.type);
            if (control.type == 'checkbox' || control.type == 'radio') {
                var boolvalue = (value == "true" ? true : false);
                if(control.checked != boolvalue) {
                    // alert("checkbox changed: " + control.checked + " vs. " + boolvalue);
                    return true;
                }
            } else if (control.type == 'select-one' || control.type == 'select-multiple') {
               if (namevaluepair.length > 2) {
                   if ( control.options.length > 0) {
                       // control is listbox
                       // there's data:value and selection:value
                       var code = value;
                       value = (namevaluepair.length > 2 ? namevaluepair[2] : "");
                       var optionValues = "";
                       // concat all listbox items
                       for( var cnt = 0; cnt < control.options.length; cnt++) {
                          if (code == 'data') {
                              optionValues += control.options[cnt].text;
                          } else if (code == 'selection') {
                              optionValues += control.options[cnt].selected;
                          }
                          optionValues += "\r\n";
                       }
                       if( encodeURIComponent(optionValues) != value ) {
                          // items in the listbox have changed
                          // alert("listbox " + code + " changed: " + encodeURIComponent(optionValues) + " vs. " + value);
                          return true;
                       }
                   }
               } else if(control.selectedIndex != value) {
                   // alert("dropdown selection changed: " + control.selectedIndex + " vs. " + value);
                   return true;
               }
            } else {
                if(encodeURIComponent(control.value) != value) {
                    // alert("control " + control.type + " changed: " + control.value + " vs. " + value);
                    return true;
                }
            }
        }
        return false;
    },

    initialize : function() {        
        DirtyPanelExtender.DirtyPanelExtenderBehavior.callBaseMethod(this, 'initialize');
        DirtyPanelExtender_dirtypanels[DirtyPanelExtender_dirtypanels.length] = this;
    },

    dispose : function() {
        DirtyPanelExtender.DirtyPanelExtenderBehavior.callBaseMethod(this, 'dispose');
    },

    get_OnLeaveMessage : function() {
        return this._OnLeaveMessageValue;
    },

    set_OnLeaveMessage : function(value) {
        this._OnLeaveMessageValue = value;
    }
}

DirtyPanelExtender.DirtyPanelExtenderBehavior.registerClass('DirtyPanelExtender.DirtyPanelExtenderBehavior', AjaxControlToolkit.BehaviorBase);

var DirtyPanelExtender_dirtypanels = new Array()

function DirtyPanelExtender_SuppressDirtyCheck()
{
  window.onbeforeunload = null;
}

function __newDoPostBack(eventTarget, eventArgument) 
{ 
  // supress prompting on postback
  DirtyPanelExtender_SuppressDirtyCheck();
  return __savedDoPostBack (eventTarget, eventArgument);
}

var __savedDoPostBack = __doPostBack;
__doPostBack = __newDoPostBack; 

window.onbeforeunload = function (eventargs) 
{
    for (i in DirtyPanelExtender_dirtypanels)
    {
        var panel = DirtyPanelExtender_dirtypanels[i];
        if (panel.isDirty()) 
        {
          if(! eventargs) eventargs = window.event;
          eventargs.returnValue = panel.get_OnLeaveMessage();
          break;
        }
    }
}


The BehaviorBase class now resides in a different namespace. You need to replace the line...

DirtyPanelExtender.DirtyPanelExtenderBehavior.registerClass('DirtyPanelExtender.DirtyPanelExtenderBehavior', AjaxControlToolkit.BehaviorBase);

with...

DirtyPanelExtender.DirtyPanelExtenderBehavior.registerClass('DirtyPanelExtender.DirtyPanelExtenderBehavior', Sys.Extended.UI.BehaviorBase);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜