1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
|
//------------------------------------------------------------------------------
// <copyright file="Button.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
namespace System.Web.UI.WebControls {
using System;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Drawing.Design;
using System.Web;
using System.Web.UI;
using System.Web.Util;
/// <devdoc>
/// <para>Represents a Windows button control.</para>
/// </devdoc>
[
DataBindingHandler("System.Web.UI.Design.TextDataBindingHandler, " + AssemblyRef.SystemDesign),
DefaultEvent("Click"),
DefaultProperty("Text"),
Designer("System.Web.UI.Design.WebControls.ButtonDesigner, " + AssemblyRef.SystemDesign),
ToolboxData("<{0}:Button runat=\"server\" Text=\"Button\"></{0}:Button>"),
SupportsEventValidation
]
public class Button : WebControl, IButtonControl, IPostBackEventHandler {
private static readonly object EventClick = new object();
private static readonly object EventCommand = new object();
/// <devdoc>
/// <para>Initializes a new instance of the <see cref='System.Web.UI.WebControls.Button'/> class.</para>
/// </devdoc>
public Button() : base(HtmlTextWriterTag.Input) {
}
/// <devdoc>
/// <para>Gets or sets whether pressing the button causes page validation to fire. This defaults to True so that when
/// using validation controls, the validation state of all controls are updated when the button is clicked, both
/// on the client and the server. Setting this to False is useful when defining a cancel or reset button on a page
/// that has validators.</para>
/// </devdoc>
[
DefaultValue(true),
Themeable(false),
WebCategory("Behavior"),
WebSysDescription(SR.Button_CausesValidation),
]
public virtual bool CausesValidation {
get {
object b = ViewState["CausesValidation"];
return((b == null) ? true : (bool)b);
}
set {
ViewState["CausesValidation"] = value;
}
}
/// <devdoc>
/// <para>Gets or sets the command associated with a <see cref='System.Web.UI.WebControls.Button'/> propogated in the <see langword='Command'/> event along with the <see cref='System.Web.UI.WebControls.Button.CommandArgument'/>
/// property.</para>
/// </devdoc>
[
DefaultValue(""),
Themeable(false),
WebCategory("Behavior"),
WebSysDescription(SR.WebControl_CommandName),
]
public string CommandName {
get {
string s = (string)ViewState["CommandName"];
return((s == null) ? String.Empty : s);
}
set {
ViewState["CommandName"] = value;
}
}
/// <devdoc>
/// <para>Gets or sets the property propogated in
/// the <see langword='Command'/> event with the associated <see cref='System.Web.UI.WebControls.Button.CommandName'/>
/// property.</para>
/// </devdoc>
[
Bindable(true),
DefaultValue(""),
Themeable(false),
WebCategory("Behavior"),
WebSysDescription(SR.WebControl_CommandArgument),
]
public string CommandArgument {
get {
string s = (string)ViewState["CommandArgument"];
return((s == null) ? String.Empty : s);
}
set {
ViewState["CommandArgument"] = value;
}
}
/// <devdoc>
/// The script that is executed on a client-side click.
/// </devdoc>
[
DefaultValue(""),
Themeable(false),
WebCategory("Behavior"),
WebSysDescription(SR.Button_OnClientClick),
]
public virtual string OnClientClick {
get {
string s = (string)ViewState["OnClientClick"];
if (s == null) {
return String.Empty;
}
return s;
}
set {
ViewState["OnClientClick"] = value;
}
}
[
DefaultValue(""),
Editor("System.Web.UI.Design.UrlEditor, " + AssemblyRef.SystemDesign, typeof(UITypeEditor)),
Themeable(false),
UrlProperty("*.aspx"),
WebCategory("Behavior"),
WebSysDescription(SR.Button_PostBackUrl),
]
public virtual string PostBackUrl {
get {
string s = (string)ViewState["PostBackUrl"];
return s == null? String.Empty : s;
}
set {
ViewState["PostBackUrl"] = value;
}
}
/// <devdoc>
/// <para>Gets or sets the text caption displayed on the <see cref='System.Web.UI.WebControls.Button'/> .</para>
/// </devdoc>
[
Bindable(true),
Localizable(true),
WebCategory("Appearance"),
DefaultValue(""),
WebSysDescription(SR.Button_Text),
]
public string Text {
get {
string s = (string)ViewState["Text"];
return((s == null) ? String.Empty : s);
}
set {
ViewState["Text"] = value;
}
}
/// <devdoc>
/// Whether the button should use the client's submit mechanism to implement its
/// behavior, or whether it should use the ASP.NET postback mechanism similar
/// to LinkButton. By default, it uses the browser's submit mechanism.
/// </devdoc>
[
DefaultValue(true),
Themeable(false),
WebCategory("Behavior"),
WebSysDescription(SR.Button_UseSubmitBehavior),
]
public virtual bool UseSubmitBehavior {
get {
object b = ViewState["UseSubmitBehavior"];
return ((b == null) ? true : (bool)b);
}
set {
ViewState["UseSubmitBehavior"] = value;
}
}
[
DefaultValue(""),
Themeable(false),
WebCategory("Behavior"),
WebSysDescription(SR.PostBackControl_ValidationGroup),
]
public virtual string ValidationGroup {
get {
string s = (string)ViewState["ValidationGroup"];
return((s == null) ? String.Empty : s);
}
set {
ViewState["ValidationGroup"] = value;
}
}
/// <devdoc>
/// <para>Occurs when the <see cref='System.Web.UI.WebControls.Button'/> is clicked.</para>
/// </devdoc>
[
WebCategory("Action"),
WebSysDescription(SR.Button_OnClick)
]
public event EventHandler Click {
add {
Events.AddHandler(EventClick, value);
}
remove {
Events.RemoveHandler(EventClick, value);
}
}
/// <devdoc>
/// <para>Occurs when the <see cref='System.Web.UI.WebControls.Button'/> is clicked.</para>
/// </devdoc>
[
WebCategory("Action"),
WebSysDescription(SR.Button_OnCommand)
]
public event CommandEventHandler Command {
add {
Events.AddHandler(EventCommand, value);
}
remove {
Events.RemoveHandler(EventCommand, value);
}
}
/// <internalonly/>
/// <devdoc>
/// <para>Adds the attributes of the <see cref='System.Web.UI.WebControls.Button'/> control to the output stream for rendering
/// on the client.</para>
/// </devdoc>
protected override void AddAttributesToRender(HtmlTextWriter writer) {
bool submitButton = UseSubmitBehavior;
// Make sure we are in a form tag with runat=server.
if (Page != null) {
Page.VerifyRenderingInServerForm(this);
}
if (submitButton) {
writer.AddAttribute(HtmlTextWriterAttribute.Type, "submit");
}
else {
writer.AddAttribute(HtmlTextWriterAttribute.Type, "button");
}
PostBackOptions options = GetPostBackOptions();
string uniqueID = UniqueID;
// Don't render Name on a button if __doPostBack is posting back to a different control
// because Page will register this control as requiring post back notification even though
// it's not the target of the postback. If the TargetControl isn't this control, this control's
// RaisePostBackEvent should never get called. See VSWhidbey 477095.
if (uniqueID != null && (options == null || options.TargetControl == this)) {
writer.AddAttribute(HtmlTextWriterAttribute.Name, uniqueID);
}
writer.AddAttribute(HtmlTextWriterAttribute.Value, Text);
//
bool effectiveEnabled = IsEnabled;
string onClick = String.Empty;
if (effectiveEnabled) {
// Need to merge the onclick attribute with the OnClientClick
// VSWhidbey 111791: Defensively add a ';' in case it is
// missing in user customized onClick value above.
onClick = Util.EnsureEndWithSemiColon(OnClientClick);
if (HasAttributes) {
string userOnClick = Attributes["onclick"];
if (userOnClick != null) {
onClick += Util.EnsureEndWithSemiColon(userOnClick);
Attributes.Remove("onclick");
}
}
if (Page != null) {
string reference = Page.ClientScript.GetPostBackEventReference(options, false);
if (reference != null) {
onClick = Util.MergeScript(onClick, reference);
}
}
}
if (Page != null) {
Page.ClientScript.RegisterForEventValidation(options);
}
if (onClick.Length > 0) {
writer.AddAttribute(HtmlTextWriterAttribute.Onclick, onClick);
if (EnableLegacyRendering) {
writer.AddAttribute("language", "javascript", false);
}
}
if (Enabled && !effectiveEnabled && SupportsDisabledAttribute) {
// We need to do the cascade effect on the server, because the browser
// only renders as disabled, but doesn't disable the functionality.
writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
}
base.AddAttributesToRender(writer);
}
protected virtual PostBackOptions GetPostBackOptions() {
PostBackOptions options = new PostBackOptions(this, String.Empty);
options.ClientSubmit = false;
if (Page != null) {
if (CausesValidation && Page.GetValidators(ValidationGroup).Count > 0) {
options.PerformValidation = true;
options.ValidationGroup = ValidationGroup;
}
if (!String.IsNullOrEmpty(PostBackUrl)) {
options.ActionUrl = HttpUtility.UrlPathEncode(ResolveClientUrl(PostBackUrl));
}
options.ClientSubmit = !UseSubmitBehavior;
}
return options;
}
/// <devdoc>
/// <para>Raises the <see langword='Click '/>event of a <see cref='System.Web.UI.WebControls.Button'/>
/// .</para>
/// </devdoc>
protected virtual void OnClick(EventArgs e) {
EventHandler handler = (EventHandler)Events[EventClick];
if (handler != null) handler(this,e);
}
/// <devdoc>
/// <para>Raises the <see langword='Command '/>event of a <see cref='System.Web.UI.WebControls.Button'/>
/// .</para>
/// </devdoc>
protected virtual void OnCommand(CommandEventArgs e) {
CommandEventHandler handler = (CommandEventHandler)Events[EventCommand];
if (handler != null)
handler(this,e);
// Command events are bubbled up the control heirarchy
RaiseBubbleEvent(this, e);
}
protected internal override void OnPreRender(EventArgs e) {
base.OnPreRender(e);
// VSWhidbey 489577
if (Page != null && IsEnabled) {
if ((CausesValidation && Page.GetValidators(ValidationGroup).Count > 0) ||
!String.IsNullOrEmpty(PostBackUrl)) {
Page.RegisterWebFormsScript();
}
else if (!UseSubmitBehavior) {
Page.RegisterPostBackScript();
}
}
}
/// <internalonly/>
/// <devdoc>
/// </devdoc>
protected internal override void RenderContents(HtmlTextWriter writer) {
// Do not render the children of a button since it does not
// make sense to have children of an <input> tag.
}
/// <internalonly/>
/// <devdoc>
/// <para>Raises events for the <see cref='System.Web.UI.WebControls.Button'/>
/// control on post back.</para>
/// </devdoc>
void IPostBackEventHandler.RaisePostBackEvent(string eventArgument) {
RaisePostBackEvent(eventArgument);
}
/// <internalonly/>
/// <devdoc>
/// <para>Raises events for the <see cref='System.Web.UI.WebControls.Button'/>
/// control on post back.</para>
/// </devdoc>
protected virtual void RaisePostBackEvent(string eventArgument) {
ValidateEvent(this.UniqueID, eventArgument);
if (CausesValidation) {
Page.Validate(ValidationGroup);
}
OnClick(EventArgs.Empty);
OnCommand(new CommandEventArgs(CommandName, CommandArgument));
}
}
}
|