//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//------------------------------------------------------------------------------
// HtmlForm.cs
//
namespace System.Web.UI.HtmlControls {
using System.ComponentModel;
using System;
using System.Collections;
using System.Globalization;
using System.IO;
using System.Text;
using System.Web.Configuration;
using System.Web.Util;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Security.Permissions;
///
///
/// The class defines the methods, properties, and
/// events for the HtmlForm control. This class provides programmatic access to the
/// HTML <form> element on the server.
///
///
public class HtmlForm : HtmlContainerControl {
private string _defaultFocus;
private string _defaultButton;
private bool _submitDisabledControls;
private const string _aspnetFormID = "aspnetForm";
///
///
public HtmlForm()
: base("form") {
}
[
WebCategory("Behavior"),
DefaultValue(""),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
]
public string Action {
get {
string s = Attributes["action"];
return ((s != null) ? s : String.Empty);
}
set {
Attributes["action"] = MapStringAttributeToString(value);
}
}
///
/// Gets or sets default button for the form
///
[
WebCategory("Behavior"),
DefaultValue(""),
]
public string DefaultButton {
get {
if (_defaultButton == null) {
return String.Empty;
}
return _defaultButton;
}
set {
_defaultButton = value;
}
}
///
/// Gets or sets default focused control for the form
///
[
WebCategory("Behavior"),
DefaultValue(""),
]
public string DefaultFocus {
get {
if (_defaultFocus == null) {
return String.Empty;
}
return _defaultFocus;
}
set {
_defaultFocus = value;
}
}
/*
* Encode Type property.
*/
///
///
/// Gets or sets the Enctype attribute of the form. This is
/// the encoding type that browsers
/// use when posting the form's data to the server.
///
///
[
WebCategory("Behavior"),
DefaultValue(""),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
]
public string Enctype {
get {
string s = Attributes["enctype"];
return ((s != null) ? s : String.Empty);
}
set {
Attributes["enctype"] = MapStringAttributeToString(value);
}
}
/*
* Method property.
*/
///
///
/// Gets or sets the Method attribute for the form. This defines how a browser
/// posts form data to the server for processing. The two common methods supported
/// by all browsers are GET and POST.
///
///
[
WebCategory("Behavior"),
DefaultValue(""),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
]
public string Method {
get {
string s = Attributes["method"];
return ((s != null) ? s : "post");
}
set {
Attributes["method"] = MapStringAttributeToString(value);
}
}
/*
* Name property.
*/
///
///
/// Gets the value of the HTML Name attribute that will be rendered to the
/// browser.
///
///
[
WebCategory("Appearance"),
DefaultValue(""),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
]
public virtual string Name {
get {
return UniqueID;
}
set {
// no-op setter to prevent the name from being set
}
}
///
/// If true, forces controls disabled on the client to submit their values (thus preserving their previous postback state)
///
[
WebCategory("Behavior"),
DefaultValue(false)
]
public virtual bool SubmitDisabledControls {
get {
return _submitDisabledControls;
}
set {
_submitDisabledControls = value;
}
}
/*
* Target property.
*/
///
///
/// Gets or sets the Uri of the frame or window to render the results of a Form
/// POST request. Developers can use this property to redirect these results to
/// another browser window or frame.
///
///
[
WebCategory("Behavior"),
DefaultValue(""),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
]
public string Target {
get {
string s = Attributes["target"];
return ((s != null) ? s : String.Empty);
}
set {
Attributes["target"] = MapStringAttributeToString(value);
}
}
///
/// Overridden to return a constant value or tack the ID onto the same constant value.
/// This fixes a bug in PocketPC which doesn't allow the name and ID of a form to be different
///
public override string UniqueID {
get {
if (NamingContainer == Page) {
return base.UniqueID;
}
else if (this.EffectiveClientIDMode != ClientIDMode.AutoID) {
return ID ?? _aspnetFormID;
}
return _aspnetFormID;
}
}
public override string ClientID {
get {
if (this.EffectiveClientIDMode != ClientIDMode.AutoID) {
return ID;
}
return base.ClientID;
}
}
protected internal override void Render(HtmlTextWriter output) {
Page p = Page;
if (p == null)
throw new HttpException(SR.GetString(SR.Form_Needs_Page));
#pragma warning disable 0618 // To avoid deprecation warning
if (p.SmartNavigation) {
#pragma warning restore 0618
((IAttributeAccessor)this).SetAttribute("__smartNavEnabled", "true");
// Output the IFrame
StringBuilder sb = new StringBuilder("");
output.WriteLine(sb.ToString());
}
base.Render(output);
}
private string GetActionAttribute() {
// If the Action property is nonempty, we use it instead of the current page. This allows the developer
// to support scenarios like PathInfo, UrlMapping, etc. (DevDiv Bugs 164390)
string actionProperty = Action;
if (!String.IsNullOrEmpty(actionProperty)) {
return actionProperty;
}
string action;
VirtualPath clientFilePath = Context.Request.ClientFilePath;
// ASURT 15075/11054/59970: always set the action to the current page.
// DevDiv Servicing 215795/Dev10 567580: The IIS URL Rewrite module and other rewrite
// scenarios need the postback action to be the original URL. Note however, if Server.Transfer/Execute
// is used, the action will be set to the transferred/executed page, that is, the value of
// CurrentExecutionFilePathObject. This is because of ASURT 59970 and the document attached to
// that bug, which indirectly states that things should behave this way when Transfer/Execute is used.
if (Context.ServerExecuteDepth == 0) {
// There hasn't been any Server.Transfer or RewritePath.
// ASURT 15979: need to use a relative path, not absolute
action = clientFilePath.VirtualPathString;
int iPos = action.LastIndexOf('/');
if (iPos >= 0) {
// Ensure the segment is always a relative path, so prepend a dot-segment
// (RFC section 4.2 Relative Reference)
action = "./" + action.Substring(iPos + 1);
}
}
else {
VirtualPath currentFilePath = Context.Request.CurrentExecutionFilePathObject;
// Server.Transfer or RewritePath case. We need to make the form action relative
// to the original ClientFilePath (since that's where the browser thinks we are).
currentFilePath = clientFilePath.MakeRelative(currentFilePath);
action = currentFilePath.VirtualPathString;
}
// VSWhidbey 202380: If cookieless is on, we need to add the app path modifier to the form action
bool cookieless = CookielessHelperClass.UseCookieless(Context, false, FormsAuthentication.CookieMode);
if (cookieless && Context.Request != null && Context.Response != null) {
action = Context.Response.ApplyAppPathModifier(action);
}
// Dev11 406986: