public event EventHandler ValueChanged
{
add
{
Events.AddHandler(ValueChangedEvent, value);
}
remove
{
Events.RemoveHandler(ValueChangedEvent, value) ;
}
}
//触发值被改变事件的方法
protected virtual void OnValueChanged(EventArgs e)
{
if( Events != null )
{
EventHandler oEventHandler = ( EventHandler )Events[ValueChangedEvent] ;
if (oEventHandler != null) oEventHandler(this, e);
}
}
//处理回发数据
bool IPostBackDataHandler.LoadPostData( string postDataKey, System.Collections.Specialized.NameValueCollection postCollection )
{
if ( postCollection[postDataKey] != Text )
{
Text = postCollection[postDataKey];
return true;
}
return false;
}
//告诉应用程序该组件的状态已更改
void IPostBackDataHandler.RaisePostDataChangedEvent()
{
OnValueChanged( EventArgs.Empty );
}-------------------------------------------------------------------------------------
关于add,remove本人看了些文章,但始终没弄明白,它在什么时候起作用?怎样调用它的
?可能问题有些不合逻辑,还得请高手指点迷津。谢谢各位!
public event EventHandler ValueChanged
{
add
{
Events.AddHandler(ValueChangedEvent, value);
}
remove
{
Events.RemoveHandler(ValueChangedEvent, value) ;
}
}
{
add
{
Events.AddHandler(ValueChangedEvent, value);
}
remove
{
Events.RemoveHandler(ValueChangedEvent, value) ;
}
}
//触发值被改变事件的方法
protected virtual void OnValueChanged(EventArgs e)
{
if( Events != null )
{
EventHandler oEventHandler = ( EventHandler )Events[ValueChangedEvent] ;
if (oEventHandler != null) oEventHandler(this, e);
}
}
//处理回发数据
bool IPostBackDataHandler.LoadPostData( string postDataKey, System.Collections.Specialized.NameValueCollection postCollection )
{
if ( postCollection[postDataKey] != Text )
{
Text = postCollection[postDataKey];
return true;
}
return false;
}
//告诉应用程序该组件的状态已更改
void IPostBackDataHandler.RaisePostDataChangedEvent()
{
OnValueChanged( EventArgs.Empty );
}-------------------------------------------------------------------------------------
关于add,remove本人看了些文章,但始终没弄明白,它在什么时候起作用?怎样调用它的
?可能问题有些不合逻辑,还得请高手指点迷津。谢谢各位!
public event EventHandler ValueChanged
{
add
{
Events.AddHandler(ValueChangedEvent, value);
}
remove
{
Events.RemoveHandler(ValueChangedEvent, value) ;
}
}
比如你要为控件增加一个click事件
public event EventHandler Click {
add {
Events.AddHandler(EventClick, value);
}
remove {
Events.RemoveHandler(EventClick, value);
}
}
你就需要它了。一般的要是作复合控件和继承服务器控件的时候不需要这个,基类
已经把这步工作做好,给你一个简单的复合控件作为参考using System;
using System.IO;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;namespace MyControl
{
public class MyFriend : WebControl, INamingContainer
{
public MyFriend()
{ }
private TextBox txt; protected override void Render(HtmlTextWriter writer)
{
if(!this.Visible)
{
return;
}
base.Render (writer); } protected override void CreateChildControls()
{
if(!this.Visible)
{
return;
} this.Controls.Clear();
this.ClearChildViewState();
Table myTable = new Table();
TableRow row = new TableRow(); TableCell myCell = new TableCell();
TableCell myCell2 = new TableCell(); myCell.HorizontalAlign = HorizontalAlign.Left ;
myCell.VerticalAlign = VerticalAlign.Middle; myCell2.HorizontalAlign = HorizontalAlign.Left ;
myCell2.VerticalAlign = VerticalAlign.Middle;
txt = new TextBox();
txt.ID = "txt";
myCell.Controls .Add (txt);
row.Cells.Add(myCell); LinkButton myLinkButton = new LinkButton();
myLinkButton.ID = "myLinkButton";
myLinkButton.Click += new EventHandler(myLinkButton_Click);
myLinkButton.Text = "好友";
myCell2.Controls.Add(myLinkButton);
row.Cells.Add(myCell2);
myTable.Rows.Add(row);
Controls.Add(myTable);
}
private void myLinkButton_Click(object sender,System.EventArgs e)
{
txt.Text = ((LinkButton)sender).Text+Guid.NewGuid().ToString ();
} }
}
我也是在学习这个,希望多多交流
[email protected]
http://search.csdn.net/Expert/topic/2499/2499254.xml?temp=.3906977
{
add
{
..........
}
remove
{
............
}
}
2. 这个检查回复数据是否已经改变,返回值决定了是否调用RaisePostDataChangedEvent,即 return true 将导致RaisePostDataChangedEvent被调用 bool IPostBackDataHandler.LoadPostData( string postDataKey, System.Collections.Specialized.NameValueCollection postCollection )
{
if ( postCollection[postDataKey] != Text )
{
Text = postCollection[postDataKey];
return true;
}
return false;
}3. 这里调用 OnValueChanged(),即,根据LoadPostData(),如果数据改变了,return true, OnValueChanged()就会被调用
void IPostBackDataHandler.RaisePostDataChangedEvent()
{
OnValueChanged( EventArgs.Empty );
}4. 如果有订阅,即触发事件 protected virtual void OnValueChanged(EventArgs e)
{
if( Events != null )
{
EventHandler oEventHandler = ( EventHandler )Events[ValueChangedEvent] ;
if (oEventHandler != null) oEventHandler(this, e);
}
}
http://msdn.microsoft.com/library/default.asp?url=/library/chs/cpguide/html/cpconreceivingpostbackdatachangednotifications.asp