Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
The following sample creates a custom link button that initiates postback through client-side script (JScript, JavaScript). To build the sample, see the instructions in Server Control Samples.
For an example that is similar in this sample but derives from WebControl see Rendering Server Control Samples.
using System;
using System.Web.UI;
using System.Collections;
namespace CustomControls {
public class MyLinkButton: Control, IPostBackEventHandler{
// Defines the Click event.
//
public event EventHandler Click;
// Invokes delegates registered with the Click event.
//
protected virtual void OnClick(EventArgs e) {
if (Click != null) {
Click(this, e);
}
}
// Method of IPostBackEventHandler that raises change events.
//
public void RaisePostBackEvent(string eventArgument){
OnClick(new EventArgs());
}
protected override void Render(HtmlTextWriter output) {
output.Write("<a id=\"" + this.UniqueID + "\" href=\"javascript:" + Page.GetPostBackEventReference(this) +"\">");
output.Write(" " + this.UniqueID + "</a>");
}
}
}
[Visual Basic]
Option Explicit
Option Strict
Imports System
Imports System.Web.UI
Imports System.Collections
Namespace CustomControls
Public Class MyLinkButton
Inherits Control
Implements IPostBackEventHandler
' Defines the Click event.
'
Public Event Click As EventHandler
' Invokes delegates registered with the Click event.
'
Protected Overridable Sub OnClick(e As EventArgs)
RaiseEvent Click(Me, e)
End Sub
' Method of IPostBackEventHandler that raises change events.
'
Public Sub RaisePostBackEvent(eventArgument As String) Implements IPostBackEventHandler.RaisePostBackEvent
OnClick(New EventArgs())
End Sub
Protected Overrides Sub Render(output As HtmlTextWriter)
output.Write(("<a id=""" & Me.UniqueID & _
""" href=""javascript:" & _
Page.GetPostBackEventReference(Me) & """>"))
output.Write((" " & Me.UniqueID & "</a>"))
End Sub
End Class
End Namespace
The following ASP.NET page uses the custom link button created in the preceding sample.
<%@ Register TagPrefix="Custom" Namespace="CustomControls" Assembly = "CustomControls" %>
<script language="C#" runat=server>
private void Button_Click(Object sender, EventArgs e) {
TextBox.BackColor = System.Drawing.Color.LightGreen;
TextBox.Text = "The link button caused postback.";
}
</script>
<html>
<body>
<form runat=server>
Here is the custom link button.<br>
<Custom:MyLinkButton Id = "Link" OnClick = "Button_Click" runat=server/>
<br><br>
<asp:TextBox id = "TextBox" Text = "Click the link" Width = "200" BackColor = "Cyan" runat=server/>
<br>
</form>
</body>
</html>
[Visual Basic]
<%@ Register TagPrefix="Custom" Namespace="CustomControls" Assembly = "CustomControls" %>
<script language="VB" runat=server>
Private Sub Button_Click(sender As Object, e As EventArgs)
TextBox.BackColor = System.Drawing.Color.LightGreen
TextBox.Text = "The link button caused postback."
End Sub
</script>
<html>
<body>
<form runat=server>
Here is the custom link button.<br>
<Custom:MyLinkButton Id = "Link" OnClick = "Button_Click" runat=server/>
<br><br>
<asp:TextBox id = "TextBox" Text = "Click the link" Width = "200" BackColor = "Cyan" runat=server/>
<br>
</form>
</body>
</html>