AjaxControlToolkit未定义 - 将扩展器更新时出错

时间:2011-10-07 15:55:40

标签: asp.net ajaxcontroltoolkit asp.net-4.0

我正在寻找一种方法来警告用户,如果表单“脏”并且他们试图远离它。

This project似乎正是我所需要的。

然而,它是在2007年写的,当我尝试在我的.NET 4.0应用程序中使用最新版本的Ajax Control Toolkit“按原样”使用它时,它无效。

所以,我下载了源代码(available from the CodeProject article),并尝试将其更新。但这是我第一次尝试这样的任务而且我有点失落。

我现在得到的错误如下:
Microsoft JScript运行时错误:'AjaxControlToolkit'未定义

给出错误的DirtyPanelExtenderBehaviour.js中的行是: DirtyPanelExtender.DirtyPanelExtenderBehavior.registerClass('DirtyPanelExtender.DirtyPanelExtenderBehavior', AjaxControlToolkit.BehaviorBase);

这是我到目前为止所做的事情:
1.从Codeproject文章下载源 2.在Visual Studio 2010中打开并完成升级向导
3.删除了对AjaxControlToolkit.dll的引用,并替换为对最新版本工具包的新引用 4.修改了ScriptManagerToolkitScriptManager的引用(不确定是否需要)
5.修改了示例网站使用页面上的ToolkitScriptManager代替ScriptManager

Sooo,任何想法我做错了什么?或者我错过了什么?这样我可以重新编译DirtyPanelExtender.dll并在我的.NET 4.0项目中使用它吗?

完整代码低于我正在使用的代码。 The original author写了这个,除了上面详述的我的修正案 - 我没有得到赞誉!

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();
                    StringBuilder 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;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

BehaviorBase类现在位于不同的命名空间中。你需要更换线......

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

...与

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