1. Trang chủ
  2. » Giáo án - Bài giảng

Bài giảng - giáo án: Bài tập thực hành môn lập trình web

102 880 2

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 102
Dung lượng 5,24 MB

Nội dung

TransClass = new TranslatorClass; // Store the object in a Session state variable.. Session["TransClass"] = TransClass; } else // Get the Session TransClass variable.. Please choose

Trang 1

Random mrndNumber;

int min, max;

public int Min

get { return max; }

set { max = value; }

// Get random numbers

mintFirstNumber = mrndNumber.Next(Min, Max+1); mintSecondNumber = mrndNumber.Next(Min, Max+1); mstrOp = mstrOp_list[mrndNumber.Next(0, 5)]; }

Trang 2

public int Answer()

Trang 3

// Run the following code the first time the page is displayed

if (!IsPostBack)

{

FlashCard1 = new FlashCardClass();

Session["FlashCard"] = FlashCard1;

}

else

{

// Get the Session FlashCard object

FlashCard1 = (FlashCardClass)Session["FlashCard"];

}

FlashCard1.Min = int.Parse(TextBox1.Text);

FlashCard1.Max = int.Parse(TextBox2.Text);

Trang 5

public string Text

// Restores translated text back to the original

public void Restore()

{

mstrText = mstrOriginal;

}

// Translates the value in the Text property

public void Translate()

{

string strWord;

string[] arrWords;

bool bCaps = false;

// Convert the string into an array using System.String arrWords = mstrText.Split(' ');

for (int intCount = 0; intCount <= arrWords.GetUpperBound(0); intCount++)

Trang 6

// Create a new Translator object

TransClass = new TranslatorClass();

// Store the object in a Session state variable

Session["TransClass"] = TransClass;

}

else

// Get the Session TransClass variable

TransClass = (TranslatorClass)Session["TransClass"];

// Check if ViewState variable exists

if (ViewState["bSwitch"] != null)

// Get the value from ViewState and switch it

bSwitch = !(bool)ViewState["bSwitch"];

else

// Set the switch

bSwitch = true;

// Save the new value in ViewState

ViewState["bSwitch"] = bSwitch;

Trang 8

public partial class SignOn : System.Web.UI.Page

// Set session variables

Session["Path"] = strPath;

Server.Transfer("FileManager.aspx");

}

else

{

Session["NewName"] = txtName.Text;

// Otherwise, report that user wasn't found

litNoAccount.Text = "<p>The name " + txtName.Text +

" wasn't found Check the name, or click " +

"<A href='NewAccount.aspx'>here</A> if you " +

"are a new user.</p>";

}

}

Trang 9

// Tell the user to choose another name

litNameExists.Text = "<p>The name " + txtName.Text +

" already exists Please choose a different one.</p>";

// Set the session variable

Session["Path"] = strPath;

Trang 10

strFiles = Directory.GetFiles(strPath);

// Get the short names for the files

for (int iCount = 0; iCount <= strFiles.GetUpperBound(0);

Trang 11

protected void butEdit_Click(object sender, System.EventArgs e) {

// If there is a file name, then start the editor

if (lstFiles.SelectedItem != null)

Response.Redirect("EditFile.aspx?file=" +

lstFiles.SelectedItem.ToString());

else

// Otherwise, display a message

litNoneSelected.Text = "<p>You must select a file.<p>"; }

protected void butDelete_Click(object sender, System.EventArgs e) {

// If there is a file name, then start the editor

// Otherwise, display a message

litNoneSelected.Text = "<p>You must select a file.<p>"; }

}

EditFile.aspx

Trang 12

if ((ViewState["Changed"] != null) &&

(ViewState["Changed"].ToString() == "true"))

// Set changed flag

ViewState["Changed"] = "true";

}

Trang 13

File.Delete(strPath + strFile);

// Create the file with new text

strmEditWrite = File.CreateText(strPath + "\\" + strFile);

strmEditWrite.Write(txtEditFile.Text);

strmEditWrite.Close();

// Reset changed flag

ViewState["Changed"] = "false";

}

}

NotAuthorized.aspx

Trang 14

// Set session variables

Session["Path"] = strPath;

Session["NewName"] = tbAccount.Text;

// Otherwise, report that user wasn't found

lit.Text = "<p>The name " + tbAccount.Text +

" wasn't found Check the name, or click " +

"<A href='NewAccount.aspx'>here</A> if you " +

"are a new user.</p>";

Trang 16

Directory.CreateDirectory(strPath);

SaveInfo(strPath);

// Set the session variable

Session["Path"] = strPath;

Trang 17

strPath = Session["Path"].ToString();

Trang 18

1 Thiết kế trang SwitchBoard.aspx

2 Thiết kế trang AddContact

Trang 20

Thêm vào Website class DataSet

Trang 24

Tạo truy vấn Insert:

Trang 26

TRở về Form AddContact

Thiết lập nguồn dữ liệu cho DropDownList drpContactTypes

Trang 28

Chuyển sang phần code behind:

Trang 29

Khai báo phương thức GetNewsId

Xử lý sự kiện click vào button Add

Trang 30

Sử dụng code tạo trang AddContact

Tạo class ConnectDb

Trang 31

Trang code behind :

Trang 34

Thiết kế trang Call.aspx

DropdownList

Trang 36

Source=|DataDirectory|\Products.mdb;Persist Security Info=True");

OleDbDataAdapter adapt = new OleDbDataAdapter("select ProductID , ProductName from Products", con);

DataTable tab = new DataTable();

adapt.Fill(tab);

Trang 37

OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data

Source=|DataDirectory|\Products.mdb;Persist Security Info=True");

OleDbDataAdapter adapt = new OleDbDataAdapter("select

SupplierName,ProductName,UnitPrice,Types from Products, Suppliers where

Products.SupplierID = Suppliers.SupplierID and ProductID = " +

Source=|DataDirectory|\Products.mdb;Persist Security Info=True");

OleDbDataAdapter adapt = new OleDbDataAdapter("select SupplierID, SupplierName from Suppliers", con);

DataTable tab = new DataTable();

adapt.Fill(tab);

Trang 38

{

con.Open();

List<int> idList = new List<int>();

OleDbDataReader reader = cmd.ExecuteReader();

int autoID = AutoID();

OleDbConnection con = new

OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data

Source=|DataDirectory|\Products.mdb;Persist Security Info=True");

OleDbCommand cmd1 = new OleDbCommand("insert into Products values (@id,@name,@supplierid,@unitprice,@quantity,@types)", con);

cmd1.Parameters.AddWithValue("@id", autoID);

cmd1.Parameters.AddWithValue("@name", TextBox1.Text);

cmd1.Parameters.AddWithValue("@supplierid",

DropDownList1.SelectedValue);

cmd1.Parameters.AddWithValue("@unitprice", TextBox2.Text);

cmd1.Parameters.AddWithValue("@quantity", TextBox3.Text);

cmd1.Parameters.AddWithValue("@types", TextBox4.Text);

using (con)

{

con.Open();

Trang 40

XemThem.aspx

DataList1 html

<asp:DataList ID="DataList1" runat="server">

<HeaderTemplate>

<strong><span style="font-size: 14pt">

<table border="0" style="width: 485px; height: 32px">

Trang 41

<ItemTemplate>

<strong><span style="font-size: 14pt">

<table border="0" style="width: 485px; height: 32px">

style="font-size: 13pt"></span></td>

<td style="width: 74px; text-align: left; height: 19px;">

<asp:Label ID="Label2" runat="server"Text='<%# Eval("LastName") %>' Font-Bold="False"></asp:Label><span

style="font-size: 13pt"></span></td>

<td style="width: 243px; text-align: center; height: 19px;" colspan="1">

<asp:Label

ID="Label3" runat="server" Text='<%#

Eval("FirstName") %>' Font-Bold="False"></asp:Label><span style="font-size: 13pt"></span></td>

<td style="width: 100px; text-align: left; height: 19px;">

<asp:Label ID="Label4" runat="server"

Text='<%# Eval("Phone") %>'

Font-Bold="False"></asp:Label></td>

Source=|DataDirectory|\Database1.mdb;Persist Security Info=True");

OleDbDataAdapter adap = new OleDbDataAdapter("SELECT ClassID FROM Classes", con);

DataTable table = new DataTable();

Trang 42

id = reader["StudentID"].ToString();

ListItem item = new ListItem(name, id);

Source=|DataDirectory|\Database1.mdb;Persist Security Info=True");

OleDbDataAdapter adap = new OleDbDataAdapter("SELECT * FROM Students WHERE StudentID = '"+DropDownList1.SelectedValue.ToString()+"'", con);

DataTable table = new DataTable();

string last = TextBox2.Text;

string first = TextBox3.Text;

string phone = TextBox4.Text;

int eff = 0;

string clas = DropDownList2.SelectedValue.ToString();

OleDbConnection con = new

OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data

Source=|DataDirectory|\Database1.mdb;Persist Security Info=True");

OleDbCommand cmd = new OleDbCommand("INSERT INTO Students

VALUES(TRIM('" + id + "'),TRIM('" + first + "'),TRIM('" + last + "'),TRIM('"

+ phone + "'),TRIM('" + clas + "'))",con);

Trang 44

while (reader.Read())

{

name = reader["LastName"].ToString() + " " +

reader["FirstName"];

id = reader["StudentID"].ToString();

ListItem item = new ListItem(name, id);

Literal1.Text = "Click vào nút 'Đồng ý' để xóa sinh viên " +

DropDownList1.SelectedItem + ".Click nút 'Hủy bỏ' để dừng";

Source=|DataDirectory|\Database1.mdb;Persist Security Info=True");

OleDbCommand cmd = new OleDbCommand("DELETE FROM Students WHERE

StudentID = '" + DropDownList1.SelectedValue.ToString() + "'", con);

Trang 46

Source=|DataDirectory|\Products.mdb;Persist Security Info=True");

OleDbDataAdapter adapt = new OleDbDataAdapter("select ProductID , ProductName from Products", con);

DataTable tab = new DataTable();

adapt.Fill(tab);

Trang 47

OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data

Source=|DataDirectory|\Products.mdb;Persist Security Info=True");

OleDbDataAdapter adapt = new OleDbDataAdapter("select

SupplierName,ProductName,UnitPrice,Types from Products, Suppliers where

Products.SupplierID = Suppliers.SupplierID and ProductID = " +

Source=|DataDirectory|\Products.mdb;Persist Security Info=True");

OleDbDataAdapter adapt = new OleDbDataAdapter("select SupplierID, SupplierName from Suppliers", con);

DataTable tab = new DataTable();

adapt.Fill(tab);

Trang 48

{

con.Open();

List<int> idList = new List<int>();

OleDbDataReader reader = cmd.ExecuteReader();

int autoID = AutoID();

OleDbConnection con = new

OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data

Source=|DataDirectory|\Products.mdb;Persist Security Info=True");

OleDbCommand cmd1 = new OleDbCommand("insert into Products values (@id,@name,@supplierid,@unitprice,@quantity,@types)", con);

cmd1.Parameters.AddWithValue("@id", autoID);

cmd1.Parameters.AddWithValue("@name", TextBox1.Text);

cmd1.Parameters.AddWithValue("@supplierid",

DropDownList1.SelectedValue);

cmd1.Parameters.AddWithValue("@unitprice", TextBox2.Text);

cmd1.Parameters.AddWithValue("@quantity", TextBox3.Text);

cmd1.Parameters.AddWithValue("@types", TextBox4.Text);

using (con)

{

con.Open();

Trang 51

</ SeparatorTemplate >

< HeaderTemplate >

< asp : Label ID ="Label1"

runat ="server" BackColor ="Blue" Font-Bold ="True"

(< asp : Label ID ="Label3"

runat ="server" Font-Italic ="True" ForeColor ="Red"

Text ='<%# Eval("PostDate","{0:dd/MM/yyyy}")

%>'></ asp : Label >

-

Trang 52

DESC"></ asp : AccessDataSource >

Thao tác tương tự đối với các Datalist còn lại

Trang Menu.ascx:

- HyperLink về Trang chủ: Text: TRANG CHỦ

- 1 DataList, thiết kế như sau:

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

CodeFile ="Menu.ascx.cs" Inherits ="Menu" %>

< asp : HyperLink ID ="HyperLink2" runat ="server"

BackColor ="#804000" Font-Bold ="True"

Trang 53

< HeaderTemplate >

< asp : Label ID ="Label1" runat ="server"

BackColor ="Blue" Font-Bold ="True"

Font-Size ="Medium"

ForeColor ="White" Text ="CÁC CH Ủ ĐỀ"

Width ="100%"></ asp : Label >

</ HeaderTemplate >

< ItemTemplate >

< asp : HyperLink ID ="HyperLink1"

runat ="server" BackColor ="#804000"

</ asp : DataList >< asp : AccessDataSource

ID ="AccessDataSource1" runat ="server"

DataFile ="~/App_Data/Forum.mdb"

SelectCommand ="SELECT [SubjectId],

[SubjectName] FROM

[TbSubject]"></ asp : AccessDataSource >

[TbSubject]"></ asp : AccessDataSource >

Kéo trang Menu.ascx vào phần Left của MassterPage

Trang TopicList: gồm Gridview +AccessDatasource

Trang 54

HeaderText ="T&#234;n b&#224;i vi ết" >

< HeaderStyle HorizontalAlign ="Left" />

Trang 55

< FooterStyle BackColor ="#5D7B9D"

Font-Bold ="True" ForeColor ="White" />

< PagerStyle BackColor ="#284775"

ForeColor ="White" HorizontalAlign ="Center" />

< SelectedRowStyle BackColor ="#E2DED6" Bold ="True" ForeColor ="#333333" />

< HeaderStyle BackColor ="#5D7B9D"

Font-Bold ="True" ForeColor ="White" />

< EditRowStyle BackColor ="#999999" />

< AlternatingRowStyle BackColor ="White"

ForeColor ="#284775" />

</ asp : GridView >

< asp : AccessDataSource ID ="AccessDataSource1"

runat ="server" DataFile ="~/App_Data/Forum.mdb"

DeleteCommand ="DELETE FROM [TbTopic] WHERE [TopicId] = ?" InsertCommand ="INSERT INTO [TbTopic] ([TopicId], [Title], [Content], [Image],

[PostDate], [PostUser], [SubjectId]) VALUES (?, ?,

?, ?, ?, ?, ?)"

SelectCommand ="SELECT TbTopic.TopicId,

TbTopic.Title, TbTopic.Content, TbTopic.[Image], TbTopic.PostDate, TbTopic.PostUser,

TbTopic.SubjectId, TbSubject.SubjectName FROM

(TbTopic INNER JOIN TbSubject ON TbTopic.SubjectId

= TbSubject.SubjectId) WHERE (TbTopic.SubjectId =

?) ORDER BY TbTopic.PostDate DESC"

UpdateCommand ="UPDATE [TbTopic] SET [Title]

= ?, [Content] = ?, [Image] = ?, [PostDate] = ?, [PostUser] = ?, [SubjectId] = ? WHERE [TopicId] =

Ngày đăng: 17/04/2014, 18:18

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

w