Exemple de publication à l'aide d'un script côté client
L'exemple suivant crée un bouton de lien personnalisé qui lance la publication à l'aide d'un script côté client (JScript, JavaScript). Pour générer l'exemple, consultez les instructions fournies dans Exemples de contrôles serveur.
Pour obtenir un exemple similaire dans cet exemple, mais dérivant de WebControl, consultez Rendu d'exemples d'un contrôle serveur.
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
Utilisation du contrôle sur une page
La page ASP.NET suivante utilise le bouton de lien personnalisé créé dans l'exemple précédent.
<%@ 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>