WebControl PostBack处理逻辑的问题

uncarman 2007-09-28 09:48:31
查看包含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个方法,是否我哪些地方没有搞清楚?请高手指点。
...全文
94 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
cyy1981 2007-09-28
  • 打赏
  • 举报
回复
这两个方法都会在page中执行,在control 和masnter page中是不会执行的
load view state和load post data 在page:
protected override object LoadPageStateFromPersistenceMedium()
{
return base.LoadPageStateFromPersistenceMedium();
}
RaisePostDataChangedEvent 也是在page中
/// <summary>
/// The Page object calls the RaisePostBackEvent method when a postback occurs.
/// This call occurs in the page life cycle after loading and change notification are complete but before prerendering occurs.
/// </summary>
protected override void RaisePostBackEvent(IPostBackEventHandler sourceControl, string eventArgument)
{
base.RaisePostBackEvent(sourceControl, eventArgument);
}
uncarman 2007-09-28
  • 打赏
  • 举报
回复
已经搞定,在protected override void Render(HtmlTextWriter output)方法中加上output.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID);

uncarman 2007-09-28
  • 打赏
  • 举报
回复
已经搞定,在方法 protected override void Render(HtmlTextWriter output)中加上output.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID);

8,833

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 组件/控件开发
社区管理员
  • 组件/控件开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧