Các điều khiển khác
Sử dụng ListControl Trong chương này các bạn sẽ được học các điều khiển trình bày danh sách như DropDownList, RadioButtonList… và kết thúc chương các bạn sẽ được học 1 cách chi tiết để sử dụng các List Control này tạo một Module bình chọn cho ...
Sử dụng ListControl
Trong chương này các bạn sẽ được học các điều khiển trình bày danh sách như DropDownList, RadioButtonList… và kết thúc chương các bạn sẽ được học 1 cách chi tiết để sử dụng các List Control này tạo một Module bình chọn cho trang web của bạn
Điểm chung cho tất cả các điều khiển danh sách là nó gồm 3 thuộc tính chính
Bạn có thể đưa dữ liệu vào DropDownList từ một mảng danh sách hoặc dữ liệu từ một cơ sở dữ liệu:
Thuộc tính quan trọng
DataSource: chỉ đến nguồn dữ liệu
DataTextField: trường dữ liệu được hiển thị
DataValueField: trường dữ liệu thiết lập giá trị với tương ứng với Text hiển thị
Phương thức OnSelectedIndexChanged
Xảy ra khi người dùng thay đổi lựa chọn phần tử trên DropDownList
Cho phép hiển thị một danh sách các lựa chọn, nguời sử dụng chỉ chọn một lựa chọn 1 lần
Ví dụ:
Bạn tạo một lớp phục vụ đưa dữ liệu vào DropDownList như sau:
để sử dụng lớp này bạn tạo 1 trang aspx và trong phần code behind bạn nhập khẩu gói Website.Library, trong trong sự kiện Load của trang bạn gọi như sau
protected void Page_Load(object sender, EventArgs e)
{
DropdownListHelper.Fillcombobox(DropDownList1, "tblIntrodure", "sTitle", "pkIntrodureID");
}
Để sử dụng sự kiện OnSelectedIndexchanged bạn cần thêm vào cho DropDownList thuộc tính AutoPostBack và thiết lập cho nó giá trị là true
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="dropdownlist.aspx.cs" Inherits="_Default" %>
<!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>DropDownList</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_Changed" ID="DropDownList1" runat="server">
</asp:DropDownList><hr />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
Trang dropdownlist.aspx.cs
Code 9.3
using System;
using Website.Library;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropdownListHelper.Fillcombobox(DropDownList1, "tblIntrodure", "sTitle", "pkIntrodureID");
}
}
protected void DropDownList1_Changed(object sender, EventArgs e)
{
Label1.Text = "Text:" + DropDownList1.SelectedItem.Text + "<br>giá tri:" + DropDownList1.SelectedValue.ToString();
}
}
Đ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" %>
<!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{awidth: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.cs
using System;
using Website.Library;
public partial class radiobuttonlist : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
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
Nó là một điều khiển giống với DropDownList nhưng nó sẽ hiển thị một danh sách trên trang và chúng ta có thể lựa chọn nhiều phần tử một lúc với thuộc tính selectionMode với hai giá trị là Singer và Multiple.
Ví dụ sau mình sẽ đưa ra với một ListBox nhiều lựa chọn
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ListBox.aspx.cs" Inherits="ListBox" %>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div id="navcontain">
Điều khiển ListBox<br />
<asp:ListBox SelectionMode="Multiple" ID="listbox1" runat="server" /><br />
<asp:Button ID="btnChon" runat="server" Text="Chọn" OnClick="btnChon_Click" /><hr />
Bạn đã chọn: <asp:Label ID="lblresult" runat="server" />
</div>
</form>
</body>
</html>
ListBox.aspx.cs
using System;
using System.Collections;
using System.Web;
using System.Web.UI.WebControls;
using Website.Library;
public partial class ListBox : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
ListControlHelper.fillListBox(listbox1, "tblSurveyAnswer", "sContent", "pkAnswerID");
}
protected void btnChon_Click(object sender, EventArgs e)
{
lblresult.Text = "";
foreach (ListItem item in listbox1.Items)
{
if (item.Selected)
lblresult.Text += "<li>" + item.Text;
}
}
}
Kết xuất của chương trình