Ajax value transfer (three layers ):
1. Write in front-end page js to obtain the page data, pass the value to. CS, and return the value to determine whether the page is successful.
Code:
1 < Script Language = "JavaScript" > 2 VaR Title = $ ( " # Tb1 " ). Val (); 3 VaR Contents = $ ( " # Tb2 " ). Val (); 4 $. Ajax ({ 5 Type: " Post " , 6 Datatype: " Josn " , 7 Data: {Jian: title, jian2: Contents }, 8 Success: Function (Data ){ 9 If (Data. Result = " 0 " { 10 Alert ( " Failed to add " ); 11 } 12 Else { 13 Alert ( " Added " ); 14 } 15 }, 16 Error: Function (Data ){ 17 Alert ( " Error " ); 18 } 19 }) 20 21 </ Script >
2. In the background. CS, the aim is to pass the data obtained from the foreground to BLL.
Code:
1 Protected Void Bt1_click ( Object Sender, eventargs E) 2 { 3 String Title = context. request [ " Jian " ]; // These two statements store the values obtained in front-end Js. 4 String Conte = context. request [ " Jian2 " ]; 5 Int Result = New Bll. articlesmanager. Add (title, content ); // Transmits a value to BLL and returns it. 6 Response. Write ( " {\ "Result \":\" " + Result + " \"} " ); Returns the obtained value. 7 // "{" Key ":" value "}" is in this format. If not, it must be transferred. 8 // "{\" Result \ ": \" "+ Result + "\"}" 9 }
3. Obtain the value in. CS in BLL and pass it to Dal.
Code:
1 Public Static IntAdd (StringTitle,StringContent)2 {3Return NewDal. articlesservice. Add (title, content );4}
4. Adding data in the dal is the same as normal,
Code:
1 Public Static Int Add (String Title, String Content) 2 { 3 String SQL = " Insert into articles (title, contents) values (@ title, @ contents) " ; 4 Sqlparameter [] par = New Sqlparameter [] { 5 6 // New sqlparameter ("@ ID", articels. ID ), 7 New Sqlparameter ( " @ Title " , Title ), 8 New Sqlparameter ( " @ Contents " , Content) 9 }; 10 Return Dbhelper. adddeed (SQL, par ); 11 }