標籤:style blog http io ar os 使用 sp for
走過了牛腩老師的新聞發布系統,漫遊過了孫鑫老師的Html,在427沐浴第一縷冬日陽光的美麗月底,小編迎來了北大青鳥的Asp.net,北大青鳥,高大上的趕腳有麼有,哈哈哈,開始了小編的.net之旅。
首先,小編來簡單介紹一下Asp.net,她是.NET FrameWork的一部分,是一項微軟公司的技術,是一種使嵌入網頁中的指令碼可由網際網路伺服器執行的伺服器端指令碼技術,它可以在通過HTTP請求文檔時再在Web伺服器上動態建立它們。 指 Active Server Pages(動態伺服器頁面) ,運行於 IIS(Internet Information Server 服務,是Windows開發的Web伺服器)之中的程式。該博文小編主要帶領小夥伴們學習一個例子,利用Asp.net實現下拉框和列表框的連動,以及小編在這個過程中遇到的問題,記錄一下小編的學習過程。
首先,我們需要建立一個資料庫:
<span style="font-size:18px;">create database department --建立資料庫use departmentcreate table TDepartment --穿件TDepartment表( depID int primary key, deName varchar(30) not null)insert into TDepartment values(1,'教務')insert into TDepartment values(2,'後勤服務中心')insert into TDepartment values(3,'辦公室')create table emp --建立emp表( empID int primary key, empName varchar(30) not null, depID int foreign key references TDepartment(depID) )insert into emp values(1,'王小剛',1)insert into emp values(2,'李剛',1)insert into emp values(3,'張紅',2)insert into emp values(4,'張波',3)</span>
接著,開啟VS,我們建立一個項目,如所示:
接著,編寫代碼,我們需要串連資料庫,代碼編寫如下:
<span style="font-size:18px;">/**********************************************'文 件 名: DBCon'內 容:串連資料庫'功 能:串連資料庫'作 者:丁國華'產生日期: 2014年11月28日 14:59:45'版本號碼:V2.0'**********************************************/using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Data.SqlClient;namespace departMent{ public class DBCon { public DBCon() { } public static SqlConnection createConnection() { //串連資料庫 SqlConnection con = new SqlConnection("server=(local);database=department;uid=sa;pwd=123456;"); return con; } }}</span> 緊接著,我們來編寫Web的代碼,如下:
<span style="font-size:18px;">/**********************************************'文 件 名: WebForm1'內 容:實現下拉框和列表框的連動'功 能:實現下拉框和列表框的連動'作 者:丁國華'產生日期: 2014年11月28日 15:05:45'版本號碼:V2.0'**********************************************/using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Data.SqlClient;namespace departMent{ public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if(!this.IsPostBack) { SqlConnection con = DBCon.createConnection(); con.Open(); //顯示部門 SqlCommand cmd = new SqlCommand("select * from TDepartment",con); SqlDataReader sdr = cmd.ExecuteReader(); this.ddlDep.DataSource = sdr; this.ddlDep.DataTextField = "deName"; this.ddlDep.DataValueField= "depID"; this.ddlDep.DataBind(); sdr.Close(); //顯示員工 SqlCommand cmdEmp = new SqlCommand("select * from emp where depID="+this.ddlDep.SelectedValue ,con); SqlDataReader sdrEmp = cmdEmp.ExecuteReader(); while(sdrEmp.Read()) { this.lBoxEmp.Items.Add(new ListItem(sdrEmp.GetString(1),sdrEmp.GetInt32(0).ToString())); } sdrEmp.Close(); //關閉串連 con.Close(); } } protected void ddlDep_SelectedIndexChanged(object sender, EventArgs e) { this.lBoxEmp.Items.Clear(); SqlConnection con = new SqlConnection("server=(local);database=department;uid=sa;pwd=123456;"); con.Open(); //顯示員工 SqlCommand cmdEmp = new SqlCommand("select * from emp where depID=" + this.ddlDep.SelectedValue,con); SqlDataReader sdrEmp = cmdEmp.ExecuteReader(); while (sdrEmp.Read()) { this.lBoxEmp.Items.Add(new ListItem(sdrEmp.GetString(1), sdrEmp.GetInt32(0).ToString())); } sdrEmp.Close(); //關閉串連 con.Close(); } }}</span> Ctrl+F5運行,咳咳咳,滿懷期待,期待運行效果會是什麼樣子的nie,啊哦,出錯了,如下所示:
這個錯是為神馬nie,再此小編來講解一下解決方案,以Win8為例:
第一步:進入控制台:
第二步:選擇程式選項:
第三步:選擇程式和功能:
第四步:啟用和關閉Windows功能:
第五步:勾選Internet Information Services可承載的Web核心:
最後點擊“確定”等待更改完成。然後重啟電腦,設定完成,我們再來運行,效果如下:
點擊相應的下拉框,列表框會隨之變化,但是這種連動不提倡使用,在使用者串連少的時候可以使用,如果大量的使用者串連,會對資料庫造成很大的負擔。
小編寄語:該博文,小編主要講解了Asp.net方面的知識,第一次接觸,小宇宙頓時爆發了`(*∩_∩*)′,小編主要敲了一個例子,美其名曰利用Asp.net實現下拉框和列表框的連動,以及在敲這個小例子的過程中遇見的困難,希望可以協助到其他小夥伴,北大青鳥Asp.net未完,待續......
HTTP 錯誤 403.14 - Forbidden0--Asp.net實現下拉框和列表框的連動