Firing Postbacks from a Custom Control

Some controls support the AutoPostBack attribute and so can cause a postback to occur after a change in the data. Unfortunately, the custom controls you design might not descend from a control that supports AutoPostBack. Fortunately, there is a solution. The Page class exposes a method named GetPostBackEventReference that emits client-side script that allows a control to cause a postback. The resulting JavaScript is nearly identical to the JavaScript in Listing 7-3. Listing 7-4 shows a Visual Basic .NET hyperlink control that reacts to a click by causing a postback rather than directly calling another page, as a traditional hyperlink would do.

Listing 7-4 PostLink.vb, a postback link control

Listing 7-4 PostLink.vb, a postback link control

Imports System.ComponentModel Imports System.Web.UI

<Assembly: TagPrefix("PostLink", "PostLinkStuff")>

<DefaultProperty("Text"), ToolboxData( _ "<{0}:PostLinkControl runat=server></{0}:PostLinkControl>")> Public Class PostLinkControl

Inherits System.Web.UI.WebControls.WebControl

Dim _text As String

<Bindable(True), Category("Appearance"), DefaultValue("")> Property [Text]() As String Get

Return _text End Get

Set(ByVal Value As String)

_text = Value End Set End Property

Protected Overrides Sub Render( _ ByVal output As System.Web.UI.HtmlTextWriter)

Page.GetPostBackClientEvent(Me, _text) + >")

output.Write(_text + "</a>") End Sub

End Class

End Class

Note ASP.NET

includes a

LinkButto n control that does much of what this example control does. In general, the techniqu e shown here would be used on more complex controls, but adding this functiona lity to a hyperlink control makes the process easier to understa nd.

PostLink.vb, shown in Listing 7-4, began as a Visual Basic Web Control Library project created in Visual Studio .NET. I added the following line:

<Assembly: TagPrefix("PostLink", "PostLinkStuff")>

This code tells Visual Studio .NET to use the tag prefix PostLinkStuff for any control in the PostLink namespace that is dragged onto the design surface. Recall from Chapter 6 that if you don't specify a TagPrefix, Visual Studio .NET will use cc1, cc2, and so on. The other section of code modified from the Web Control Library project is the Render method, shown here:

Protected Overrides Sub Render( _

ByVal output As System.Web.UI.HtmlTextWriter)

Page.GetPostBackClientEvent(Me, _text) + >")

output.Write(_text + "</a>") End Sub

One confusing part of this Render code is the use of multiple quotation marks in the output.Write call. To include quotation marks within a string, each quotation mark must be preceded by a quotation mark so that the quotation mark isn't interpreted as the end of the string.

In C#, the same method would be written as follows:

protected override void Render(HtmlTextWriter output) {

output.Write("<a id=\"" + this.UniqueID + "\" href=\"javascript:" +

Page.GetPostBackClientEvent(this, text) + V>") output.Write(text + "</a>")

C# follows the C/C++ convention of using the backslash character to prefix a quotation mark within a quoted literal string. In addition, C# uses the this keyword to identify the current instance, whereas Visual Basic .NET uses Me.

The Render method creates an anchor tag that includes JavaScript code in the href attribute. The JavaScript includes a call to Page.GetPostBackClientEvent, which is passed a reference to the current instance (using Me in Visual Basic .NET, or this in C#) and the value of _text. Both of these values are available on the server in the

_EVENTTARGET and_EVENTARGUMENT variables, as shown in the Page_Load method of the code-behind file TestPostLink.aspx.vb in Listing 7 .

0 0

Post a comment