Recently, I was working on a device Repair System for the school. Due to the limited level, I seldom used Ajax knowledge when I was working on projects. To achieve better results, I checked the relevant materials, refer to the online tutorial to write the followingProgram, Please correct me more.
Development Environment configuration: Visual Studio 2008 SQL Server 2008
Source code: brushless newest Cascade
1. Create a Test Database
Two tables are used, one is department, and the other is usedepart.
Table: Department
Table: usedepart
2. Create a New myclass class
Public class myclass {public dataset getlist (string table, string where) {string connstr = configurationmanager. connectionstrings ["conn"]. connectionstring; using (sqlconnection conn = new sqlconnection (connstr) {stringbuilder strsql = new stringbuilder (); strsql. append ("select * from "). append (table); If (! String. isnullorempty (where) {strsql. append ("where "). append (where);} using (sqlcommand cmd = new sqlcommand (strsql. tostring (), Conn) {Conn. open (); dataset DS = new dataset (); sqldataadapter da = new sqldataadapter (); DA. selectcommand = cmd; DA. fill (DS, "ds"); Return DS ;}}}}
3. default. aspx pageCodeJavaScript code
<SCRIPT type = "text/JavaScript"> $ (function () {$ ("# <% = ddldepartment. clientid %> "). change (function () {$ ("# ddlusedepart "). load ("loadusedepart. aspx? Indepartid = "+ $ (" # "+" <% = ddldepartment. clientid %> "+" option: Selected "). val () ;}); </SCRIPT>
4. loadusedepart. aspx background code
Protected void page_load (Object sender, eventargs e) {int ID; If (Int. tryparse (request. params ["indepartid"]. tostring (), Out ID) {response. write (getusedepart (ID); response. end () ;}} private string getusedepart (int id) {stringbuilder sb = new stringbuilder (); dataset DS = new myclass (). getlist ("usedepart", "departid =" + id); If (Ds. tables. count> 0) {for (INT I = 0; I <Ds. tables [0]. rows. count; I ++) {sb. append ("<option value = '" + Ds. tables [0]. rows [I] ["ID"]. tostring () + "'>" + Ds. tables [0]. rows [I] ["name"]. tostring () + "</option>") ;}} return sb. tostring ();}