我是想开发一个服务器控件,该控件继承Panel类,
我是想将Botton添加到"这里有内容"下面,不知道怎么实现?
<cc1:FlowContral ID="FlowContral1" runat="server" Text="立即提交" OnButtonClick="SaveFlow">
这里有内容
</cc1:FlowContral>服务器控件代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;namespace test
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:ServerControl1 runat=server></{0}:ServerControl1>")]
public class FlowContral : Panel
{
//声明变量
private Button _button;
private static readonly object EventButtonClick = new object();
private Style _buttonStyle;
private Label _label; //定义属性Text,用于指定按钮上的文字
[
Bindable(true),
Category("Appearance"),
DefaultValue(""),
Description("获取或设置显示显示在按钮上的文字")
]
public string Text
{
get
{
EnsureChildControls();
return _button.Text;
}
set
{
EnsureChildControls();
_button.Text = value;
}
} //定义ButtonStyle属性
[
Category("Style"),
Description("设置Button的样式属性"),
DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
NotifyParentProperty(true),
PersistenceMode(PersistenceMode.InnerProperty),
]
public virtual Style ButtonStyle
{
get
{
if (_buttonStyle == null)
{
_buttonStyle = new Style();
if (IsTrackingViewState)
{
((IStateManager)_buttonStyle).TrackViewState();
}
}
return _buttonStyle;
}
} //重写Controls属性
public override ControlCollection Controls
{
get
{
EnsureChildControls();
return base.Controls;
}
}
//重写CreateChildControls方法,将子控件添加到复合控件中
protected override void CreateChildControls()
{
Controls.Clear();
_button = new Button();
_label = new Label();
_label.ID = "lab";
_button.ID = "btn";
_button.CommandName = "ButtonClick";
this.Controls.Add(_button);
this.Controls.Add(_label);
}
public event EventHandler ButtonClick
{
add
{
Events.AddHandler(EventButtonClick, value);
}
remove
{
Events.RemoveHandler(EventButtonClick, value);
}
} protected virtual void OnButtonClick(EventArgs e)
{
EventHandler buttonClickHandler = (EventHandler)Events[EventButtonClick];
if (buttonClickHandler != null)
{
buttonClickHandler(this, e);
}
} protected override bool OnBubbleEvent(object sender, EventArgs e)
{
bool handled = false;
if (e is CommandEventArgs)
{
CommandEventArgs ce = (CommandEventArgs)e;
if (ce.CommandName == "ButtonClick")
{
OnButtonClick(EventArgs.Empty);
handled = true;
}
}
return handled;
}
}
}
我是想将Botton添加到"这里有内容"下面,不知道怎么实现?
<cc1:FlowContral ID="FlowContral1" runat="server" Text="立即提交" OnButtonClick="SaveFlow">
这里有内容
</cc1:FlowContral>服务器控件代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;namespace test
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:ServerControl1 runat=server></{0}:ServerControl1>")]
public class FlowContral : Panel
{
//声明变量
private Button _button;
private static readonly object EventButtonClick = new object();
private Style _buttonStyle;
private Label _label; //定义属性Text,用于指定按钮上的文字
[
Bindable(true),
Category("Appearance"),
DefaultValue(""),
Description("获取或设置显示显示在按钮上的文字")
]
public string Text
{
get
{
EnsureChildControls();
return _button.Text;
}
set
{
EnsureChildControls();
_button.Text = value;
}
} //定义ButtonStyle属性
[
Category("Style"),
Description("设置Button的样式属性"),
DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
NotifyParentProperty(true),
PersistenceMode(PersistenceMode.InnerProperty),
]
public virtual Style ButtonStyle
{
get
{
if (_buttonStyle == null)
{
_buttonStyle = new Style();
if (IsTrackingViewState)
{
((IStateManager)_buttonStyle).TrackViewState();
}
}
return _buttonStyle;
}
} //重写Controls属性
public override ControlCollection Controls
{
get
{
EnsureChildControls();
return base.Controls;
}
}
//重写CreateChildControls方法,将子控件添加到复合控件中
protected override void CreateChildControls()
{
Controls.Clear();
_button = new Button();
_label = new Label();
_label.ID = "lab";
_button.ID = "btn";
_button.CommandName = "ButtonClick";
this.Controls.Add(_button);
this.Controls.Add(_label);
}
public event EventHandler ButtonClick
{
add
{
Events.AddHandler(EventButtonClick, value);
}
remove
{
Events.RemoveHandler(EventButtonClick, value);
}
} protected virtual void OnButtonClick(EventArgs e)
{
EventHandler buttonClickHandler = (EventHandler)Events[EventButtonClick];
if (buttonClickHandler != null)
{
buttonClickHandler(this, e);
}
} protected override bool OnBubbleEvent(object sender, EventArgs e)
{
bool handled = false;
if (e is CommandEventArgs)
{
CommandEventArgs ce = (CommandEventArgs)e;
if (ce.CommandName == "ButtonClick")
{
OnButtonClick(EventArgs.Empty);
handled = true;
}
}
return handled;
}
}
}
{
Controls.Remove(_button);//先将button移出Controls集,这样输出就不会显示此button
RenderContents(writer);//显示Panel内的内容
writer.RenderBeginTag(HtmlTextWriterTag.Div);
this.Controls.Add(_button);//必须再将button添加到Controls集,这样能才捕获到onclick的事件
_button.RenderControl(writer);//显示button
writer.RenderEndTag();
}