基於ASP.NET技術的駕校網頁設計

來源:互聯網
上載者:User
asp.net|設計|網頁|網頁設計

  摘 要 本文以駕校管理系統為例,介紹如何利用asp.net和SQL server 2000來進行動態網頁設計,以滿足使用者對資料庫即時更新以及查詢的要求。

  關鍵詞 asp.net; c#; SQL server 2000

  引言

  近年來,隨著Internet的迅速發展以及網頁製作技術的日臻完善,駕校資訊管理系統軟體的設計也日趨簡單化和正常化。這裡我們將採用asp.net動態網頁技術,通過編寫c#指令碼語言對SQL server 2000資料庫進行操作,以實現系統中的諸多功能,如“報名錄入”,“報名資料查詢”,“確認合格學員”,“學員資料查詢”等等。

  asp.net 、c#和ado.net

  1、ASP.NET是microsoft推出的一種強大的Web伺服器端技術,與ASP相比,ASP.NET擁有更高效能的編譯特性與緩衝機制。支援多種開發語言,包括C#、J#、Visual Basic和JScript。ASP.NET分離程式碼與顯示內容,使代碼看起來更簡潔。由於ASP.NET的程式碼是編譯過的,所以執行時會比ASP快很多。

  2、C#語言是Microsoft針對.Net平台開發的一種全新的程式設計語言。它是一種面相對象的開發語言,因此具有封裝、繼承和多態性。C#文法簡潔,效率高,並且可以與以其他.NET語言編寫的代碼進行相容。

  3、ADO.NET用於在 Microsoft .NET 平台中提供資料訪問服務。它作用在伺服器端通過執行SQL命令對資料庫進行訪問和更新。ADO.net主要包括Connection,Dataset和Command三個對象, 它們的主要功能如下:

  Connection對象:串連資料庫;
  Dataset對象:存取資料庫的內容;
  Command對象:對資料庫執行查詢指令,以及執行非查詢(更新、刪除和添加等)命令。

  網頁設計


  下面將以駕校管理系統軟體中的“學員資料查詢與修改”為例說明有關網頁設計問題。(c#)

  首先,在SQL server 2000中建一個名為”jx”的資料庫,包含表”車型”、”學員資料” 和視圖”V學員資料”。

  其次,”學員資料查詢與修改”主要由三個頁面組成,為:xy_search.aspx、xy_search_win.aspx 、 xy_search_detail.aspx,分別用於輸入查詢條件、顯示查詢結果、顯示其中一條記錄的詳細資料並根據需要進行修改。

  1、xy_search.aspx

  在該頁面中根據使用者的查詢條件設計頁面,方法:用滑鼠左鍵選中工具箱web表單中的控制項,拖拽到頁面,放置到合適的位置,然後設定該控制項的屬性,其它的控制項類似。如圖1所示。


圖1

  頁面設計好要進行後台代碼的編寫,在xy_search.aspx.cs中進行:


using System.Data.SqlClient;
using System.Data.SqlTypes;// 添加命名空間

public string name1
{
 get
 {
  return
  TextBox9.Text.Trim();
 }
}

//定義TextBox9中的內容為公用變數,設定文字框的Visible屬性為False

private void Page_Load(object sender, System.EventArgs e)
{
 Page.RegisterStartupScript("focus", "<script language= javascript> document.getElementById('TextBox1').focus();</script>");//插入java指令碼(聚焦TextBox1)

 if(!IsPostBack)// 檢查是否是第一次載入本頁
 {
  SqlConnection myConnection=new  SqlConnection("server=JXSERVER;uid=sa;pwd=1818;database=jx");

  myConnection.Open();//串連資料庫
  SqlDataAdapter myAdapter=new SqlDataAdapter("select * from 車型",myConnection);// 建立myAdapter對象
  DataSet myDataSet=new DataSet(); //建立一個myDataSet對象 
  myAdapter.Fill(myDataSet,"車型");//把執行select語句得到的記錄添加到myDataSet中   DropDownList1.DataSource=myDataSet.Tables["車型"].DefaultView;
  //指定DropDownList1資料來源
  DropDownList1.DataTextField="車型";
  //指定DropDownList1的文本值為“車型”表中‘車型’欄位
  DropDownList1.DataValueField="車型ID";

  //指定DropDownList1的Value值為“車型”表中‘車型ID’欄位 

  DropDownList1.DataBind();//綁定資料
  DropDownList1.SelectedValue="C1";//指定DropDownList1的預設Value值為C1
  myConnection.Close();//關閉資料庫連接
 }
}// 在此處放置使用者代碼以初始化頁面

/*由於在實現刪改功能的同時可以實現查詢功能因此僅以刪改功能為例*/

private void Button2_Click(object sender, System.EventArgs e)
{
 TextBox9.Text = "where 姓名 like '" + TextBox1.Text.Trim() + "'";
 //定義姓名模糊查詢條件陳述式

 Server.Transfer("xy_search_win.aspx");

 //傳送姓名查詢條件陳述式到第二個頁面
}

private void Button4_Click(object sender, System.EventArgs e)
{
 TextBox9.Text = "where 證件號碼= '" + TextBox2.Text.Trim() + "'";
 //證件號碼= TextBox2.Text的值
 Server.Transfer("xy_search_win.aspx");
}

private void Button6_Click(object sender, System.EventArgs e)
{
 extBox9.Text = "where 學號= '" + TextBox3.Text.Trim() + "'";
 //學號= TextBox3.Text的值
 Server.Transfer("xy_search_win.aspx");
}
private void Button8_Click(object sender, System.EventArgs e)
{
 TextBox9.Text = "where 學號 like '"+TextBox4.Text.Trim()+"%'";
 //學號與TextBox4.Text的值模糊比對
 Server.Transfer("xy_search_win.aspx");
}

private void Button10_Click(object sender, System.EventArgs e)
{
 TextBox9.Text = "where 報名日期 between '"+ TextBox5.Text.Trim() + "' and '" + TextBox6.Text.Trim() + "'";
 //TextBox5.Text>=報名日期<=TextBox6.Text
 Server.Transfer("xy_search_win.aspx");
}

private void Button12_Click(object sender, System.EventArgs e)
{
 TextBox9.Text = "where 報考車種 = '"+DropDownList1.SelectedValue.ToString().Trim()+"'";
 //報考車種=DropDownList1選中項的value值
 Server.Transfer("xy_search_win.aspx");
}

private void Button14_Click(object sender, System.EventArgs e)
{
 TextBox9.Text = "where 欠款金額 between "+Convert.ToDecimal(TextBox7.Text.Trim())+" and "+Convert.ToDecimal(TextBox8.Text.Trim())+" ";

 //注意要將字串類型轉換成貨幣類型decimal

 Server.Transfer("xy_search_win.aspx");
}

  2、xy_search_win.aspx

  在該頁面中顯示查詢結果,如圖2所示,此外還可以選中任一條記錄進行刪除和編輯操作。注意:在DataGrid1的”屬性產生器”中添加‘刪除’和‘選擇’列,並將選擇列的文本設成‘編輯’。

圖2

  xy_search_win.aspx.cs代碼如下:

using System.Data.SqlClient;//添加命名空間

public class xy_search_win : System.Web.UI.Page
{
 public xy_search sourcepage;

 //定義第一個頁面為當前頁面的源頁面

 public string name1
 {
  get
  {
   return DataGrid1.Items[DataGrid1.SelectedIndex].Cells[2].Text.ToString();
  }
 }//定義所選行的‘學號’為公開變數

 private void search1()//定義一個查詢子程式
 {
  SqlConnection myConnection= new SqlConnection ("server=JXSERVER;uid=sa;pwd=1818;database=jx");

  myConnection.Open();//串連資料庫jx
  SqlDataAdapter adapter1= new SqlDataAdapter ("select 學號,姓名,報考車種,連絡方式,證件號碼,培訓方式,欠款金額,備忘 from V學員資料 "+ TextBox1.Text + " order by 學號", myConnection); //查詢檢視

  DataSet myDataSet=new DataSet(); Adapter1.Fill(myDataSet,"result1");     
  DataGrid1.DataSource=myDataSet.Tables["result1"];
  DataGrid1.DataKeyField="學號";//指定關鍵字段為學號
  DataGrid1.DataBind();//綁定資料
  myConnection.Close();
 }

 private void Page_Load(object sender, System.EventArgs e)
 {
  if(!IsPostBack)
  {
   sourcepage=(xy_search)Context.Handler;
   // 使用Context.Handler屬性來獲得前一個頁面執行個體對象的引用
   TextBox1.Text=sourcepage.name1;//將第一個頁面傳過來的查詢語句賦給TextBox1
   search1();//調用查詢子程式顯示查詢結果
  }
 }

 private void DataGrid1_DeleteCommand(object source,System.Web.UI.WebControls.DataGridCommandEventArgs e)//”刪除”按鈕作業碼
 {
  SqlConnection myConnection =new SqlConnection("server=JXSERVER;uid=sa;pwd=1818;database=jx");

  myConnection.Open();
  string mydelete="delete from 學員資料 where 學號='"+DataGrid1.DataKeys[(int)e.Item.ItemIndex]+"'";//刪除“學員資料”表中‘學號’等於所選行對應的學號

  SqlCommand myCommand=new SqlCommand(mydelete, myConnection);//建立myCommand對象
  myCommand.ExecuteNonQuery();//執行sql語句
  myConnection.Close();
  search1();//重新調用子程式重新整理頁面;
 }
 private void
 DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
 //”編輯”按鈕命令
 {
  Server.Transfer("xy_search_detail.aspx");
 }

 //轉到第三個頁面進行詳細編輯
}
  3、xy_search_detail.aspx

  在該頁面中可以對學員資料中帶*號的項進行編輯,其它項的文字框的屬性’ReadOnly’設定成true,不可以進行修改,如圖3所示

圖3

using System.Data.SqlClient;

public class xy_search_detail : System.Web.UI.Page
{
 public xy_search_win sourcepage;//設定第二個頁面為當前頁面的源頁面
 private void myselect1()//定義查詢子程式
 {
  SqlConnection myConnection =new SqlConnection("server=JXSERVER;uid=sa;pwd=1818;database=jx");

  myConnection.Open();
  string myselect1="select 學號,姓名,性別,報考車種,證件號碼,手機,小靈通,住家電話,辦公電話,備忘 from 學員資料 where 學號='"+TextBox10.Text+"'";//查詢檢視,TextBox10.Text為第二個頁面傳遞過來的學號

  SqlCommand myCommand1=new SqlCommand(myselect1, myConnection);
  SqlDataReader myReader1 =myCommand1.ExecuteReader();//執行讀操作
  while(myReader1.Read())//該條記錄不為空白時執行{…}裡的程式
  {
   TextBox1.Text=myReader1.GetString(0);//學號賦值給TextBox1
   TextBox2.Text=myReader1.GetString(1);//姓名
   TextBox3.Text=myReader1.GetString(2);//性別
   TextBox4.Text=myReader1.GetString(3);//報考車種
   TextBox5.Text=myReader1.GetString(4);//證件號碼
   if(myReader1.IsDBNull(5)==false)
   {
    TextBox9.Text=myReader1.GetString(5);
    //手機
   }
   //判斷該記錄的手機這一列資料是否為空白
   else {TextBox9.Text=""; }
   if(myReader1.IsDBNull(6)==false)
   {TextBox11.Text=myReader1.GetString(6);//小靈通 }
   else {TextBox11.Text="";}
   if(myReader1.IsDBNull(7)==false)
   {TextBox12.Text=myReader1.GetString(7);//住家電話}
   else {TextBox12.Text="";}
   if(myReader1.IsDBNull(8)==false)
   {TextBox13.Text=myReader1.GetString(8);//辦公電話
  }
  else {TextBox13.Text="";}
  if(myReader1.IsDBNull(9)==false)
  {TextBox7.Text=myReader1.GetString(9);}//備忘
  else {TextBox7.Text=""; }
 }
 myReader1.Close();
 myConnection.Close();
}

private void Page_Load(object sender, System.EventArgs e)
{
 if(!IsPostBack)
 {
  sourcepage=(xy_search_win)Context.Handler;
  TextBox10.Text=sourcepage.name1;//將第二個頁面的‘學號’值傳到第三個頁面
  Label3.Text="";//提示標籤置空
  myselect1();//調用查詢資料子程式
 }
}

private void Button1_Click(object sender, System.EventArgs e) //“修改“按鈕命令
{
 SqlConnection myConnection=new SqlConnection("server=JXSERVER;uid=sa;pwd=1818;database=jx");

 myConnection.Open();
 string lxfs=TextBox9.Text.Trim()+"+"+TextBox11.Text.Trim()+"+"+_
TextBox12.Text.Trim()+"+"+TextBox13.Text.Trim()+"+"; //定義連絡方式

 string myupdate= "update 學員資料 set _
手機='"+TextBox9.Text.Trim()+"',小靈通='"+TextBox11.Text.Trim()+"',_
住家電話='"+TextBox12.Text.Trim()+"',辦公電話='"+TextBox13.Text.Trim()+"',_
備忘='"+TextBox7.Text+"',連絡方式='"+lxfs+"' where 學號="+TextBox10.Text.Trim()+"";

 SqlCommand myCommand2 = new SqlCommand(myupdate,myConnection);
 MyCommand2.ExecuteNonQuery();//
 Label3.Text="修改成功,請繼續!";
 myConnection.Close();
}

private void Button2_Click(object sender, System.EventArgs e)//“重設”按鈕命令
{
 Label3.Text="";//提示標籤置空
 myselect1();//調用查詢資料子程式
}

}

   結束語

  本文簡單介紹利用了如何利用VisualStudio.NET2003開發ASP.NETWeb應用程式。開發出來的該系統具有的查詢快捷、儲存量大、可靠性高等特點,有效地提高了駕校管理系統的效率。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.