ComponentModel;
namespace ControlsBook2Lib.Ch08
{
[ToolboxData("<{0}:ClickLabel runat=server>{0}:ClickLabel>"),
DefaultProperty("ClickText")]
public class ClickLabel : Label
{
public virtual string ClickText
{
get
{
return (string)ViewState["ClickText"];
}
set
{
ViewState["ClickText"] = value;
}
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
// Add the onclick client-side event handler to
// display a JavaScript alert box
350 CHAPTER 8 ?– I NTEGRAT ING CLI ENT-SI D E SCRIPT
Attributes.Add("onclick", "alert('" + ClickText + "');");
}
}
}
This ensures that the control??™s Attributes collection is loaded before the Render method is
called. A ClickText property is provided to make the control easily configurable as to what text
message displays in the JavaScript alert pop-up. Though this is a trivial example, it does demonstrate
one way to make client-side script capabilities available through a server control to the
developer/user without the developer having to know how to write the JavaScript. In the next
section, we present a web form that demonstrates these options.
The Click Web Form
The following Click web form example demonstrates all three techniques for emitting clientside
event handlers.
Pages:
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463