一个B/S结构自动二次请求需求的实现
生活随笔
收集整理的這篇文章主要介紹了
一个B/S结构自动二次请求需求的实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
有這樣一個需求,當客戶輸入卡號,查詢出客戶的金額,如果金客大于等于1000,提示詢問客戶是否對帳,如果客戶選擇對帳,就改對客戶數據進行對帳操作(其實就是改丟這個客戶的對帳標志),前提是在B/S架構下實現這個功能。 當看到這個需求時,先分解一下,來弄清數據的層次和操作的層次。 首先數據肯定是在服務器端存放,客戶端需要的數據都需要從服務端得到。操作層次是客戶先把卡號送到服務端,讓服務判斷用戶金客是否大于等于1000,如果不滿足條件,就向客戶端提示用戶金客不足1000,不能對帳,如果滿足條件,就向客戶端詢問用戶是否對帳,如果客戶選擇確定對帳,就讓服務端更新數據標志,如果客戶選擇不對帳,流程就此完結。 現在從技術角度分析一下這個過程,B/S架構是基于HTTP協議,這個協議的特點是B端有請求,S端才有回應,一問一答的形式,當然不問,也就不答?,F在就基于HTTP來考慮一下,當客戶端提交卡號時,是客戶端的第一次請求;服務端得到卡號從數據庫中得到金額,然后跟據金額是否大于等于1000來向用戶發送不同的請求,如果客戶端大于等于1000,向客戶端發的應答是帶有詢問提示框的,并有二次請求代碼,如果客戶金額少于1000,應答的是一個提示金額不足的代。 提下來的焦就是怎么在第一次應答中,帶有第二次請求的代碼,這里的代碼是請求的代碼,一定是在客戶端,也當然是腳本代碼了。 下面看一下代碼實現: aspx代碼: html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <input type="hidden" name="mark" /> <div> <script language="javascript" type ="text/javascript"> function PostPage() { document.all.mark.value = "mark_value"; document.form1.submit(); } </script> </div> <asp:TextBox ID="Number_TB" runat="server"></asp:TextBox> <asp:Button ID="Sub_But" runat="server" OnClick="Sub_But_Click" Text="提交" ;80px"/> </form> </body> </html> 在aspx中用一個Hide標簽,是用來存放標志的,在第二次客戶端提交是應用。 PostPage方法是客戶端向后臺提交請求的代碼。 C#代碼: using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class Test : System.Web.UI.Page { string constr = @"server=.\sqlexpress;database=testdb;uid=sa;pwd=sa;"; protected void Page_Load(object sender, EventArgs e) { if (IsPostBack) { //實現對前臺標志的判斷 string mark = Request.Form["mark"] != null ? Request.Form["mark"] : ""; if (mark == "mark_value") { WriteMark(); } } } /// <summary> /// 更新數據庫,寫數據據對帳標志 /// </summary> public void WriteMark() { string SQL = "update member set accmark=1 where number=@number"; SqlConnection con = new SqlConnection(constr); SqlCommand cmd = new SqlCommand(); try { cmd.CommandText = SQL; cmd.Connection = con; cmd.Parameters.Clear(); cmd.Parameters.Add("@number", SqlDbType.VarChar).Value = Number_TB.Text; con.Open(); cmd.ExecuteNonQuery(); } catch { this.ClientScript.RegisterStartupScript(this.GetType(), "messages", "<script>alert('更新異常!')</script>"); } finally { con.Close(); } } protected void Sub_But_Click(object sender, EventArgs e) { //按照卡號查詢金額 double amount = 0d; string SQL = "select amount from member where number=@number and accmark=0"; SqlConnection con = new SqlConnection(constr); SqlCommand cmd = new SqlCommand(); try { cmd.CommandText = SQL; cmd.Connection = con; cmd.Parameters.Clear(); cmd.Parameters.Add("@number", SqlDbType.VarChar).Value = Number_TB.Text; con.Open(); amount = Convert.ToDouble(cmd.ExecuteScalar()); } catch { this.ClientScript.RegisterStartupScript(this.GetType(), "message", "<script>alert('查詢異常!')</script>"); } finally { con.Close(); } //判斷金額是否大于500 if (amount > 500) { //下面的代碼是調用二次請求的代碼,用confirm來提示用戶做判斷 string s = " <script language='javascript'>if(confirm('您的金額為:" + amount + "元,確定要對帳嗎?')){ PostPage(); }</script>"; this.ClientScript.RegisterStartupScript(this.GetType(), "clientScript", s); } else { string s = " <script language='javascript'>alert('您的金額不足1000元');</script>"; this.ClientScript.RegisterStartupScript(this.GetType(), "clientScript", s); } } } 數據庫結構:
| 列名 | 數據類型 | 備注 |
| id | Int | 編號主鍵,自動增長 |
| number | Varchar(50) | 卡號 |
| amount | Float | 金額 |
| accmark | int | 對帳標志 |
轉載于:https://blog.51cto.com/axzxs/164269
總結
以上是生活随笔為你收集整理的一个B/S结构自动二次请求需求的实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 简短高冷个性女生游戏名字104个
- 下一篇: 互动直播娱乐平台宣传文案30句