Prev | Current Page 470 | Next

Rob Cameron and Dale Michalk

"Pro ASP.NET 3.5 Server Controls and AJAX Components"

This hooks
into the normal HTTP posting mechanism, as we describe in Chapter 5.
Listing 8-7. The FormConfirmation Server Control
using System;
using System.Web;
using System.Web.UI;
using System.ComponentModel;
namespace ControlsBook2Lib.Ch08
{
[ToolboxData("<{0}:FormConfirmation runat=server>"),
DefaultProperty("Message")]
public class FormConfirmation : Control
{
public virtual string Message
{
get
{
return (string)ViewState["Message"];
}
CHAPTER 8 ?–  INTEGRATING CL IENT-SIDE SCRIPT 367
set
{
ViewState["Message"] = value;
}
}
protected override void OnPreRender(EventArgs e)
{
if (Context.Request.Browser.EcmaScriptVersion.Major >= 1)
{
string script = "return (confirm('" + this.Message + "'));";
// register JavaScript code for onsubmit event
// of the HTML
element
Page.ClientScript.RegisterOnSubmitStatement(typeof(FormConfirmation),
"FormConfirmation", script);
}
}
protected override void Render(HtmlTextWriter writer)
{
// make sure the control is rendered inside
// tags
Page.VerifyRenderingInServerForm(this);
base.Render(writer);
}
}
}
FormConfirmation exposes a Message property to allow web developers to customize the
JavaScript confirmation prompt to the end user.


Pages:
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482