NavButton代码
using System.Web.UI.WebControls;
using System.ComponentModel;
namespace WebControlTestItem
{
/// <summary>
/// WebCustomControl 的摘要说明。
/// </summary>
[DefaultEvent("ClickNext"),
DefaultProperty("NextText"),
ToolboxData("<{0}:SimpleButton runat=server></{0}:SimpleButton>")
]
public class SimpleButton:WebControl,IPostBackEventHandler
{
private string _nexttext;
private string _previoustext;
[Bindable(true),
Category("外观"),
DefaultValue("Next"),
Description("显示在按钮上的文字")
]
public virtual string NextText
{
get{ return((_nexttext==null)?String.Empty:_nexttext);}
set{_nexttext=value;}
}
[Bindable(true),
Category("外观"),
DefaultValue("Previous"),
Description("显示在按钮上的文字")
]
public virtual string PreviousText
{
get{ return((_previoustext==null)?String.Empty:_previoustext);}
set{_previoustext=value;}
}
/*protected override HtmlTextWriterTag TagKey
{
get { return HtmlTextWriterTag.Input;}
}
*/
//Click事件的键
private static readonly object EventClickNext=new object();
private static readonly object EventClickPreivous=new object();
//事件属性
[Category("操作"),Description("按下时触发按钮单击事件")]
public event EventHandler ClickNext
{
add{
Events.AddHandler(EventClickNext,value);
}
remove{Events.RemoveHandler(EventClickNext,value);}
}
[Category("操作"),Description("按下时触发按钮单击事件")]
public event EventHandler ClickPreivous
{
add
{
Events.AddHandler(EventClickPreivous,value);
}
remove{Events.RemoveHandler(EventClickPreivous,value);}
}
protected virtual void OnClickNext(EventArgs e)
{
EventHandler clickHandler=(EventHandler)Events[EventClickNext];
if(clickHandler!=null)
clickHandler(this,e);
}
protected virtual void OnClickPreivous(EventArgs e)
{
EventHandler clickHandler=(EventHandler)Events[EventClickPreivous];
if(clickHandler!=null)
clickHandler(this,e);
}
/*protected override void AddAttributesToRender(HtmlTextWriter writer)
{
base.AddAttributesToRender(writer);
writer.AddAttribute(HtmlTextWriterAttribute.Name,this.UniqueID);
writer.AddAttribute(HtmlTextWriterAttribute.Type,"submit");
writer.AddAttribute(HtmlTextWriterAttribute.Value,this.Text);
}*/
#region IPostBackEventHandler 成员
public void RaisePostBackEvent(string eventArgument)
{
// TODO: 添加 SimpleButton.RaisePostBackEvent 实现
if(eventArgument=="下一步")
OnClickNext(EventArgs.Empty);
if(eventArgument=="上一步")
OnClickPreivous(EventArgs.Empty);
}
#endregion
protected override void Render(HtmlTextWriter writer)
{
if(Page!=null)
Page.VerifyRenderingInServerForm(this);
base.Render (writer);
}
protected override void RenderContents(HtmlTextWriter writer)
{
base.RenderContents (writer);
writer.AddAttribute(HtmlTextWriterAttribute.Onclick,Page.GetPostBackEventReference(this,"上一步"));
writer.AddAttribute("language","javascript"); //此句非必须
writer.RenderBeginTag(HtmlTextWriterTag.Button);
writer.Write(this.PreviousText);
writer.RenderEndTag();
writer.Write(" ");
writer.AddAttribute(HtmlTextWriterAttribute.Onclick,Page.GetPostBackEventReference(this,"下一步"));
writer.AddAttribute("language","javascript");//此句非必须
writer.RenderBeginTag(HtmlTextWriterTag.Button);
writer.Write(this.NextText);
writer.RenderEndTag();
}
}
}
以上代码是按照书第9章的NavButton代码修改的代码,原书中属性是用ViewState字典做的,但运行时按钮上没有任何字符,我把它改成普通属性,就好了。
另外,PreviousText和NextText不能设置为Previous和Next,否则运行时按钮上也没有任何字符。这一点很让人困惑。注释部分是书上前一个例子的代码。