通用型(common)的伺服器控制項,指的是 Label 、 TextBox 、 Button 、 CheckBox 、 RadioButton 這類較基本功能的控制項。
The Label Control
這個控制項比較沒什麼好講的,值得一提的是,若由後置程式碼填資料到 Label 控制項,必項注意安全性問題。 因為它可能引發 XSS 資安弱點。 若要避免這個問題,可以使用 HttpUtility.HtmlEncode 或 Server.HtmlEncode 方法,在資料填入前先行編碼。
string script = @"<script>alert('test1');</script>";
Label1.Text = HttpUtility.HtmlEncode(script);
//碥碼後的文字: &lt;script&gt;alert(&#39;test&#39;);&lt;/script&gt;
The TextBox Control
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.
- Text :
- TextAlign :
- Checked :
- AutoPostBack :是否自動回傳。
- CheckedChanged :Event 發生於 Checked 屬性的值變更時。
若 AutoPostBack = false ,這個 CheckedChanged 事件就不會引發 PostBack ; 但是若其它控制項發生 PostBack 事件時, CheckChanged 事件還是會被執行。
<asp:CheckBox ID="CheckBox1" runat="server"
Text="是/否"
TextAlign="Left"
Checked="true"
AutoPostBack="false"
onCheckedChanged="CheckBox1_CheckedChanged"
/>
The RadioButton Control
The RadioButton control gives the user the ability to select between mutually exclusive RadioButton 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 controls together, specify the same GroupName for each RadioButton control in the group.
<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"
/>
沒有留言:
張貼留言