更新 : 2007 年 11 月
Web ページにプッシュ ボタン コントロールを表示します。
名前空間 :
System.Web.UI.WebControls アセンブリ :
System.Web (System.Web.dll 内)
<AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
<AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
Public Class Button _
Inherits WebControl _
Implements IButtonControl, IPostBackEventHandler
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public class Button : WebControl, IButtonControl,
IPostBackEventHandler
[AspNetHostingPermissionAttribute(SecurityAction::LinkDemand, Level = AspNetHostingPermissionLevel::Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction::InheritanceDemand, Level = AspNetHostingPermissionLevel::Minimal)]
public ref class Button : public WebControl,
IButtonControl, IPostBackEventHandler
/** @attribute AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal) */
/** @attribute AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal) */
public class Button extends WebControl implements IButtonControl,
IPostBackEventHandler
public class Button extends WebControl implements IButtonControl, IPostBackEventHandler
Button コントロールを使用して、Web ページにプッシュ ボタンを作成します。送信ボタンまたはコマンド ボタンを作成できます。
既定では、Button コントロールは送信ボタンです。送信ボタンに関連付けられているコマンド名 (CommandName プロパティで指定) はありません。このボタンは Web ページをサーバーにポストバックするだけです。Click イベントのイベント ハンドラを作成して、送信ボタンがクリックされたときに実行されるアクションをプログラムにより制御できます。
コマンド ボタンの場合は、CommandName プロパティを設定することにより、Sort などのコマンド名を関連付けることができます。コマンド名を設定すると、Web ページに複数の Button コントロールを作成し、どの Button コントロールがクリックされたかをプログラムによって確認できます。コマンド ボタンの場合は、CommandArgument プロパティを使用して、実行するコマンドに関して昇順などの追加情報も指定できます。Command イベントのイベント ハンドラを作成して、コマンド ボタンがクリックされたときに実行されるアクションをプログラムによって制御できます。
既定では、Button コントロールがクリックされたときにページ検証を実行します。ページ検証は、ページ上にある検証コントロールに関連付けられたすべての入力コントロールが、その検証コントロールによって指定されている検証規則に準拠しているかどうかを判断します。ページ検証を実行しないようにするには、CausesValidation プロパティを false に設定します。
ユーザー補助
このコントロールに既定でレンダリングされるマークアップは、Web Content Accessibility Guidelines (WCAG) 1.0 の優先度 1 ガイドラインなどのユーザー補助に関する標準に適合しない可能性があります。このコントロールのユーザー補助サポートの詳細については、「ASP.NET コントロールとユーザー補助」を参照してください。
Web ページの内容をサーバーにポストバックする、送信 Button コントロールを作成する方法を次のコード例に示します。
<%@ Page Language="VB" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head>
<title>Button Example</title>
<script language="VB" runat="server">
Sub SubmitBtn_Click(sender As Object, e As EventArgs)
Message.Text = "Hello World!!"
End Sub 'SubmitBtn_Click
</script>
</head>
<body>
<form id="form1" runat="server">
<h3>Button Example</h3>
Click on the submit button.<br /><br />
<asp:Button id="Button1"
Text="Submit"
OnClick="SubmitBtn_Click"
runat="server"/>
<br />
<asp:label id="Message" runat="server"/>
</form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head>
<title>Button Example</title>
<script language="C#" runat="server">
void SubmitBtn_Click(Object sender, EventArgs e)
{
Message.Text="Hello World!!";
}
</script>
</head>
<body>
<form id="form1" runat="server">
<h3>Button Example</h3>
Click on the submit button.<br /><br />
<asp:Button id="Button1"
Text="Submit"
OnClick="SubmitBtn_Click"
runat="server"/>
<br />
<asp:label id="Message" runat="server"/>
</form>
</body>
</html>
<%@ Page Language="JScript" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head>
<title>Button Example</title>
<script language="JScript" runat="server">
function SubmitBtn_Click(sender : Object, e : EventArgs)
{
Message.Text="Hello World!!";
}
</script>
</head>
<body>
<form id="form1" runat="server">
<h3>Button Example</h3>
Click on the submit button.<br /><br />
<asp:Button id="Button1"
Text="Submit"
OnClick="SubmitBtn_Click"
runat="server"/>
<br />
<asp:label id="Message" runat="server"/>
</form>
</body>
</html>
リストを並べ替えるコマンド Button コントロールを作成する方法を次のコード例に示します。
<%@ Page Language="VB" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head runat="server">
<title>Button CommandName Example</title>
<script runat="server">
Sub CommandBtn_Click(sender As Object, e As CommandEventArgs)
Select e.CommandName
Case "Sort"
' Call the method to sort the list.
Sort_List(CType(e.CommandArgument, String))
Case "Submit"
' Display a message for the Submit button being clicked.
Message.Text = "You clicked the Submit button"
' Test whether the command argument is an empty string ("").
If CType(e.CommandArgument , String) = "" Then
' End the message.
Message.Text &= "."
Else
' Display an error message for the command argument.
Message.Text &= ", however the command argument is not recogized."
End If
Case Else
' The command name is not recognized. Display an error message.
Message.Text = "Command name not recogized."
End Select
End Sub
Sub Sort_List(commandArgument As String)
Select commandArgument
Case "Ascending"
' Insert code to sort the list in ascending order here.
Message.Text = "You clicked the Sort Ascending button."
Case "Descending"
' Insert code to sort the list in descending order here.
Message.Text = "You clicked the Sort Descending button."
Case Else
' The command argument is not recognized. Display an error message.
Message.Text = "Command argument not recogized."
End Select
End Sub
</script>
</head>
<body>
<form id="form1" runat="server">
<h3>Button CommandName Example</h3>
Click on one of the command buttons.
<br /><br />
<asp:Button id="Button1"
Text="Sort Ascending"
CommandName="Sort"
CommandArgument="Ascending"
OnCommand="CommandBtn_Click"
runat="server"/>
<asp:Button id="Button2"
Text="Sort Descending"
CommandName="Sort"
CommandArgument="Descending"
OnCommand="CommandBtn_Click"
runat="server"/>
<br /><br />
<asp:Button id="Button3"
Text="Submit"
CommandName="Submit"
OnCommand="CommandBtn_Click"
runat="server"/>
<asp:Button id="Button4"
Text="Unknown Command Name"
CommandName="UnknownName"
CommandArgument="UnknownArgument"
OnCommand="CommandBtn_Click"
runat="server"/>
<asp:Button id="Button5"
Text="Submit Unknown Command Argument"
CommandName="Submit"
CommandArgument="UnknownArgument"
OnCommand="CommandBtn_Click"
runat="server"/>
<br /><br />
<asp:Label id="Message" runat="server"/>
</form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head runat="server">
<title>Button CommandName Example</title>
<script runat="server">
void CommandBtn_Click(Object sender, CommandEventArgs e)
{
switch(e.CommandName)
{
case "Sort":
// Call the method to sort the list.
Sort_List((String)e.CommandArgument);
break;
case "Submit":
// Display a message for the Submit button being clicked.
Message.Text = "You clicked the Submit button";
// Test whether the command argument is an empty string ("").
if((String)e.CommandArgument == "")
{
// End the message.
Message.Text += ".";
}
else
{
// Display an error message for the command argument.
Message.Text += ", however the command argument is not recogized.";
}
break;
default:
// The command name is not recognized. Display an error message.
Message.Text = "Command name not recogized.";
break;
}
}
void Sort_List(string commandArgument)
{
switch(commandArgument)
{
case "Ascending":
// Insert code to sort the list in ascending order here.
Message.Text = "You clicked the Sort Ascending button.";
break;
case "Descending":
// Insert code to sort the list in descending order here.
Message.Text = "You clicked the Sort Descending button.";
break;
default:
// The command argument is not recognized. Display an error message.
Message.Text = "Command argument not recogized.";
break;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<h3>Button CommandName Example</h3>
Click on one of the command buttons.
<br /><br />
<asp:Button id="Button1"
Text="Sort Ascending"
CommandName="Sort"
CommandArgument="Ascending"
OnCommand="CommandBtn_Click"
runat="server"/>
<asp:Button id="Button2"
Text="Sort Descending"
CommandName="Sort"
CommandArgument="Descending"
OnCommand="CommandBtn_Click"
runat="server"/>
<br /><br />
<asp:Button id="Button3"
Text="Submit"
CommandName="Submit"
OnCommand="CommandBtn_Click"
runat="server"/>
<asp:Button id="Button4"
Text="Unknown Command Name"
CommandName="UnknownName"
CommandArgument="UnknownArgument"
OnCommand="CommandBtn_Click"
runat="server"/>
<asp:Button id="Button5"
Text="Submit Unknown Command Argument"
CommandName="Submit"
CommandArgument="UnknownArgument"
OnCommand="CommandBtn_Click"
runat="server"/>
<br /><br />
<asp:Label id="Message" runat="server"/>
</form>
</body>
</html>
System..::.Object
System.Web.UI..::.Control
System.Web.UI.WebControls..::.WebControl
System.Web.UI.WebControls..::.Button
この型のすべてのパブリック static (Visual Basic では Shared) メンバは、スレッド セーフです。インスタンス メンバの場合は、スレッド セーフであるとは限りません。
Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
.NET Framework および .NET Compact Framework では、各プラットフォームのすべてのバージョンはサポートしていません。サポートされているバージョンについては、「.NET Framework システム要件」を参照してください。
.NET Framework
サポート対象 : 3.5、3.0、2.0、1.1、1.0
参照
その他の技術情報