通用型(common)的伺服器控制項,指的是 LabelTextBoxButtonCheckBoxRadioButton 這類較基本功能的控制項。

The Label Control

這個控制項比較沒什麼好講的,值得一提的是,若由後置程式碼填資料到 Label 控制項,必項注意安全性問題。 因為它可能引發 XSS 資安弱點。 若要避免這個問題,可以使用 HttpUtility.HtmlEncodeServer.HtmlEncode 方法,在資料填入前先行編碼。

string script = @"<script>alert('test1');</script>";
Label1.Text = HttpUtility.HtmlEncode(script);

//碥碼後的文字: &amp;lt;script&amp;gt;alert(&amp;#39;test&amp;#39;);&amp;lt;/script&amp;gt;

The TextBox Control

  • TextMode :設定的行為模式 (SingleLine / MultiLine / Password)。
  • MaxLength :設定文字方塊中所允許的字元數目上限。
  • Wrap :設定多行文字方塊中的文字內容是否換行。(default=true)。

The Button Control

A Button control can also be used as a command button, which is one of a set of buttons that work together as a group, such as a toolbar. You define a button as a command button by assigning a value to its CommandName property. When a user clicks one of the command buttons, its Command event is called on the server. This event is passed an instance of CommandEventArgs as a parameter.

<asp:Button ID="Button4" runat="server" Text="<<" CommandName="previous"  oncommand="Play_Command" />
<asp:Button ID="Button5" runat="server" Text="●" CommandName="play"  oncommand="Play_Command" />
<asp:Button ID="Button6" runat="server" Text=">>" CommandName="next"  oncommand="Play_Command" />
protected void Play_Command(object sender, CommandEventArgs e)
{
switch (e.CommandName)
{
case "previous":
myMessage.Show(this, "Previous");
break;
case "play":
myMessage.Show(this, "Play");
break;
case "next":
myMessage.Show(this, "Next");
break;
}
}

The CheckBox Control

The CheckBox control gives the user the ability to select between true and false.


# The RadioButton Control

The [RadioButton](http://msdn.microsoft.com/zh-tw/library/system.web.ui.webcontrols.radiobutton.aspx) control gives the user the ability to select between mutually exclusive [RadioButton](http://msdn.microsoft.com/zh-tw/library/system.web.ui.webcontrols.radiobutton.aspx) controls in a group.   This is useful when you are asking a user to select a single item from a group of items.   To group multiple [RadioButton](http://msdn.microsoft.com/zh-tw/library/system.web.ui.webcontrols.radiobutton.aspx) controls together, specify the same [GroupName](http://msdn.microsoft.com/zh-tw/library/system.web.ui.webcontrols.radiobutton.groupname.aspx) for each [RadioButton](http://msdn.microsoft.com/zh-tw/library/system.web.ui.webcontrols.radiobutton.aspx) control in the group.  
```xml
<asp:RadioButton ID="RadioButton1" runat="server" GroupName="Fruit" Text="Apple" /><br />
<asp:RadioButton ID="RadioButton2" runat="server" GroupName="Fruit" Text="Melon" /><br />
<asp:RadioButton ID="RadioButton3" runat="server" GroupName="Fruit" 
Text="Tangerine" 
TextAlign="Right" 
Checked="true"
AutoPostBack="true" 
oncheckedchanged="RadioButton1_CheckedChanged"
/>

ListBox

DropDownList

CheckBoxList

BulletedList

RadioButtonList

AdRotator