我有一个扩展程序控件,它在用户完成输入500ms后引发textbox的OnTextChanged
事件。这样做的问题是,当文本框失去焦点时,会引发OnTextChanged
,这会导致问题(由于回发)。
我想要做的是为扩展程序控件提供自己的服务器端事件(比如OnDelayedSubmit
),这样我就可以单独处理它了。该事件将在扩展器控件的行为脚本中产生(在500ms延迟之后),因此不能在onchanged
中放置__doPostBack
。
有没有人能说明一下该怎么做呢?
发布于 2008-09-01 05:12:20
在大量阅读了扩展控件和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);
}
}
现在,可以像处理任何其他控件上的事件一样处理服务器端事件。
https://stackoverflow.com/questions/37555
复制