Professional Documents
Culture Documents
NET
Khoa CNTT
Hnh 1: Minh ha giao din trang SongManaging.aspx Minh ha cch thao tc trn trang SongManaging.
Lp trnh ASP.NET
Khoa CNTT
Hnh 2: Thao tc tm kim v chn bi ht a vo album M t: phn tm kim theo tiu chun, khi to sn 3 mc chn l: Artist, Genre v Album. Mi khi chn mt tiu chun no th mc chn s hin gi tr c th c (load trong database) tng ng vi mc tm kim. V d nu mc tm kim theo chun chn Artist th trong mc chn ny s truy vn trong bng Artist ly ra tt c cc ngh s v a vo DropDownList Chn. Sau khi chn xong, th kch vo button tm kim, kt qu s lit k ra tt c bi ht theo tiu chun tm kim trn vo trong DataGrid. DataGrid ny chi hin th tn bi ht v mt chc nng chn. User c th chn cc bi ht ng to ra mt album khc. Mi ln chn mt bi ht, thng tin bi ht ny c lu li (s dng m bi ht) trong session. User c th chn tiu chun tm kim khc xem v chn cc bi khc. Khi hon tt qu trnh chn bi ht user kch vo button To Album chuyn sang phn to album mi t cc bi ht chn (gi trang Album.aspx).
o Album.aspx: Cho php user to album mi vi tn gi v danh sch cc bi ht chn trong trang SongManaging.aspx.
Lp trnh ASP.NET
Khoa CNTT
Hnh 3: Trang thm album M t: trang ny ly danh sch cc bi ht chn trong bc trc trang SongManaging.aspx, ti trang ny user c th hiu chnh li danh sch bi ht bng cch xa cc bi ht trc khi thc hin thao tc cui cng to album. o
Hng dn
1. To CSDL SongAlbum c cc bng nh sau: a. Albums: cha danh sch cc album b. Songs: cha danh sch cc bi ht c. AlbumSong: cha danh sch cc bi ht thuc album d. Artist: danh sch cc ngh s e. Genres: th loi
Bng Album
Lp trnh ASP.NET
Khoa CNTT
Bng AlbumSong
Bng Artist
Bng Genres
Lp trnh ASP.NET
Khoa CNTT
Hnh 4: Layout ca trang SongManaging.aspx Cc control m t trn webform Tn ddlSearchBy Kiu DropDownList Thuc tnh Items = {Artist, Genre, Album} Din gii Combobox cho php chn tiu chun tm kim Cha cc gi tr c th chn, tm kim, gi tr ny ph thuc vo gi tr ca ddlSearchBy bn trn Kch vo tm bi ht Hin th thng tin s bi ht chn Hin th kt qu cc bi ht tm c Gi trang Album to album mi t cc bi ht chn
ddlSearchOption
DropDownList
btnSearch lblStatus
Button Label
dgSongs
DataGrid
btnCreateAlbum
Button
3. To chui kt ni CSDL d liu trong file cu hnh web.config Lu : Ty thuc vo server m sinh vin chnh sa li cho thch hp! B sung vo tag <configuration> mt tag <appSettings> nh sau <appSettings> 5
Lp trnh ASP.NET
<add key="ConnectionString" value="sinh vin m t y"></add> </appSettings> Cch thc ly chui ny trong code
String chuoiketnoi = ConfigurationManager.AppSettings["ConnectionString"].ToString(); 4. To hm FillSearchOption trong code behind ca trang SongManaging.aspx: hm ny s ly thng tin user chn trong combobox ddlSearchBy v truy xut database ly d liu tng ng. 1. private void FillSearchOption() 2. { string scnn = 3. ConfigurationManager.AppSettings["ConnectionString"].ToString(); 4. SqlConnection conn = new SqlConnection(scnn); 5. SqlCommand cmd = new SqlCommand(); 6. cmd.Connection = conn; 7. SqlDataAdapter da = new SqlDataAdapter(cmd); 8. // t o datatable l y d li u t database 9. DataTable dt = new DataTable(); 10. 11. //ki m tra xem SearchBy c ch n 12. switch (ddlSearchBy.SelectedIndex) 13. { 14. case 0: // l y thng tin ngh s 15. cmd.CommandText = "SELECT * FROM artists"; 16. break; 17. 18. case 1:// l y thng tin th lo i 19. cmd.CommandText = "SELECT * FROM genres"; 20. break; 21. 22. case 2: // l y danh sch album 23. cmd.CommandText = "SELECT * FROM albums"; 24. break; 25. } 26. try 27. { 28. da.Fill(dt); // a d li u vo datatable 29. } 30. catch (SqlException ex) 31. { 32. Response.Write(ex.Message); 33. } 34. // hi n th k t qu t datatable ra combobox ddlSearchOption 35. ddlSearchOption.DataSource = dt.DefaultView; 6
5. Khi to trong hm Page_Load 1. protected void Page_Load(object sender, EventArgs e) 2. { 3. if (!IsPostBack) 4. { // m c nh ch n item u tin 5. ddlSearchBy.SelectedIndex = 0; 6. FillSearchOption(); // l y thng tin 7. } 8. } 6. To hm FillDataGrid: hm ny c gi khi user kch vo button Tm kim, hm c chc nng ly cc thng tin t phn chn ca user trong 2 combobox v truy vn database, kt qu hin th trong datagrid 1. private void FillDataGrid() 2. { 3. string scnn = ConfigurationManager.AppSettings["ConnectionString"].ToString(); 4. SqlConnection cnn = new SqlConnection(scnn); 5. SqlCommand cmd = new SqlCommand(); 6. cmd.Connection = cnn; 7. 8. SqlDataAdapter da = new SqlDataAdapter(cmd); 9. // t o datatable l y d li u 10. DataTable dt = new DataTable(); 11. // l y gi tr c ch n trong SearchOption 12. string value = ddlSearchOption.SelectedValue; 13. // ki m tra xem search theo tiu chu n no 14. switch (ddlSearchBy.SelectedIndex) 15. { 16. case 0: // l y bi ht theo id c a ngh s 17. cmd.CommandText = "SELECT * FROM Songs WHERE ArtistID = "+ value; 18. break; 19. case 1:// l y bi ht theo id c a th lo i 20. cmd.CommandText = "SELECT * FROM Songs WHERE GenreID = "+value; 21. break; 7
Lp trnh ASP.NET
Khoa CNTT
22. case 2:// l y cc bi ht thu c album id 23. cmd.CommandText = "SELECT * FROM Songs WHERE SongID in (SELECT SongID FROM AlbumSong WHERE AlbumID = " + value+")"; break; 24. 25. } 26. 27. try 28. { 29. da.Fill(dt); // l y d li u vo dt 30. } catch (SqlException ex) 31. 32. { 33. Response.Write(ex.Message); 34. } 35. // hi n th k t qu danh sch bi ht trong datagrid 36. dgSongs.DataSource = dt.DefaultView; 37. dgSongs.DataBind(); 38. 39. } 7. Khai bo trnh x l s kin click ca button btnSearch 1. protected void btnSearch_Click(object sender, EventArgs e) 2. { 3. FillDataGrid(); // th c hi n tm ki m v hi n th ds bi ht 4. } 5. Thit k li DataGrid hin th danh sch tn bi ht. Trong datagrid ny ta hin th 2 ct, mt ct l tn bi ht, ct cn li l chc nng chn, Chn chc nng Property Builder cho datagrid, chn tab Columns Thm mt Bound Column kt buc vi field SongNam tn bi ht
Lp trnh ASP.NET
Khoa CNTT
Thm 1 button ku Select cho chc nng chn, khai bo thuc tnh Command Name l SelectSong.
Lp trnh ASP.NET
Khoa CNTT
6. Chc nng chn bi ht vo danh sch to mi album, chc nng chn ny user c th kch vo datagrid lit k danh sch bi ht trn giao din. Do mi bi ht cha trn 1 dng ca datagrid nn khi kch chn vo ta phi bit c id ca bi ht , lm c iu ny ta thit lp thuc tnh DataKeyField cho datagrid dgSong vi id ca bi ht. Trong mn hnh thit k form ta chn dgSong v thit lp thuc tnh DataKeyField l SongID nh hnh minh ha
10
Lp trnh ASP.NET
Khoa CNTT
Hnh: Khai bo thuc tnh DataKeyField l SongID. Ta s cha danh sch ID cc bi ht c chn trong bin i tng Session[SelectedSongs], l dng ArrayList. Khai bo trnh x l s kin ItemCommand cho DataGrid, s kin ny c gi khi mt ct dng button c kch.
11
Lp trnh ASP.NET
Khoa CNTT
Ni dung ca hm x l s kin ItemCommand ca DataGrid protected void dgSongs_ItemCommand(object source, DataGridCommandEventArgs e) { // l y id bi ht c a dng ang ch n int SongID = Convert.ToInt32(dgSongs.DataKeys[e.Item.ItemIndex]); // x l khi button Select c ch n if (e.CommandName == "SelectSong") { // ki m tra xem c id c a bi ht no c ch n hay khng if (Session["SelectedSongs"] != null) { ArrayList SelectedSongs = (ArrayList)Session["SelectedSongs"]; if (!SelectedSongs.Contains(SongID)) // cha c -> thm SelectedSongs.Add(SongID); } else // kh i t o session n u cha c 12
Lp trnh ASP.NET {
Khoa CNTT
ArrayList SelectedSongs = new ArrayList(); SelectedSongs.Add(SongID); Session["SelectedSongs"] = SelectedSongs; } } // hi n th thng tin s bi ht ch n lblStatus.Text = "S bi ht ch n: " + ((ArrayList)Session["SelectedSongs"]).Count; } 7. Khai bo x l khi user kch vo button To Album, button ny cho php chuyn user sang trang Album.aspx thm album gi chuyn ta dng: // chuy n h ng sang trang qu n l album Response.Redirect("album.aspx");
Cc control trn page Tn txtAlbumName TextBox Kiu Thuc tnh Din gii Nhp tn ca album mi 13
Khoa CNTT Chc nng to album Xc nhn xem user c nhp tn album hay khng, nu khng nhp th khng to mi Hin th cc bi ht chn trong trang SongManaging.aspx Hin thi thng tin: li hoc hon tt
dgSongs
DataGrid
DataKeyField = SongID
lblStatus
Label
9. Khai bo DataGrid dgSongs c 4 ct, 3 ct u l: tn bi ht, tn tc gi, th loi l dng Bound Column, ct cui cng l dng Button Column.
14
Lp trnh ASP.NET
Khoa CNTT
15
Lp trnh ASP.NET
Khoa CNTT
16
Lp trnh ASP.NET
Khoa CNTT
17
Lp trnh ASP.NET
Khoa CNTT
Hnh: khai bo ct Xa, c Command name l RemoveSong 10. To hm FillDataGrid: ly cc bi ht chn v hin th ln datagrid 1. protected void Page_Load(object sender, EventArgs e) 2. { 3. if (!IsPostBack) 4. { 5. FillDataGrid(); 6. } 7. } 8. private void FillDataGrid() 9. { 10. // ki m tra xem session c t o cha 11. if (Session["SelectedSongs"] == null) 12. { 13. lblStatus.Text = "<a href=\"default.aspx\">Cha ch n bi ht</a>"; 18
ArrayList SelectedSongs = (ArrayList)Session["SelectedSongs"]; 19. // ki m tra s item ch n 20. if (SelectedSongs.Count < 1) 21. { 22. lblStatus.Text = "<a href=\"default.aspx\">Cha ch n bi ht</a>"; 23. btnCreateAlbum.Enabled = false; return; 24. 25. } 26. 27. // c ch n bi ht 28. // t o danh sch id bi ht 29. string SongIDs=""; 30. foreach (int id in SelectedSongs) 31. SongIDs += id + ", "; 32. // xa d u ', cu i cng 33. SongIDs = SongIDs.Remove(SongIDs.Length - 2, 2); 34. // t o cc i t ng comand, connection, adapter 35. string scnn = ConfigurationManager.AppSettings["ConnectionString"].ToString(); 36. SqlConnection cnn = new SqlConnection(scnn); 37. SqlCommand cmd = new SqlCommand(); 38. cmd.Connection = cnn; 39. cmd.CommandText = "SELECT songid,songname, artistName, genrename FROM songs, artists, genres where songs.artistid = artists.artistid and songs.genreid = genres.genreid and songid in ("+SongIDs+")"; 40. 41. SqlDataAdapter da = new SqlDataAdapter(cmd); 42. // t o b ng lu d li u 43. DataTable dt = new DataTable(); 44. da.Fill(dt); 45. // fill vo datagrid 46. dgSongs.DataSource = dt.DefaultView; 47. dgSongs.DataBind(); 48. } 11. Khai bo trnh x l s kin ItemCommand ca datagrid, chc nng ny cho php user xa mt bi ht trong danh sch chn. 1. protected void dgSongs_ItemCommand(object source, DataGridCommandEventArgs e) 2. { 19
Lp trnh ASP.NET 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17.
Khoa CNTT // n u ch n c t xa if (e.CommandName == "RemoveSong" && Session["SelectedSongs"] != null) { ArrayList SelectedSongs = (ArrayList)Session["SelectedSongs"]; //l y id c a bi ht int SongID = Convert.ToInt32(dgSongs.DataKeys[e.Item.ItemIndex]); if (SelectedSongs.Contains(SongID)) { SelectedSongs.Remove(SongID); Session["SelectedSongs"] = SelectedSongs; FillDataGrid(); } }
12. Khai bo trnh x l s kin click ca button: To album Hm x l nh sau protected void btnCreateAlbum_Click(object sender, EventArgs e) { if (!IsValid) return; // n u ch a validation xong // l y tn c a album string AlbumName = txtAlbumName.Text.Trim(); // lu AlbumID int AlbumID=0; // th c hi n thao tc thm album m i string scnn = ConfigurationManager.AppSettings["ConnectionString"]; SqlConnection cnn = new SqlConnection(scnn); SqlCommand cmd = new SqlCommand(); cmd.Connection = cnn; cmd.CommandText = "Insert into Albums values(@albumname,@createDate); select MAX(AlbumID) from albums"; // t o tn album cmd.Parameters.Add("@albumname", SqlDbType.VarChar); cmd.Parameters["@albumname"].Value = AlbumName; // ngy t o album cmd.Parameters.Add("@createDate", SqlDbType.SmallDateTime); cmd.Parameters["@createDate"].Value = DateTime.Now.ToString(); try 20
Lp trnh ASP.NET {
Khoa CNTT
cnn.Open(); AlbumID = Convert.ToInt32(cmd.ExecuteScalar());// th c thi if (AlbumID > 0) // thm thnh cng lblStatus.Text = "Thm album thnh cng!"; } catch (SqlException ex) { Response.Write(ex.Message); } finally { cnn.Close(); } // thm thng tin vo b ng AlbumSong cmd.CommandText = "INSERT INTO AlbumSong values(@AlbumID,@SongID)"; cmd.Parameters.Add("@AlbumID", SqlDbType.Int); cmd.Parameters["@AlbumID"].Value = AlbumID; cmd.Parameters.Add("@SongID", SqlDbType.Int); ArrayList SelectedSongs = (ArrayList)Session["SelectedSongs"]; try { cnn.Open(); foreach (int id in SelectedSongs) { cmd.Parameters["@SongID"].Value = id; cmd.ExecuteNonQuery(); } } catch (SqlException ex) { Response.Write(ex.Message); } finally { cnn.Close(); } // xa t t c cc bi ch n SelectedSongs.Clear(); Session["SelectedSongs"] = SelectedSongs; // c p nh t l i giao di n dgSongs.DataSource = null; dgSongs.DataBind(); FillDataGrid() } 21
Lp trnh ASP.NET
Khoa CNTT
Bi 5.2:
Sinh vin b sung thm cc trang thm bi ht, thm ngh s , thm th lo i v thm bi ht. Xy d ng m t trang chnh, ch a menu o o o o Qu n l bi ht Album: g i trang SongManaging.aspx Thm bi ht Thm ngh s Thm th lo i
=oOo=
22