我按照文章上写的那样,写一个很简单的自定义控件,实现点击增加按钮,文本框的值加1,点击减少按钮,文本框的值减1 ,可是我每次运行时候不管怎么点击按钮,文本框的值都为0 ,调试了一下,每次回发服务器后页面都运行CreateChildControls函数,所以为0,可是我把函数里的
box.Text="0"; 改成 box.Text=Convert.ToString(this.Value);
就会出错,怎么回事啊
box.Text="0"; 改成 box.Text=Convert.ToString(this.Value);
就会出错,怎么回事啊
[Bindable(true), Category("Appearance"),DefaultValue("")]
public int Value
{
get{
this.EnsureChildControls();
return Int32.Parse(((TextBox)Controls[1]).Text); }
set{
this.EnsureChildControls();
((TextBox)Controls[1]).Text = kvalue.ToString();
}
} protected override void CreateChildControls()
{
base.CreateChildControls ();
this.Controls.Add(new LiteralControl("<h3>"+"值:"));
TextBox box=new TextBox();
box.Text=Convert.ToString(this.Value);
this.Controls.Add(box);
this.Controls.Add(new LiteralControl("<h3>")); Button btnadd=new Button();
btnadd.Text="增加";
btnadd.Click+=new EventHandler(this.addclick);
this.Controls.Add(btnadd); this.Controls.Add(new LiteralControl("|")); Button btndel=new Button();
btndel.Text="减少";
btndel.Click+=new EventHandler(this.delclick);
this.Controls.Add(btndel);
} private void addclick(Object sender,System.EventArgs e)
{
this.Value++;
} private void delclick(Object sender,System.EventArgs e)
{
this.Value--;
}