You are on page 1of 40

1

LP TRNH WEB (Lin Thng) TI LIU THAM KHO 1. Xy dng v trin khai ng dng thng mi in t. Tp 2: vi cng c ASP.Net, VB.Net, SQL Server. Phm Hu Khang. 2. www.w3schools.com 3. www.asp.net 4. www.asp.net/learn/videos/ (T nghin cu) 5. Wrox.Professional.ASP.NET.2.0.Nov.2005.eBook-LinG.pdf 6. (ASP.NET2.0) Visual Web Developer 2005 Express Edition For Dummies (2005, Wiley).pdf . N MN HC Lm theo nhm, mi nhm 2 hoc 3 sinh vin. Yu cu: Xy dng website CA HNG GII THIU SCH V NHN T MUA SCH vi y cc chc nng c bn. Cc chc nng chnh: 1. Gii thiu v tm kim: Nh cung cp, Sch (kt qu xut theo nhiu cch hp l, p, phn trang, sp xp, tm kim theo nhiu tiu ch ...) 2. t mua hng 3. Thm Nh cung cp, Sch (nh dng trong textarea, kim tra thng tin hp l, thm tp tin), xa (nhiu cch), sa. 4. Thng k n t hng theo ngy Phn quyn: Cc chc nng 3, 4 phi ng nhp mi thc hin c. Lu : mi loi thng tin phi c t nht 10 dng d liu, d liu c ni dung thc t. C th lm thm mt s chc nng khc. Cch chm im: cc thnh vin ca nhm phi nm vng tt c nhng phn trong bo co ca nhm. Nu 1 thnh vin khng tr li c cu hi lin quan n nhng ni dung bo co trong n, c nhm s b tr im. Np bi trong ngy thi mn hc theo mu (MauBaoCaoDeTaiWeb.doc).

MT S THUT NG THNG DNG WebSite: tp hp cc trang Web ca mt t chc hay c nhn, c mt ch no . WebPage: l trang Web, c th hin th thng tin di dng: text, image, video HomePage: l trang Web u tin c hin th trong mt Website. ( Thng c tn: index, default )

HyperLink: cc mi lin kt gia cc trang Web. (3 loi, trang web nn c nhiu lin kt, trang web khng c l ng ct) URL (Uniform Resource Locator): ng dn ch ti mt tp tin trong mt my ch trn Internet Protocol: giao thc truy cp (http, ftp ) Domain name: tn vng ca ngun ti nguyn. File name: tn file v ng dn nu c. V d: http://www.yahoo.com/email/beta.htm Nu lin kt gia cc trang trn cng mt WebSite th ch cn File name HTTP (port 80: HyperText Transfer Protocol) FTP (20:data transfer, 21: command) File Transfer Protocol SMTP (25: Simple Mail Transfer Protocol) POP3 (Post Office Protocol Version 3) IAP (Internet Access Provider): nh cung cp ng truyn Internet. ISP (Internet Service Provider): nh cung cp dch v Internet. INTRANET: l mng s dng ni b (LAN hoc WAN) dng chia s thng tin ring trong mt t chc nhng cng c th ni vi cc trang Web trn Internet. Web Server: my lu tr v cung cp thng tin dng Web Cc phn mm Web Server - Personal Web Server (PWS Win98) - Internet Information Server (IIS5.0 Win 2000, IIS 5.1 Win XP) - Apache - Netscape Enterprise server - . Web Client: my truy xut v hin th thng tin dng Web truy xut cc thng tin trn Web Server, cc Web Client phi s dng mt chng trnh duyt cc thng tin ny gi l Web Browser (trnh duyt Web) Cc trnh duyt Web - Internet Explorer (tch hp t Win98) - Netscape Navigator - Opera - Trang web tnh (static web pages) Trang web ng (dynamic web pages)

Trang Web ng ngha l trang Web c kh nng tng tc (interact) vi ngi truy cp trang Web . Mt trong nhng phng tin to ra trang Web ng l ngn ng script. HOT NG CA DCH V WEB Dch v web hot ng theo m hnh Client/Server: khi mt Web Browser gi mt yu cu (Request) n Web Server, Web Server nhn yu cu v gi kt qu tr li li cho Web Browser (Response). Web Browser yu cu thng tin t Web Server bng cch gi mt a ch URL n Web Server. Web Server tr li cho Web Browser kt qu l mt trang .html. Trang .html c th l mt trang c sn trn Web Server hoc c th l kt qu x l ca mt trang web ng trn Web Server.

Lp trnh pha Client (Client-Side) on lnh c thi hnh ti client. Gim bt ti trng cho webserver. Gim bt thi gian x l trang web cho server. Thc thi p ng mt s kin xy ra. (JavaScript, VBScript, Java Applets, Flash) Hot ng ca web ng Client-Side

HTTP Request (http://www.website.com)


WEB CLIENT

WEB SERVER

HTTP Response Browser processes instructions and turns them into HTML and displays page <HTML> <B> Hello </B> </HTML>

WebServer locates HTML and instructions file

Hello
4

Hot ng ca web ng Server-Side Lp trnh pha Server (Server-Side) on lnh c thi hnh ti server. Khng ph thuc vo trnh duyt ti client. Ngi dng khng th thy c m lnh. Lin kt c s d liu, cc tp tin khc. (CGI, ASP, ASP.NET, JSP, PHP, )

WEB SERVER

HTTP Request (http://www.website.com)


WEB CLIENT

HTTP Response Browser processes HTML and displays page Hello

WebServer locates instructions file and processes instructions to create HTML

<HTML> <B> Hello </B> </HTML>

<HTML> <% Response.Write(Hello ) %> </HTML>

Chng 1 GII THIU ASP.NET Gii thiu Asp.net la cng ngh x ly web phia may chu, c lp vi moi trinh duyt, Ky thut ap dung la cho phep moi thc thi c thc hin trinh chu, tc la trinh chu phai x ly nhiu vn cung luc cho nhiu ngi dung trinh chu phai co cu hinh manh va bng thng tt Tng thich vi asp 3.0 Thit k ma theo asp.net Trinh t thc hin bin c: Page_Init Page_Load Control Speciifc Event Page_Unload. Gia webform va code behind co quan h k tha, u moi trang webform ta se thy co chi dn khai bao nh sau: <%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="sample.WebForm1"%> Ngn ng lp trnh Visual Basic.Net (vb), JScript.Net (js), C Sharp (cs). Mc nh l ngn ng VB, c th thay i ngn ng mc nh trong tp tin machine.config nh sau <compilation debug=false explicit=true defaultLanguage=cs> Net Frameword cho php s dng 3 ngn ng trong 1 ng dng, ngha l c th s dng mi trang web mt loi ngn ng. ViewState L nh danh dng lu trng thi ca mi WebForm do Server qun l, cha tt c cc trng thi ca iu khin ngi dng trong trang. Khi tr v cho trnh khch, viewstate c trnh by di dng th hidden v c gi tr c m ha. C th v hiu ha hay cho php viewstate bng cch s dng thuc tnh EnableViewState trong tng th hay trong trang cu hnh ca ng dng. IsPostBack Thuc tnh ca Page, tr v gi tr False khi trang c np ln u tin, tr v gi tr True trong cc ln np tip theo. V d: Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not IsPostBack Then cc cu lnh ch mun thc thi 1 ln khi trang c np ln u tin End If End Sub (Ti liu tham kho s 1 - Chng 1,2)

Chng 2 MT S IM LU Xem li VB.NET Lp trnh hng i tng i tng: thuc tnh, phng thc, s kin. Khai bo i tng Class <tn i tng> Public/Protected/Private <thuc tnh> as <kiu d liu> Public/Protected/Private Sub <tn phng thc> (<tham s>) Public/Protected/Private Function <tn phng thc> (<tham s>) as <kiu tr v> (gn tr tr v vo tn phng thc/return) End class Public: mi ni trong chng trnh u thy v dng c. Protected: trong class v cc chng trnh con ca n thy v dng c. (inherits: con->k tha) Private: ch trong class thy v dng c. Trong cng class cc hm phi khc tn hoc khc kiu tham s hoc khc s lng tham s. V d: Nu c: Public sub New(Ten as String) Th sai Public sub New(Ho as String) Th ng Public sub New(Luong as Double) Th ng Public sub New(Ten as String, Luong as Double) Nu c: Public sub New(Ten as String, Luong as Double) Th sai Public sub New(Ten as String, Optional Luong as Double=10) Khi vit trng li tn th: Cha l: overrideable Con l: overrides Chng ta khai bo thuc tnh l Private bn ngoi khng gn trc tip gi tr cho thuc tnh c m phi thng qua Property ca Class c th kim tra gi tr trc khi gn : Private mMnv As Long Public Property MNV() As Long Get Return mMnv End Get Set(ByVal Value As Long) If Value > 0 Then mMnv = Value End If End Set End Property

Thc hnh son tho trang web Ci t cc chng trnh cn thit Internet Information Services (IIS) (CaiDatIIS.doc) .Net Framework (Ti liu tham kho s 1 - Chng 2) (Nu ci t IIS sau khi ci t .Net Framework th cn ng k Aspnet_isapi.dll bng cch thi hnh tp tin Regsvr32.exe hay aspnet_regis.exe) Khi ng Visual Studio 2005 Chn File/New Web Site Chn ASP.NET Web site/ chn v tr lu (location), ngn ng Lu trang web vo my tnh c chc nng WebServer: phi lu vo th mc c x l bi WebServer Cch 1: C:\InetPub\wwwroot Vo IIS/ Default website Click chut phi vo ng dng chn Properties / Tab Directory chn Create Cch 2: Lu v tr bt k To th mc o (Alias) trn WebServer ch n ni lu WebSite: start\setting\control Panel\ Administrative Tools\IIS\Website\default website\new\virtual directory\ -> t tn alias -> chn th mc lu WebSite thc s -> chn quyn: read, run script Khi to trang web ting vit lu khi lu phi lun chn Encoding l Unicode (UTF8), xem bi c thm: GioiThieuUnicode_ASP.pdf M trang web xem T trnh duyt gi trang web t WebServer. (khng click trc tip) http:// tn my / [Tn Alias] / ng dn trang web (th mc, tp tin) Tn my c th l domain name (www.google.com) hay a ch IP (). Nu trn cng 1 my th c th thay l localhost. Cu trc th mc, tp tin trong 1 website thng c to ra [App_Data]: lu d liu web.config: tp tin nh dng XML lun nm trong th mc gc ca ng dng, cho php cu hnh h thng ng dng nh: nh ngha cc bin ton cc, bo mt, ... Mi ng dng c duy nht 1 tp tin ny. V d: nh ngha bin <add key="TnBin" value="GiTr"/> C th truy cp bin ny bt k u trong ng dng Chui = ConfigurationManager.AppSettings("TnBin")

default.aspx: tp tin cha cc iu khin (m giao din HTML) <%@ Page Language="VB" AutoEventWireup="false" CodeFile="default.aspx.vb" Inherits="TnLp" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Untitled Page</title> </head> <body> </body> </html> Mun vit code trong tp tin ny th dng <% thc ny gi l CodeInline

%> hay <%=tn bin%>, hnh

default.aspx.vb: tp tin cha m x l vb (code behind) Partial Class TnLp Inherits System.Web.UI.Page End Class default.aspx.resx: tp tin cha cu trc ca WebForm di dng XML

10

Chng 3 IU KHIN TRN TRNH CH (ServerControl) So snh 3 cu lnh sau: Phn t HTML <input type=text name=tn> Server Control loi HtmlInputText <input type=text id=tn runat=server/> Web Form <asp:textbox id=tn runat=server/> Thuc tnh runat=server qui nh iu khin ny c bin dch vo trang v thc thi trn Server khi trang c gi. Mun x l cc iu khin bng m th cn s dng iu khin dng ServerControl. Nhng trng hp khng cn truy cp n thuc tnh, phng thc v bin c ca phn t th khng cn dng ServerControl v d khi phn t ch s dng thc thi mt Client Script no nh: tt/m ca s trnh duyt, phn t l HyperLink ... Khi dng ServerControl th khng th khai bo cc on Script kim tra tnh hp l d liu nhp, cn ti nguyn cho qu trnh x l trn Server ... Server cn cu hnh mnh cho mt s lng ngi dng cng truy cp. 1. HTML SERVER CONTROL c nh ngha trong khng gian tn System.Web.UI.HtmlControls Bao gm cc iu khin tng tc thng s dng trong th <form> ca HTML. V d: ExHtmlServerControl.aspx <script runat="server"> Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) a.InnerHtml = "Xin Cho" a.Style("color") = "red" a.Style("font-size") = "40px" End Sub </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div id="a" runat=server> </div> </form> </body> </html>

11

Kt qu Cc v d v HtmlServerControl HtmlGenericControl: to ra phn t Html bt k HtmlAnchorControl, HtmlImageControl, HtmlFormControl, HtmlInputButtonControl, HtmlInputTextControl, HtmlInputCheckBoxControl,HtmlInputRadioButtonControl, HtmlInputImageControl, HtmlInputFileControl, HtmlInputHiddenControl, HtmlSelectControl, HtmlTextAreaControl, HtmlTable Bin c ServerClick, ServerChange ServerClick: xy ra khi ngi dng tng tc trn cc iu khin nh HtmlAnchor, HtmlButton, HtmlInputButton, HtmlInputImage. ServerChange: xy ra khi ngi dng nhn vo cc thnh phn iu khin HtmlInputButton, HtmlInputImage. 2. ASP.NET WEB FORM CONTROL Thanh cng c ca web form (toolbox) HTML: cha cc iu khin chy pha client, l cc th HTML Standard : cha cc iu khin chy pha server, l cc th bt u l ch asp v c thuc tnh runat=server u im web form l: c nhiu chc nng x l trn server, kt ni ngun d liu c sn Hu ht cc WebForm control u k tha cc thuc tnh, phng thc v bin c lp c s WebControl c nh ngha trong khng gian c tn System.Web.UI.WebControls V d: ExWebForm.aspx

12

2.1. <asp:label> Tng ng th <div> Thuc tnh Din gii ID Nhn dng iu khin Text Ni dung hin thi Visible Cho php hin th hay khng (true/false) Trang aspx: <asp:Label ID="lbThongBao" runat="server" Size="16pt"></asp:Label> Trang aspx.vb: Dim s As String Sub Page_Load() s = " " lbThongBao.Text = s End Sub <asp:textbox> Thuc tnh Din gii TextMode SingleLine(mc nh), Multiline (rows,columns), Password. Trang aspx: <asp:TextBox ID="txtTen" runat="server" Size="16pt"></asp:TextBox> Trang aspx.vb: s += " Bn tn: " + txtTen.Text 2.2.

Font-

Font-

2.3. <asp:checkbox> Trang aspx: <asp:CheckBox ID="SoThich1" runat="server" Text="Th thao" /> <asp:CheckBox ID="SoThich2" runat="server" Text="Nghe nhc" /> <asp:CheckBox ID="SoThich3" runat="server" Text="Xem phim" /> Trang aspx.vb: If SoThich1.Checked Then s += SoThich1.Text End If If SoThich2.Checked Then s += SoThich2.Text End If If SoThich3.Checked Then s += SoThich3.Text End If 2.4. <asp:radiobutton>

13

Thuc tnh Din gii GroupName Nhm cc radio cng b Trang aspx: <asp:RadioButton ID="RadioButton1" runat="server" GroupName="Phai" Text="N" /> <asp:RadioButton ID="RadioButton2" runat="server" GroupName="Phai" Text="Nam" /> <asp:hyperlink> Thuc tnh Din gii ImageUrl ng dn hnh nh lm nhn lin kt Trang aspx: <asp:HyperLink ID="HuongDan" runat="server" NavigateUrl="HuongDan.htm"> Hng dn thm</asp:HyperLink> 2.6. <asp:image> Trang aspx: <asp:Image ID="Image1" runat="server" ImageUrl="Hinh/hinhnguoi.GIF" /> 2.7. <asp:button> Tng ng th <input> c type=submit Trang aspx: <asp:Button ID="txtLuu" runat="server" Size="16pt" /> 2.5.

ImageAlign="Bottom"

Text="Lu"

Font-

2.8. <asp:imagebutton> Tng ng th <input> c type=image Trang aspx: <asp:ImageButton ID="ImageButton1" runat="server" AlternateText="Lu" ImageUrl="Hinh/hinhnguoi.GIF" /> 2.9. <asp:table> Trang aspx: <asp:Table ID="Table1" runat="server" BorderWidth="2px" GridLines="Both"> <asp:TableRow runat="server"> <asp:TableCell runat="server">a</asp:TableCell> <asp:TableCell runat="server">b</asp:TableCell> </asp:TableRow> <asp:TableRow runat="server"> <asp:TableCell runat="server">c</asp:TableCell> <asp:TableCell runat="server">d</asp:TableCell> <asp:TableCell runat="server">e</asp:TableCell> </asp:TableRow> </asp:Table>

14

3. BIN C CLICK, CHANGE, COMMAND Cc iu khin Web Form c hai bin c thng thng l: Click, Change. Bin c OnClick, OnClientClick s dng cho Button, ImageButton. Bin c OnTextChange xy ra khi kch hot ni dung ca textbox v nhn phm Tab hay tr du nhy n iu khin khc (Lu thuc tnh AutoPostBack). Tng t nh vy i vi CheckBox l OnCheckedChanged, DropDownList l OnSelectedIndexChanged, RadioButtonList l OnSelectedIndexChanged, ... OnClick: x l hm pha server OnClientClick: x l hm pha client (javascript) iu khin ch th (Command Control): Button, ImageButton, LinkButton. Cc iu khin ny cho php khai bo cc bin c khi ngi s dng thc hin hnh ng trn Control. Khi nhn chut xung th bin c Command trn server s c thc hin. C hai thuc tnh l CommandName, CommandArgument. Trang aspx: <asp:Button ID="Luu" runat="server" Text="Lu" FontSize="16pt" onclick=LuuClick/> Trang aspx.vb: Protected Sub LuuClick(ByVal sender As Object, ByVal e As System.EventArgs) s += " Bn tn: " + txtTen.Text lbThongBao.Text = s End Sub Hay Trang aspx: <asp:Button ID="Luu" runat="server" Text="Lu" FontSize="16pt" /> Trang aspx.vb: Protected Sub Luu_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Luu.Click s += " Bn tn: " + txtTen.Text lbThongBao.Text = s End Sub 4. ASP.NET LIST CONTROL Lp ASP.NET List Control c k tha t lp c s c tn ListControl, l mt phn ca khng gian System.Web.UI.WebControls. Cung cp cc thuc tnh, phng thc v bin c nh trong phn Web Form v mt s khc bit. Thuc tnh ca ListControl Thuc tnh AutoPostBack Din gii False: Khi ngi dng thay i gi tr ca ListControl th trang web s khng c triu gi cho n khi dng submit.

15

DataSource DataTextField DataValueField SelectedIndex SelectedItem Items

True: Trang web s submit mi khi ngi dng thay i gi tr ca ListControl. Gn hay tr v i tng cha d liu. Gn hay tr v tn trng cung cp nhn cho ListControl Gn hay tr v tn trng cung cp gi tr cho ListControl Gn hay tr v gi tr l s nguyn ca phn t c chn th 1 trong ListControl. Trng hp chn nhiu phn t phi s dng thuc tnh Selected ca i tng ListItem. Tr v tham chiu ca phn t c chn th 1 trong ListControl. Trng hp chn nhiu phn t phi s dng thuc tnh Selected ca i tng ListItem. Tr v tp cc gi tr ca cc phn t trong ListControl

4.1. <asp:radiobuttonlist> Trang aspx: <asp:RadioButtonList ID="Phai" runat="server" RepeatDirection="Horizontal"> <asp:ListItem>Nam</asp:ListItem> <asp:ListItem>N</asp:ListItem> </asp:RadioButtonList> Trang aspx.vb: s += " - Phi: " + Phai.SelectedValue 4.2. <asp:checkboxlist> Trang aspx: <asp:CheckBoxList ID="SoThich" runat="server" RepeatDirection="Horizontal"> <asp:ListItem>Th thao</asp:ListItem> <asp:ListItem>Nghe nhc</asp:ListItem> <asp:ListItem>Xem phim</asp:ListItem> </asp:CheckBoxList></td> Trang aspx.vb: Sub Page_Load() Dim a As New Hashtable(3) a.Add("1", 1) a.Add("2", 2) a.Add("3", 3) SoThich.DataSource = a SoThich.DataTextField = "Key" SoThich.DataValueField = "Value" SoThich.DataBind() End Sub 4.3. <asp:dropdownlist> Trang aspx:

16

<asp:DropDownList ID="Ngay" runat="server"> <asp:ListItem Selected="True">Ngy</asp:ListItem> <asp:ListItem>1</asp:ListItem> <asp:ListItem>2</asp:ListItem> <asp:ListItem>3</asp:ListItem> </asp:DropDownList> <asp:DropDownList ID="Thang" runat="server"> <asp:ListItem Selected="True">Thng</asp:ListItem> </asp:DropDownList> Trang aspx.vb: Sub Page_Load() For i = 1 To 12 Thang.Items.Add(i) Next End Sub hay Sub Page_Load() Dim a As New Hashtable(3) a.Add("1", 1) a.Add("2", 2) a.Add("3", 3) Thang.DataSource = a Thang.DataTextField = "Key" Thang.DataValueField = "Value" Thang.DataBind() End Sub 4.4. <asp:listbox> Thuc tnh SelectionMode Din gii Single/Multiple: chn mt/nhiu phn t cng lc.

Trang aspx: <asp:ListBox ID="NgheNghiep" runat="server"> <asp:ListItem>Sinh vin</asp:ListItem> <asp:ListItem>Gio vin</asp:ListItem> <asp:ListItem>Cng nhn</asp:ListItem> <asp:ListItem>K s</asp:ListItem> <asp:ListItem>Khc</asp:ListItem> </asp:ListBox></td> Trang aspx.vb: s += " Ngh nghip: " + NgheNghiep.SelectedItem.Value 5. ASP.NET VALIDATION CONTROL Kim tra tnh hp l ca d liu nhp vo form Thuc tnh chung ControlToValidate: gn/tr v tn iu khin cn kim tra

17

ErrorMessage: chui li pht sinh nu kim tra khng hp l. Enabled: gn/tr v gi tr Boolean ch ra rng nu qu trnh kim tra gi tr ca iu khin c thc hin. Gn True nu mun kim tra d liu nhp bng cc Validation Control, False v hiu ha qu trnh kim tra. EnableClientScript: gn/tr v gi tr Boolean ch ra rng cho php kim tra gi tr ca iu khin trn pha trnh khch (JavaScript)

5.1.RequiredFieldValidator: kim tra iu khin khng c rng. Trang aspx: <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtTen" ErrorMessage="cha nhp"> </asp:RequiredFieldValidator> 5.2.RangeValidator: kim tra iu khin phi l chui, s, ngy, . Nu rng th khng kim tra. Thuc tnh: MaximumValue, MinimumValue, Type (String, Interger, Date, ), Trang aspx: <asp:RangeValidator ID="RangeValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="T 1 n 10 triu" MaximumValue="10000000" MinimumValue="1" > </asp:RangeValidator> 5.3.CompareValidator: so snh iu khin ny vi iu khin khc hay mt gi tr c th. Nu rng th khng kim tra. Thuc tnh: ControlToCompare, Operator(Equal, GreaterThan, LessThan, ), Type, ValueToCompare, Trang aspx: <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="txtMatKhau" ControlToValidate="txtMatKhauLai" ErrorMessage="khng ging " Display="Dynamic"> </asp:CompareValidator> 5.4.RegularExpressionValidator: kim tra gi tr trong iu khin phi ph hp vi gi tr cho trc. Nu rng th khng kim tra. Thuc tnh: ValidationExpression, Trang aspx: <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtEmail" ErrorMessage="khng hp l" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+ ([.]\w+)*"> </asp:RegularExpressionValidator> 5.5.CustomValidator: thc hin kim tra do ngi dng t nh ngha. Thuc tnh: ClientValidationFunction, OnServerValidation

18

5.6.ValidationSummary: tp hp cc thng bo li. Thuc tnh: DisplayMode, ShowMessageBox, ShowSummary, 6. ASP.NET RICH CONTROL 6.1 <asp:AdRotator> V d: ExXmlfile.xml, ExRichControl.aspx ExXmlfile.xml (lu phn bit ch hoa, ch thng) <?xml version="1.0" encoding="utf-8" ?> <Advertisements> <Ad> <ImageUrl>Hinh/hinhnguoi.GIF</ImageUrl> <NavigateUrl>www.a.com</NavigateUrl> <AlternateText>aaa</AlternateText> <Impressions>3</Impressions> </Ad> <Ad> <ImageUrl>Hinh/HoaNho.GIF</ImageUrl> <NavigateUrl>www.b.com</NavigateUrl> <AlternateText>bbb</AlternateText> <Impressions>2</Impressions> </Ad> </Advertisements> ExRichControl.aspx <asp:AdRotator ID="AdRotator1" AdvertisementFile="ExXMLFile.xml" /> Kt qu

runat="server"

6.2 <asp:Calendar> V d ExRichControl.aspx <asp:Calendar </asp:Calendar> Kt qu ID="Calendar1" runat="server">

19

6.3 <asp:XML> 7. ASP.NET MOBILE CONTROL

20

Chng 4 CC I TNG TRONG ASP 1. RESPONSE HttpResponse c dng chuyn d liu t Server v Client Gi tt c thng tin x l cho cc client yu cu, chng ta c th s dng i tng ny giao tip vi ngi dng. Gi thng tin t server cho client. Phng thc Response.write(str): xut thng tin dng chui ra trang web. Response.Redirect: chuyn sang trang khc. WriteFile: ghi lung d liu ra tp tin. Clear, end, flush V d Response.write (str) Response.write (str) Response.Redirect("abc.aspx") 2. REQUEST HttpRequest c dng vn ti d liu t Client v Server. i tng Request trong ASP.NET c ci t trong lp HttpRequest thuc khng gian tn System.Web Nhn tt c gi tr m trnh duyt ca client gi n server thng qua mt yu cu HTTP (HTTP request). Ly thng tin t client. Phng thc form POST: i tng Request dng FORM nhn cc d liu gi n server. GET: i tng Request dng QueryString nhn cc d liu gi n server. Params: ly gi tr ca tham s trn QueryString, Forms, ServerVariables (cha cc thng tin Server_Name, IP, ), Cookies SaveAs: lu yu cu hin ti trn HTTP ra tp tin. (page54) Request.Form(thams)[(index)|.Count] Request.QueryString(thams)[(index)|.Count] Request(thams)[(index)|.Count] V d: QueryString nhn cc d liu truyn theo url sau du ? <a href=tntptin?bin1=gitr1&bin2=gitr2> Request.QueryString(bin1) Request.QueryString(bin2) Tp hp form: thuc tnh count, keys Tp hp querystring: thuc tnh count, keys(i):mng, tr v tn cc tham s V d: ExReqRes.htm, ExReqRes.aspx.vb To file ExReqRes.htm <body>

21

<FORM name=f action=ExReqRes.aspx method =post> <P>H <INPUT name=Ho></P> <P>Tn<INPUT name=Ten></P> <P>Phi <INPUT type=radio value=B name=Phai>N <INPUT type=radio value=ng name=Phai> Nam </P> <P><INPUT type=submit value=OK name=submit1></P> </FORM> </body> To file ExReqRes.aspx.vb Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim h, t, p, str As String h = Request.Form("Ho") t = Request("Ten") p = Request("Phai") str = "Cho " + p + " " + h + " " + t Response.Write(str) End Sub Kt qu

V d ExReqRes1.aspx <body> <form id="form1" runat="server"> <asp:Label ID="Label1" runat="server" Text="Ngh nghip "></asp:Label> <asp:ListBox ID="NgheNghiep" runat="server" SelectionMode="Multiple"> <asp:ListItem Selected>Gi&#225;o vi&#234;n</asp:ListItem> <asp:ListItem>C&#244;ng Nh&#226;n</asp:ListItem> <asp:ListItem>Nh&#226;n Vi&#234;n</asp:ListItem> <asp:ListItem>T Do</asp:ListItem> </asp:ListBox> <asp:Button ID="Goi" runat="server" Text="Gi" /> </form> </body>

22

Protected Sub Goi_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Goi.Click If Request.Form.GetValues("nghenghiep").Length > 0 Then Dim i, n As Integer n = Request.Form.GetValues("nghenghiep").Length 1 For i = 0 To n Response.Write(Request.Form.GetValues("NgheN ghiep")(i) + "<br>") Next End If End Sub Kt qu

V d ExReqRes1.aspx.vb Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim n As String For Each n In Request.ServerVariables Response.Write("<P><B>" + n + "</B><P>") Response.Write(Request.ServerVariables(n)) Next End Sub Kt qu

23

3. APPLICATION D liu lu trong Application s c chia s cho tan b ng dng v tan b ngi dng V d: m s ngi dng truy cp. Application(tn i tng) = gi tr 4. SESSION D liu lu trong Session s c chia s cho tan b ng dng ca 1 ngi dng. Session(tn i tng) = gi tr V d: Global.asax, ExAppSes.aspx.vb Tp tin Global.asax <script runat="server"> Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs) ' Goi 1 lan Application("SoNguoiDaDangNhap") = 0 Application("SoNguoiDangDangNhap") = 0 End Sub Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs) ' Code that runs when a new session is started Application("SoNguoiDaDangNhap") += 1 Application("SoNguoiDangDangNhap") += 1 Session("TenDangNhap") = "" Session("GioHang") = New ArrayList

24

End Sub Sub Session_End(ByVal sender As Object, ByVal e As EventArgs) Application("SoNguoiDangDangNhap") -= 1 Session("TenDangNhap") = "" End Sub </script> Tp tin ExAppSes.aspx.vb Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Response.Write("S ngi truy cp: " & Application("SoNguoiDaDangNhap")) Response.Write("<br>S ngi ang truy cp: " & Application("SoNguoiDangDangNhap")) End Sub 5. COOKIES Lu thng tin ngi dng trn cng mt my l nh nhau (khng bo mt) Thng tin d liu cha trong Cookies chia s cho 1 chng trnh ca 1 ngi dng. 6. EXCEPTION Cho php kim sot li

25

Chng 5 KT NI D LIU VI I TNG ADO.NET Tham kho thm: www.asp.net/learn/videos/ 07 - Databinding to User Interface Controls_vb Lp kt ni Lp khng kt ni Cho php truy vn d liu (select) v thao Cho php thao tc trn d liu tm thi hay tc (delete, insert, update) n ngun d liu d liu m c th c hay ghi ra lung XML, tp tin Phi dng .NET Data Provider h tr cho Dng .NET Data Provider ty . Bao gm 3 CSDL kt ni. Bao gm 4 lp lp 1. Connection: lp kt ni vi ngun 1. DataSet: b nh truy cp nhanh CSDL. (cache memory), cho php lu tr 2. Command: lp cha lnh thi hnh d liu c ly t d liu ngun. (Select, Insert, Update, Delete). L mt container cha ng v 3. DataAdapter: lp cha b iu phi kim sot ton b mu tin, m cn d liu. gi nhiu bng d liu v cc quan 4. DataReader: lp c lung d liu h gia chng. t ngun d liu mt chiu 2. DataTable: bng d liu (Forward-only 3. DataView: trnh by d liu di cc gc nhn khc nhau.

ADO.NET bao gm 2 Provider: SQL Server.NET Data Provider OLE DB.NET Data Provider SqlConnection: dng cho SQL Server 7.0 OleDbConnection: hay cc phin bn v sau dng cho SQL Server 6.5 hay cc phin bn v trc, tp tin vn bn, bng tnh ph thuc vo ngun d liu kt ni, cho php kt ni nhiu loi d liu: Access, Oracle . SqlCommand OleDbCommand SqlDataAdapter OleDbDataAdapter SqlDataReader OleDbDataReader SqlParameter OleDbParameter C php hu nh ging nhau. Khai bo khng gian tn ADO.NET Imports System.Data Imports System.Data.SqlClient hay Imports System.Data Imports System.Data.OleDb

26

hay Imports System.Data Imports System.Data.Odbc hay Imports System.Data Imports System.Data.OracleClient Khai bo v s dng khng gian tn ca ADO.NET: Imports System.Data.SqlClient 1. i tng SqlConnection Thit lp kt ni vi ngun CSDL. Chui kt ni (ConnectionString): C th vit trong trang ASP.NET StrConn = "server=TnMyChaDLiu;UID=TnngNhp;PWD=MtKhu;d atabase=TnCSDL;" Dim myConn As SqlConnection = New SqlConnection(StrConn) myConn.Open() Hay trong tp tin Web.Config <configuration> <appSettings> <add key="conn" value="server=TnMyChaDLiu;UID=TnngNhp;PW D=MtKhu;database=TnCSDL;" /> </appSettings> Sau dng trong trang ASP.NET StrConn = ConfigurationManager.AppSettings("conn") Phng thc m / ng kt ni: myConn.Open() / myConn.Close() Thuc tnh: ConnectionString, ChangeDatabase, DataSource (tn server/a ch IP), ConnectTimeOut, State (trng thi kt ni). Phng thc: Open, Close, CreateCommand, BeginTransaction, Rollback, Dispose. i tng OleDbConnection Tng t nh SqlConnection nhng c thm thuc tnh Provider: chn trnh iu khin d liu. 2. i tng SqlCommand Lp cha lnh thi hnh (Select, Insert, Update, Delete). Thc thi cc cu lnh SQL hay Stored Procedure 2.1. Phng thc:

27

ExecuteReader: V d: ExSqlConn.aspx Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim StrConn As String StrConn = "server=localhost;UID=sa;PWD=sa;database=Sach;" 'StrConn = ConfigurationSettings.AppSettings("conn") Dim myConn As SqlConnection = New SqlConnection(StrConn) myConn.Open() Dim StrSQL As String StrSQL = "select * from NhaXuatBan" Dim myCom As SqlCommand = New SqlCommand(StrSQL, myConn) Dim drd As SqlDataReader drd = myCom.ExecuteReader While drd.Read Response.Write("<br>Tn: ") Response.Write(drd("TenNXB").ToString) Response.Write("<br>a ch: ") Response.Write(drd("DiaChi").ToString) Response.Write("<br>in thoi: ") Response.Write(drd("DienThoai").ToString) Response.Write("<br>") End While drd.Close() End Sub ExecuteNonQuery V d: ExSqlCom.aspx (lu khi refesh) If Request.Form("TenNXB") <> " " Then Dim StrSQL As String StrSQL = "insert into NhaXuatBan(TenNXB,DiaChi,DienThoai) values('" + Request.Form("TenNXB") + "','" + Request.Form("DiaChi") + "','" + Request.Form("DienThoai") + "')" Dim myCom As SqlCommand = New SqlCommand(StrSQL, myConn) myCom.ExecuteNonQuery() End If

28

ExecuteScalar V d: ExSqlCom.aspx Dim StrSQL1 As String StrSQL1 = "select count(*) from NhaXuatBan" Dim myCom1 As SqlCommand = New SqlCommand(StrSQL1, myConn) Dim n As Integer n = myCom1.ExecuteScalar Response.Write("<br>m cout NXB: " & n) 2.2. Thuc tnh: Connection: cha i tng kt ni Commandtext: cha cu lnh SQL Thay Dim myCom As SqlCommand = New SqlCommand(StrSQL, myConn) Bng Dim myCom As New SqlCommand myCom.CommandText = StrSQL myCom.Connection = myConn 3. i tng DataReader 4. i tng DataAdapter 5. i tng DataSet V d: ExDataSet.aspx Response.Write("<br>Sch: ") Dim StrSQL As String StrSQL = "select * from NhaXuatBan, Sach where NhaXuatBan.MaNXB=Sach.MaNXB" Dim myDataSet As New DataSet Dim myDataAdapter As New SqlDataAdapter(StrSQL, myConn) myDataAdapter.Fill(myDataSet) GridView1.DataSource = myDataSet GridView1.DataBind() 6. i tng DataTable 7. i tng DataView V d: ExDataTable.aspx To bng d liu, thm ct, hng Dim myConn As SqlConnection Dim myDataSet As DataSet Dim myDataView As DataView Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

29

Dim StrConn As String StrConn = ConfigurationSettings.AppSettings("conn") myConn = New SqlConnection(StrConn) myConn.Open() TaoBang() GridView1.DataSource = myDataSet GridView1.DataBind() GridView2.DataSource = myDataView GridView2.DataBind() End Sub Protected Sub TaoBang() Dim myDataTable As DataTable = New DataTable("NhanVien") Dim myDataColumn As DataColumn myDataColumn = New DataColumn myDataColumn.DataType = System.Type.GetType("System.Int32") myDataColumn.ColumnName = "MaNV" myDataColumn.Unique = True myDataTable.Columns.Add(myDataColumn) myDataColumn = New DataColumn myDataColumn.DataType = System.Type.GetType("System.String") myDataColumn.ColumnName = "TenNV" myDataTable.Columns.Add(myDataColumn) Dim key(0) As DataColumn key(0) = myDataTable.Columns("MaNV") myDataTable.PrimaryKey = key myDataSet = New DataSet myDataSet.Tables.Add(myDataTable) Dim i As Integer Dim myDataRow As DataRow For i = 0 To 5 myDataRow = myDataTable.NewRow myDataRow("MaNV") = i myDataRow("TenNV") = "Nguyn " + i.ToString myDataTable.Rows.Add(myDataRow) Next myDataView = New DataView(myDataTable) With myDataView .RowFilter = "MaNV>1"

30

.Sort = "TenNV desc" End With End Sub 8. i tng DataRelation

31

Chng 6 LIST CONTROL Tham kho thm: www.asp.net/learn/videos/ 07 - Databinding to User Interface Controls_vb: DataSource, GridView, DetailView. 08 - Working with the GridView and FormView Controls_vb 1. iu khin GridView Cho php hin th d liu dng bng, kt qu xut ra trnh duyt l th <table>. C th: phn trang; sp xp tng ct d liu; chn, xa, cp nht tng dng d liu. 1.1 Cc thuc tnh thng dng V d: ExGridView2.aspx, ExGridView2.aspx.vb Tp tin ExGridView.aspx: phn x l chn record d liu hin th thng tin chi tit Danh sch nh xut bn <asp:GridView ID="GridView1" //nhn dng runat="server" AutoGenerateColumns="False" //True: t ng to cc BoundColumn //cho php hin th cc ct d liu theo d liu ngun. //False: mun ch nh ct hiu th, dng <columns> CellPadding="2" //khang cch gia cc hng CellSpacing="3" //khang cch gia cc ct DataKeyNames="MaNXB" DataSourceID="SqlDataSource1" //Ngun d liu DataSource: ch nh i tng DataSet/DataReader cha d liu, cn dng thm phng thc DataBind iu d liu vo GridView. Width="681px"> <PagerSettings Position="Top" /> //nh dng phn trang <Columns> <asp:BoundField DataField="TenNXB" HeaderText="Tn" /> //Hiu th d liu ca mt ct d liu trong DataSource Tng t c th dng <asp:templatecolumn <asp:BoundField DataField="DienThoai" HeaderText="in thoi" /> </Columns> <HeaderStyle BackColor="#8080FF" /> //nh dng d liu trnh by trong phn tiu ca bng <AlternatingRowStyle

32

BackColor="#C0C0FF" Font-Bold="True" ForeColor="CornflowerBlue" /> //nh dng d liu dng l </asp:GridView> 1.2 Hin th d liu Nh v d trong phn kt ni ngun d liu 1.3 Phn trang Khi s lng mu tin tn ti trong DataSource qu ln. bn nn trnh by thnh nhiu trang. Cc thuc tnh Th asp:GridView AllowPaging="True/False" //Cho php phn trang v iu hng PageSize="5" //s lng mu tin trong 1 trang PageCount(ch c, s lng trang) Th PagerSettings Position=Top/Bottom/TopandBottom Mode=Numeric/NumericFirstLast/NextPrevious/NextPrevious FirstLast NextPageText NextPageImageUrl PageButtonCount //s trang hiu th, nu s trang nhiu hn s ny th s hin th tng phn S kin PageIndexChanging TnGridView.PageIndex = e.NewPageIndex Vi e l System.Web.UI.WebControls.GridViewPageEventArgs Lu cn np li d liu cho GridView 1.4 Sp xp Cc thuc tnh Th asp:GridView AllowSorting="True/False" Th asp:BoundField SortExpression="TenNXB" //tn trng d liu i tng ViewState: l bin server dng lu trng thi hin hnh ca trang. Thng tin lu c m ha. L i tng mi trong .net Sub Page_Load If Not IsPostBack Then

33

ViewState("TenCot") = "TenNXB" ViewState("SapGiam") = True DuLieu() End If End Sub Sub DuLieu() kt ni v ly d liu GridView1.DataSource = myDataTable.DefaultView t = ViewState("TenCot") & IIf(ViewState("SapGiam"), " ", " Desc") myDataTable.DefaultView.Sort = t GridView1.DataBind() End Sub S kin Sorting If ViewState("TenCot") = e.SortExpression Then ViewState("SapGiam") = Not ViewState("SapGiam") Else ViewState("TenCot") = e.SortExpression End If t = e.SortExpression & IIf(ViewState("SapGiam"), " Desc", "") Vi e l System.Web.UI.WebControls.GridViewSortEventArgs, s l bin ton cc. Lu cn np li d liu cho GridView Thuc tnh DefaultView: iu chnh khung nhn ca bng. Khung nhn d liu tr v bng d liu c th lc, sp xp, tm kim. 1.5 Xa d liu Cc thuc tnh <Columns> <asp:CommandField DeleteText ="Xa" ShowDeleteButton=True /> C th dng hnh nh thay ch DeleteImageUrl="Hinh.GIF" ButtonType="Image" S kin RowDeleting StrSQL = "delete from NhaXuatBan where MaNXB=@Ma" Dim myCom As New SqlCommand(StrSQL, myConn) myCom.Parameters.AddWithValue("@Ma", GridView1.DataKeys(e.RowIndex).Value)

34

myCom.ExecuteNonQuery() Vi e l System.Web.UI.WebControls.GridViewDeleteEventArgs Xa nhiu <asp:TemplateField .. hin th thm mt ct trng ty chn iu khin vo. <Columns> <asp:TemplateField HeaderText="Xa"> <ItemTemplate> <asp:CheckBox ID="CheckBox1" runat="server" /> </ItemTemplate> </asp:TemplateField> .. <asp:Button ID="Xoa" runat="server" Text="Xa" OnClientClick ="return confirm('Bn tht s mun xa ?')" /> Trong s kin Click ca nt Xa Dim c As CheckBox Dim i As Integer Dim s As String = " " Dim myGridViewRow As GridViewRow For i = 0 To GridView1.Rows.Count - 1 myGridViewRow = GridView1.Rows(i) c = CType(myGridViewRow.FindControl("CheckBox1"), CheckBox) If c.Checked Then Dim StrConn As String Dim myConn As SqlConnection StrConn = ConfigurationManager.AppSettings("conn") myConn = New SqlConnection(StrConn) myConn.Open() Dim StrSQL As String StrSQL = "delete from NhaXuatBan where MaNXB=@Ma" Dim myCom As New SqlCommand(StrSQL, myConn) myCom.Parameters.AddWithValue("@Ma", GridView1.DataKeys(i).Value) myCom.ExecuteNonQuery() End If Next 1.6 Cp nht d liu

35

Cc thuc tnh <Columns> <asp:CommandField EditText="Sa" CancelText="B qua" UpdateText="Cp nht" ShowEditButton="True" /> C th dng hnh nh thay ch EditImageUrl="Hinh.GIF" ButtonType="Image" S kin RowEditing GridView1.EditIndex = e.NewEditIndex Vi e l System.Web.UI.WebControls.GridViewEditEventArgs Lu cn np li d liu cho GridView S kin RowCancelingEdit GridView1.EditIndex = -1 Vi e l System.Web.UI.WebControls.GridViewCancelEditEventArgs Lu cn np li d liu cho GridView S kin RowUpdating kt ni d liu StrSQL = "update NhaXuatBan set TenNXB=@Ten,DiaChi=@DC,DienThoai=@DT where MaNXB=@Ma" Dim myCom As New SqlCommand(StrSQL, myConn) myCom.Parameters.AddWithValue("@Ma", GridView1.DataKeys(e.RowIndex).Value) Dim myGridViewRow As GridViewRow myGridViewRow = GridView1.Rows(e.RowIndex) Dim t1, t2, t3 As TextBox t1 = myGridViewRow.Cells(0).Controls(0) t2 = myGridViewRow.Cells(1).Controls(0) t3 = myGridViewRow.Cells(2).Controls(0) myCom.Parameters.AddWithValue("@Ten", t1.Text) myCom.Parameters.AddWithValue("@DT", t2.Text) myCom.Parameters.AddWithValue("@DC", t3.Text) myCom.ExecuteNonQuery() GridView1.EditIndex = -1 Vi e l System.Web.UI.WebControls.GridViewUpdateEventArgs Lu cn np li d liu cho GridView

36

2. iu khin DataList 2.1 Cc thuc tnh thng dng <asp:DataList ID="DataList1" runat="server" DataKeyField="MaS" RepeatColumns=1 RepeatDirection=Horizontal Width="100%"> <ItemTemplate> TenS: <asp:Label ID="TenSLabel" runat="server" Text='<%# Eval("TenS") %>'></asp:Label><br /> DonGia: <asp:Label ID="DonGiaLabel" runat="server" Text='<%# Eval("DonGia") %>'></asp:Label><br /> </ItemTemplate> </asp:DataList> 2.2 Hin th d liu t SQL Server V d: ExDataList.aspx Kt ni ngun d liu Dim StrSQL As String StrSQL = "select * from Sach" Dim StrConn As String Dim myConn As SqlConnection StrConn = ConfigurationManager.AppSettings("conn") myConn = New SqlConnection(StrConn) myConn.Open() Dim myDataTable As New DataTable Dim myDataAdapter As New SqlDataAdapter(StrSQL, myConn) myDataAdapter.Fill(myDataTable) DataList1.DataSource = myDataTable DataList1.DataBind() C th chn cc iu khin hin th d liu dng hnh nh, lin kt .. <asp:Image <asp:HyperLink Dng s kin ItemDataBound gn d liu vo (xy ra khi 1 d liu c gn vo DataList) Dim hinh As Image Dim l1 As Label Dim l2 As Label Dim h As HyperLink

37

hinh = CType(e.Item.FindControl("Image1"), Image) hinh.ImageUrl = "Image/" & e.Item.DataItem("HinhMinhHoa") h = CType(e.Item.FindControl("HyperLink1"), HyperLink) h.NavigateUrl = "ChiTiet.aspx?Ms=" & e.Item.DataItem("MaS") h.Text = e.Item.DataItem("TenS") l1 = CType(e.Item.FindControl("Label1"), Label) l1.Text = Format(e.Item.DataItem("DonGia"), "#,##") & " ng" l2 = CType(e.Item.FindControl("Label2"), Label) l2.Text = Format(e.Item.DataItem("NgayCapNhat"), "dd/MM/yyyy") Trong tp tin ChiTiet.aspx ly gi tr ca MaS trong bin Ms ri hin th tt c thng tin lin quan n cun sch Dim s As String s = Request.QueryString("Ms") Lu phn bit ch hoa thng: "dd/MM/yyyy" 2.3 To lin kt cc chc nng To DataList c 1 lin kt <asp:DataList ID="DataList1" runat="server" RepeatDirection="Horizontal"> <ItemTemplate> <asp:HyperLink ID="HyperLink1" runat="server"> HyperLink</asp:HyperLink> </ItemTemplate> </asp:DataList> To lp Class ChucNang Public mTen As String Public mDuongDan As String Public Sub New(ByVal Ten As String, ByVal DuongDan As String) mTen = Ten mDuongDan = DuongDan End Sub End Class

38

To mng cc chc nng Dim a As New ArrayList a.Add(New ChucNang("Nh xut bn", "NhaXuatBan.aspx")) a.Add(New ChucNang("Khch Hng", "KhachHang.aspx")) S kin ItemDataBound Dim c As ChucNang Dim h As HyperLink c = CType(e.Item.DataItem, ChucNang) h = CType(e.Item.FindControl("HyperLink1"), HyperLink) h.Text = c.mTen & "|||" h.NavigateUrl() = c.mDuongDan 3. iu khin DetailView Ti mt thi im ch cho php hin th mt dng d liu. C th: phn trang, xa, thm, cp nht d liu nhng khng sp xp. C th ly d liu t View hay nhiu bng nhng lc khng th cp nht d liu. 3.1 Cc thuc tnh thng dng <asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="true" AutoGenerateRows="False" DataSourceID="SqlDataSource1" > <Fields> <asp:BoundField DataField="TenNXB" HeaderText="Tn NXB" /> <asp:BoundField DataField="DiaChi" HeaderText="a Ch" /> </Fields> </asp:DetailsView> 3.2 Hin th d liu Tng t nh trn GridView 3.3 Hin th d liu theo chn la trn GridView S kin trn GridView: SelectedIndexChanged DetailsView1.PageIndex = GridView1.SelectedIndex + GridView1.PageSize * GridView1.PageIndex Vi e l System.EventArgs 3.4 X l d liu 4. iu khin FormView

39

You might also like