ASP.NET Tutorial/Custom Controls/PostBack
Содержание
Adding PostBack capabilities to a server control (C#)
using System.ruponentModel;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebControlLibrary1
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:WebCustomControl3 runat=server></{0}:WebCustomControl3>")]
public class WebCustomControl3 : WebControl
{
protected override void Render(HtmlTextWriter output)
{
PostBackOptions p = new PostBackOptions(this);
output.AddAttribute(HtmlTextWriterAttribute.Onclick,
Page.ClientScript.GetPostBackEventReference(p));
output.AddAttribute(HtmlTextWriterAttribute.Value, "My Button");
output.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID);
output.AddAttribute(HtmlTextWriterAttribute.Name, this.ClientID);
output.RenderBeginTag(HtmlTextWriterTag.Button);
output.RenderEndTag();
}
}
}
Adding PostBack capabilities to a server control (VB)
Imports System.ruponentModel
Imports System.Web.UI
<DefaultProperty("Text")> _
<ToolboxData("<{0}:WebCustomControl3 runat=server></{0}:WebCustomControl3>")> _
Public Class WebCustomControl3
Inherits System.Web.UI.WebControls.WebControl
Protected Overrides Sub Render(ByVal output As System.Web.UI.HtmlTextWriter)
Dim p As New PostBackOptions(Me)
output.AddAttribute(HtmlTextWriterAttribute.Onclick, _
Page.ClientScript.GetPostBackEventReference(p))
output.AddAttribute(HtmlTextWriterAttribute.Value, "My Button")
output.AddAttribute(HtmlTextWriterAttribute.Id, Me.ClientID)
output.AddAttribute(HtmlTextWriterAttribute.Name, Me.ClientID)
output.RenderBeginTag(HtmlTextWriterTag.Button)
output.RenderEndTag()
End Sub
End Class
Handling postback events in a server control (C#)
using System.ruponentModel;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebControlLibrary1
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:WebCustomControl3 runat=server></{0}:WebCustomControl3>")]
public class WebCustomControl3 : WebControl, IPostBackEventHandler
{
protected override void Render(HtmlTextWriter output)
{
PostBackOptions p = new PostBackOptions(this);
output.AddAttribute(HtmlTextWriterAttribute.Onclick,Page.ClientScript.GetPostBackEventReference(p));
output.AddAttribute(HtmlTextWriterAttribute.Value, "My Button");
output.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID);
output.AddAttribute(HtmlTextWriterAttribute.Name, this.ClientID);
output.RenderBeginTag(HtmlTextWriterTag.Button);
output.RenderEndTag();
}
#region IPostBackEventHandler Members
public event EventHandler Click;
public virtual void OnClick(EventArgs e)
{
if (Click != null)
{
Click(this, e);
}
}
public void RaisePostBackEvent(string eventArgument)
{
OnClick(EventArgs.Empty);
}
#endregion
}
}
Handling postback events in a server control (VB)
Imports System.ruponentModel
Imports System.Web.UI
<DefaultProperty("Text")> _
<ToolboxData("<{0}:WebCustomControl3 runat=server></{0}:WebCustomControl3>")> _
Public Class WebCustomControl3
Inherits System.Web.UI.WebControls.WebControl
Implements IPostBackEventHandler
Protected Overrides Sub Render(ByVal output As System.Web.UI.HtmlTextWriter)
Dim p As New PostBackOptions(Me)
output.AddAttribute(HtmlTextWriterAttribute.Onclick, _
Page.ClientScript.GetPostBackEventReference(p))
output.AddAttribute(HtmlTextWriterAttribute.Value, "My Button")
output.AddAttribute(HtmlTextWriterAttribute.Id, Me.ClientID)
output.AddAttribute(HtmlTextWriterAttribute.Name, Me.ClientID)
output.RenderBeginTag(HtmlTextWriterTag.Button)
output.RenderEndTag()
End Sub
Public Event Click()
Public Sub OnClick(ByVal args As EventArgs)
RaiseEvent Click()
End Sub
Public Sub RaisePostBackEvent(ByVal eventArgument As String) Implements System.Web.UI.IPostBackEventHandler.RaisePostBackEvent
OnClick(EventArgs.Empty)
End Sub
End Class