WebControl PostBack处理逻辑的问题
查看包含WebControl页面的html源码,发现如下代码
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
<script type="text/javascript">
<!--
var theForm = document.forms['form1'];
if (!theForm) {
theForm = document.form1;
}
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
// -->
</script>
<input name="TextBox1" type="text" onchange="javascript:setTimeout('__doPostBack(\'TextBox1\',\'\')', 0)" onkeypress="if (WebForm_TextBoxKeyHandler(event) == false) return false;" id="TextBox1" />
可见TextBox控件在客户端是一个input对象,通过调用__doPostBack方法实现PostBack提交。这个方法有2个参数,存放在2个Hidden控件里。
自制一个文本框控件,部分代码如下
[DefaultProperty("Text")]
[ToolboxData("<{0}:MyTextBox runat=server></{0}:MyTextBox>")]
public class MyTextBox : WebControl, IPostBackDataHandler
{
private static readonly object EventTextChanged;
[Category("Action"), Description("TextBox_OnTextChanged")]
public event EventHandler TextChanged
{
add
{
base.Events.AddHandler(EventTextChanged, value);
}
remove
{
base.Events.RemoveHandler(EventTextChanged, value);
}
}
[DefaultValue(""), Localizable(true), Bindable(true, BindingDirection.TwoWay),
Category("Appearance"), Description("TextBox_Text"),
PersistenceMode(PersistenceMode.EncodedInnerDefaultProperty)]
public string Text
{
get
{
String s = (String)ViewState["Text"];
return ((s == null) ? String.Empty : s);
}
set
{
ViewState["Text"] = value;
}
}
protected override void RenderContents(HtmlTextWriter output)
{
//output.Write(Text);
}
protected override void Render(HtmlTextWriter output)
{
output.AddAttribute(HtmlTextWriterAttribute.Type, "Text");
output.AddAttribute(HtmlTextWriterAttribute.Onchange, "javascript:setTimeout(\"__doPostBack(\'" + this.ID + "\',\'\')\", 0)",true);
output.RenderBeginTag(HtmlTextWriterTag.Input);
output.RenderEndTag();
}
protected override void OnPreRender(EventArgs e)
{
}
[Category("Behavior"), DefaultValue(false), Themeable(false), Description("TextBox_AutoPostBack")]
public virtual bool AutoPostBack
{
get
{
object obj2 = this.ViewState["AutoPostBack"];
if (obj2 != null)
{
return (bool)obj2;
}
return false;
}
set
{
this.ViewState["AutoPostBack"] = value;
}
}
bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)
{
//base.ValidateEvent(postDataKey);
string text = this.Text;
string str2 = postCollection[postDataKey];
if (!text.Equals(str2, StringComparison.Ordinal))
{
this.Text = str2;
return true;
}
return false;
}
void IPostBackDataHandler.RaisePostDataChangedEvent()
{
if (this.AutoPostBack)
{
//this.Page.AutoPostBackControl = this;
//if (this.CausesValidation)
//{
// this.Page.Validate(this.ValidationGroup);
//}
}
this.OnTextChanged(EventArgs.Empty);
}
protected virtual void OnTextChanged(EventArgs e)
{
EventHandler handler = (EventHandler)base.Events[EventTextChanged];
if (handler != null)
{
handler(this, e);
}
}
}
注意其中LoadPostData,RaisePostDataChangedEvent2个实现IPostBackDataHandler的方法,但在提交时始终没有调用这2个方法,是否我哪些地方没有搞清楚?请高手指点。