gpt4 book ai didi

asp.net - 将服务器端事件添加到扩展器控件

转载 作者:行者123 更新时间:2023-12-04 19:24:56 25 4
gpt4 key购买 nike

我有一个扩展器控件可以引发文本框的 OnTextChanged用户完成输入后 500 毫秒的事件。问题在于 OnTextChanged当文本框失去焦点时会引发问题,这会导致问题(由于回发)。

我想要做的是让扩展器控制它自己的服务器端事件(比如, OnDelayedSubmit ),这样我就可以单独处理它。该事件将源自扩展器控件的行为脚本(在 500 毫秒延迟之后),因此放置一个 __doPostBackonchanged不是一个选择。

任何人都可以阐明如何解决这个问题吗?

最佳答案

在对扩展器控件和 JavaScript 进行了大量阅读之后,我拼凑了一个到目前为止似乎有效的解决方案。

主要技巧是从服务器端获取必要的回发代码到客户端行为脚本。我通过使用 ExtenderControlProperty 做到了这一点(在控件的 OnPreRender 函数中设置),然后在行为脚本中进行评估。其余的是基本的事件处理内容。

所以现在我的扩展器控件的 .cs文件看起来像这样:

public class DelayedSubmitExtender : ExtenderControlBase, IPostBackEventHandler
{
// This is where we'll give the behavior script the necessary code for the
// postback event
protected override void OnPreRender(EventArgs e)
{
string postback = Page.ClientScript.GetPostBackEventReference(this, "DelayedSubmit") + ";";
PostBackEvent = postback;
}

// This property matches up with a pair of get & set functions in the behavior script
[ExtenderControlProperty]
public string PostBackEvent
{
get
{
return GetPropertyValue<string>("PostBackEvent", "");
}
set
{
SetPropertyValue<string>("PostBackEvent", value);
}
}

// The event handling stuff
public event EventHandler Submit; // Our event

protected void OnSubmit(EventArgs e) // Called to raise the event
{
if (Submit != null)
{
Submit(this, e);
}
}

public void RaisePostBackEvent(string eventArgument) // From IPostBackEventHandler
{
if (eventArgument == "DelayedSubmit")
{
OnSubmit(new EventArgs());
}
}

}

我的行为脚本如下所示:
DelayedSubmitBehavior = function(element) {
DelayedSubmitBehavior.initializeBase(this, [element]);

this._postBackEvent = null; // Stores the script required for the postback
}

DelayedSubmitBehavior.prototype = {
// Delayed submit code removed for brevity, but normally this would be where
// initialize, dispose, and client-side event handlers would go

// This is the client-side part of the PostBackEvent property
get_PostBackEvent: function() {
return this._postBackEvent;
},
set_PostBackEvent: function(value) {
this._postBackEvent = value;
}

// This is the client-side event handler where the postback is initiated from
_onTimerTick: function(sender, eventArgs) {
// The following line evaluates the string var as javascript,
// which will cause the desired postback
eval(this._postBackEvent);
}
}

现在可以像处理任何其他控件上的事件一样处理服务器端事件。

关于asp.net - 将服务器端事件添加到扩展器控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37555/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com