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

Một phần của tài liệu Cấu trúc ASP NET framwork và cơ bản về c# (Trang 119 - 121)

Đ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

Code 9.4

<%@ Page Language="C#" AutoEventWireup="true"

CodeFile="radiobuttonlist.aspx.cs" Inherits="radiobuttonlist" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>RadioButtonList</title>

<style type="Text/css">

body{background-color:#e5e5e5}

#navcontain{width:399px;Height:299px;Background- color:white;margin:0px auto; padding:15px 15px 15px 15px;} 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>

<form id="form1" runat="server">

<div id="navcontain">

<b>Bạn biết đến Điễn đàn Sinh viên Hoa Sen qua:</b><br />

<asp:RadioButtonList ID="RadioButtonList1" runat="server">

</asp:RadioButtonList><hr />

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

<hr />

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

</div>

</form> </body>

</html>

Trang radiobuttonlist.aspx.csCode 9.5 Code 9.5

using System;

using Website.Library;

public partial class radiobuttonlist : System.Web.UI.Page {

protected void Page_Load(object sender, EventArgs e) {

if (!IsPostBack) { (adsbygoogle = window.adsbygoogle || []).push({});

ListControlHelper.fillRadioButtonList(RadioButtonList1,

"tblSurveyAnswer", "sContent", "pkAnswerID"); }

}

protected void 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 Website.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

Một phần của tài liệu Cấu trúc ASP NET framwork và cơ bản về c# (Trang 119 - 121)