Ngôn ngữ ASP.NET - HtmlInputRadioButton

Điều khiển HtmlInputRadioButton

HtmlInputRadioButton được sử dụng để điều khiển thẻ <input type=”radio”>. Trong HTML, thẻ này được sử dụng để tạo một radiobutton.
Các thuộc tính.

Thuộc tính Mô tả
Attributes Trả về tất cả tên thuộc tính và giá trị tương ứng của thuộc thẻ
Checked Giá trị boolean xác định thẻ có được chọn hay không
Disabled Giá trị boolean xác định control có bị disabled hay không. Mặc định là false
Id Id duy nhất của control
Name Tên của thẻ
Runat Xác định rằng control này là server control. Phải được xác định là “server”
Style Xác định hay trả về thuộc tính CSS được áp dụng cho control
TagName Trả về tên của thẻ
Type Loại thẻ
Value Giá trị của thẻ
Visible Giá trị boolean xác định control sẽ được hiển thị hay không.

Ví dụ: Tạo trang web HtmlInputRadioButton.aspx

Code trang HtmlInputRadioButton.aspx:
 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="HtmlInputRadioButton.aspx.cs" Inherits="htmlcontrol_HtmlInputRadioButton" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <body>
<form id="Form1" runat="server">
<p>Chọn màu bạn ưa thích:
<br />
<input id="r1" name="col" type="radio" runat="server" >Đỏ</input>
<br />
<input id="r2" name="col" type="radio" runat="server">Xanh lục</input>
<br />
<input id="r3" name="col" type="radio" runat="server">Xanh dương</input>
<br />
<input id="Button1" type="button" value="Submit" OnServerClick="submit" runat="server"/>
<p id="p1" runat="server" />
</form>

</body>
</html>

Tạo behind trang HtmlInputRadioButton.aspx:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class htmlcontrol_HtmlInputRadioButton : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void submit(object sender, EventArgs e)
    {
        string sColor = ""; if (r1.Checked)
            sColor = "Đỏ";
        else if (r2.Checked)
            sColor = "Xanh lục";
        else
            sColor = "Xanh dương";

        p1.InnerHtml = "Bạn thích màu: " + sColor;
    }

}

Giao diện của chúng ta có 3 HtmlInputRadioButton, một HtmlInputButton. Khi button submit được click thì hàm xử lý sự kiện click của nó được thực thi, nếu radiobutton nào được chọn thì màu tương ứng đó sẽ được hiển thị lên thông qua thẻ p như hình dưới đây :