Sử dụng điều khiển RadiobuttonList

Một phần của tài liệu Giáo trình ASP.NET cơ bản doc (Trang 114 - 116)

Điều khiển RadioButtonList cho phép hiển thị một danh sách các RadioButton mà có thể sắp xếp theo hướng ngang hay dọc, để ngừơi sử dụng có thể chọn một trong các Radiobutton đó.

Ví dụ: khi chúng ta cần thăm dò ý kiến khách hàng về một vấn đề gì đó chúng ta cần tạo một module bình chọn cho website của chúng ta.

Chúng ta sẽ thiết lập 1 bảng sau bảng tblSurveyAnswer pkAnswerID (int) sContent (nvarchar(100)) iVote (int) iPosition (int)

chúng ta sẽ tạo một trang radiobuttonlist.aspx

<%@Page Language="C#"AutoEventWireup="true"CodeFile="radiobuttonlist.aspx.cs"

Inherits="radiobuttonlist" %>

<!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<htmlxmlns="http://www.w3.org/1999/xhtml"> <headrunat="server">

<title>RadioButtonList</title>

<styletype="Text/css">

body{background-color:#e5e5e5}

#navcontain{width:399px;Height:299px;Background-color:white;margin:0pxauto; padding:15px15px15px15px;}

A:link{COLOR: #31659C; TEXT-DECORATION: none;} A:visited{COLOR: #31659C; TEXT-DECORATION: none;} A:active{COLOR: #FC8800; TEXT-DECORATION: none;} A:hover{COLOR: #FC8800; TEXT-DECORATION: none;} </style>

</head> <body>

<formid="form1"runat="server">

<divid="navcontain">

<b>Bạn biết đến TonghopIT qua:</b><br/>

<asp:RadioButtonListID="RadioButtonList1"runat="server">

</asp:RadioButtonList><hr/>

<asp:LinkButton ID="lbnVote"OnClick="lbnVote_Click"Text="Vote"runat="server"/>

<hr/>

Bạn chọn: <asp:Labelrunat="server" ID="lblResult" />

</div> </form> </body> </html> Trang radiobuttonlist.aspx.cs Code 10.5 using System; using TonghopIT.Library;

publicpartialclassradiobuttonlist : System.Web.UI.Page

{

protectedvoid Page_Load(object sender, EventArgs e) {

if (!IsPostBack) {

ListControlHelper.fillRadioButtonList(RadioButtonList1, "tblSurveyAnswer",

"sContent", "pkAnswerID"); }

}

protectedvoid lbnVote_Click(object sender, EventArgs e) {

this.lblResult.Text = RadioButtonList1.SelectedItem.Text + "<br> và giá trị của nó là:" + RadioButtonList1.SelectedItem.Value;

} }

Bạn thấy ở Code 9.6 lớp radiobuttonlist.aspx.cs cớ nhập khẩu gói TonghopIT.Library có sử dụng phương thức fillRadioButtonList từ lớp ListControlHelper với 4 đối số tương ứng như code ở cuối chương trình

Kết xuất của chương trình

Hình 3

Một phần của tài liệu Giáo trình ASP.NET cơ bản doc (Trang 114 - 116)

Tải bản đầy đủ (PDF)

(183 trang)