TransClass = new TranslatorClass; // Store the object in a Session state variable.. Session["TransClass"] = TransClass; } else // Get the Session TransClass variable.. Please choose
Trang 1Random 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 2public 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 5public 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 8public 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 10strFiles = Directory.GetFiles(strPath);
// Get the short names for the files
for (int iCount = 0; iCount <= strFiles.GetUpperBound(0);
Trang 11protected 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 12if ((ViewState["Changed"] != null) &&
(ViewState["Changed"].ToString() == "true"))
// Set changed flag
ViewState["Changed"] = "true";
}
Trang 13File.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 16Directory.CreateDirectory(strPath);
SaveInfo(strPath);
// Set the session variable
Session["Path"] = strPath;
Trang 17strPath = Session["Path"].ToString();
Trang 181 Thiết kế trang SwitchBoard.aspx
2 Thiết kế trang AddContact
Trang 20Thêm vào Website class DataSet
Trang 24Tạo truy vấn Insert:
Trang 26TRở về Form AddContact
Thiết lập nguồn dữ liệu cho DropDownList drpContactTypes
Trang 28Chuyển sang phần code behind:
Trang 29Khai báo phương thức GetNewsId
Xử lý sự kiện click vào button Add
Trang 30Sử dụng code tạo trang AddContact
Tạo class ConnectDb
Trang 31Trang code behind :
Trang 34Thiết kế trang Call.aspx
DropdownList
Trang 36Source=|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 37OleDbConnection(@"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 40XemThem.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 42id = 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 44while (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 46Source=|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 47OleDbConnection(@"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 52DESC"></ 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 54HeaderText ="Tên bà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] =