ASP.NET服务器控件客户端事件处理

时间:2012-11-15 17:03:44

标签: javascript asp.net-ajax webforms

我对使用客户端功能的ASP.NET服务器控件有疑问。我想改进客户端事件处理:当子客户端控件触发事件时,我希望直接通知父客户端控件。

假设我们有两个服务器控件,MyWindow和MyTextBox。两者都是“Sys.UI.Control”类型的客户端对象。

MyTextBox是MyWindow的孩子:

public class MyWindow : Control, IScriptControl
{
   private MyTextBox _mtb;
   //....
   protected override void CreateChildControls()
   {
       _mtb = new MyTextBox();
       _mtb.ID = "MTB";
       _mtb.OnClientTextChanged = "OnClientTextChanged";
       Controls.Add(_mtb);
   }

   //...
   public IEnumerable<ScriptDescriptor> GetScriptDescriptors()
  {
     ScriptControlDescriptor descriptor = new ScriptControlDescriptor("Test.MyWindow", ClientID);
     descriptor.AddComponent("myTextBox", _mtb.ClientID);
     yield return descriptor;
  }

客户端:

  function OnClientTextChanged(sender, args) {
      alert('test')
  }

现在,只要文本框的文本发生更改,就会触发client-event并调用函数“OnClientTextChanged”。

我想要的是通知客户端对象“MyWindow”。我可以这样做:

  function OnClientTextChanged(sender, args) {
      $find("MyWindow").textBoxChangedItsText();
  }

但是如何在不使用这个全局javascript函数的情况下直接通知MyWindow的客户端对象?我测试的是

_mtb.OnClientTextChanged = string.Format("($find('{0}')).textBoxChangedItsText", ClientID);

但是我在客户端对象中的“textBoxChangedItsText”函数无法访问对象本身 - “this”是函数本身,但不是我将使用“$ find(”MyWindow“)”

找到的对象

我希望那些熟悉客户端启用的AJAX服务器端控件的人能够明白这个问题。

谢谢!

编辑:在客户端工作中使用此事件处理程序:

服务器端:

 _mtb.OnClientTextChanged = string.Format(" ($find('{0}')).textBoxChangedItsTextDelegate", ClientID);

客户端:

Test.MyWindow = function (element) {
   Test.MyWindow.initializeBase(this, [element]);
   this.textBoxChangedItsTextDelegate= Function.createDelegate(this, function (sender, e) {
      this.textBoxChangedItsText();
   });
};

Test.MyWindow.prototype = {
   initialize: function () {
      Test.MyWindow.callBaseMethod(this, 'initialize');
   },
   textBoxChangedItsText: function () {
      alert(this.get_id());
   },
   dispose: function () {
      Test.MyWindow.callBaseMethod(this, 'dispose');
   }
};

我仍然不喜欢的是使用事件处理程序中的$ find附加到事件服务器端:_mtb.OnClientTextChanged = string.Format(“($ find('{0}'))。textBoxChangedItsTextDelegate “,ClientID);

1 个答案:

答案 0 :(得分:1)

我认为您需要定义事件,因为它基本上是在ASP.NET客户端控件中完成的。因此,有必要将以下内容添加到MyTextBox客户端对象原型中:

add_textChanged: function(handler) {
    this.get_events().addHandler('shown', handler);
}
remove_TextChanged: function (handler) {
    this.get_events().removeHandler('textChanged', handler);
}
raise_textChanged: function (eventArgs) {
    var handler = this.get_events().getHandler('textChanged');
    if (handler) {
        handler(this, eventArgs);
    }
}

这将定义客户端代码(父控件或页面)可以订阅并执行所需操作的客户端事件。要在MyTextBox中引发此事件,您可以使用以下代码:

this.raise_textChanged(Sys.EventArgs.Empty);

要在MyWindow客户端对象中使用此事件,您需要将代码修改为以下内容:

Test.MyWindow = function (element) {
   Test.MyWindow.initializeBase(this, [element]);

   this._myTextBoxID = null;
   this._onTextChanged$delegate = Function.createDelegate(this, this._onTextChanged);
};

Test.MyWindow.prototype = {
   initialize: function () {
      Test.MyWindow.callBaseMethod(this, 'initialize');

      $find(this._myTextBoxID).add_textChanged(this._onTextChanged$delegate);
   },

   _onTextChanged: function(sender, eventArgs) {
        // Perform required actions.
        // this - will be instance of the this client side object.
   }

   dispose: function () {
        $find(this._myTextBoxID).remove_textChanged(this._onTextChanged$delegate);

      Test.MyWindow.callBaseMethod(this, 'dispose');
   }
};

注意:在提供的示例中,我使用了this._myTextBox,它等于子MyTextBox控件的客户端ID。您可以使用属性“myTextBox”而不执行$ find,因为您使用descriptor.AddComponent(“myTextBox”,_mtb.ClientID)初始化它;

相关问题