62,025
社区成员
发帖
与我相关
我的任务
分享
<asp:Panel ID="Panel1" runat="server">
这里有内容
</asp:Panel>
Button _button = new Button();
Panel1.Controls.Add(_button);
protected override void Render(HtmlTextWriter writer)
{
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();
}
<cc1:FlowContral ID="FlowContral1" runat="server" Text="点我啊">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</cc1:FlowContral>
<div id="FlowContral1">
<input type="submit" name="btn" value="点我啊" id="btn" /><span id="lab"></span>
<input name="TextBox1" type="text" id="TextBox1" />
</div>
<div id="FlowContral1">
<input name="TextBox1" type="text" id="TextBox1" />
<input type="submit" name="btn" value="点我啊" id="btn" /><span id="lab"></span>
</div>
<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;
}
}
}
<asp:Panel ID="Panel1" runat="server">
这里有内容<br />
这里有内容<br />
<table border=0 cellspacing=0 cellpadding=0>
<tr>
<td>这里有内容</td>
<td><asp:PlaceHolder ID="place1" runat="server" /></td>
</tr>
</table>
</asp:Panel>
<asp:Panel ID="Panel1" runat="server">
这里有内容<br />
</asp:Panel>