从ajax提升自定义服务器控件的事件?

时间:2014-05-18 16:42:23

标签: asp.net ajax events custom-controls custom-server-controls

我有一个自定义服务器控件,它被添加到许多不同的asp.net页面(它生成一个blueimp jquery文件上传插件)。

如何从javascript / ajax在该服务器控件上引发事件?或者从http处理程序? 我想在jquery文件上传后用ajax将一些文件发布到HTTP处理程序后,在该控件上引发一个OnFileUploaded事件?

编辑 - 除了Dalorzo的回答:

我使用了@Dalorzo编写的代码。 然后我不得不像这样回复我的对象作为目标:

myctrlPostBackEventReference = Page.ClientScript.GetPostBackEventReference(myCtrl, "");
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "initMyClientVariable", "var postBackEventReference=\"" + myctrlPostBackEventReference + "\";", true);

并在我的javascript文件中添加了:

eval(postBackEventReference)

执行它。

然后我仍然有一个问题,我想阻止整页渲染,所以我使用相同的方法,但我回到了一个'虚拟'updatepanel作为目标。更新面板在我想要的控件上引发了事件(我已经为更新面板发送了所需的控件ID作为__EVENTARGUMENT(GetPostBackEventReference的第二个参数),以区分实际应该引发哪个事件。

1 个答案:

答案 0 :(得分:0)

您需要实施IPostaBackEventHandler和/或IScriptControl之类的内容:

[ToolboxData("<{0}:ComboBox runat=server></{0}:ComboBox>")]
public class ComboBox : DropDownList, IScriptControl, IPostBackEventHandler 
{

以下是您使用IPostBackEventHandler的方式:

  // Defines the Change event. 
  public event EventHandler Change;

  //Invoke delegates event Change. 
  protected virtual void OnChange(EventArgs e) {

     if (Change != null) {
        Change(this, e);
     }   
  }

  // Implements IPostBackEventHandler that raises the change events. 
  public void RaisePostBackEvent(string eventArgument){
     OnChange(new EventArgs());
  }