一个B/S结构自动二次请求需求的实现
生活随笔
收集整理的這篇文章主要介紹了
一个B/S结构自动二次请求需求的实现
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
有這樣一個(gè)需求,當(dāng)客戶輸入卡號(hào),查詢出客戶的金額,如果金客大于等于1000,提示詢問(wèn)客戶是否對(duì)帳,如果客戶選擇對(duì)帳,就改對(duì)客戶數(shù)據(jù)進(jìn)行對(duì)帳操作(其實(shí)就是改丟這個(gè)客戶的對(duì)帳標(biāo)志),前提是在B/S架構(gòu)下實(shí)現(xiàn)這個(gè)功能。 當(dāng)看到這個(gè)需求時(shí),先分解一下,來(lái)弄清數(shù)據(jù)的層次和操作的層次。 首先數(shù)據(jù)肯定是在服務(wù)器端存放,客戶端需要的數(shù)據(jù)都需要從服務(wù)端得到。操作層次是客戶先把卡號(hào)送到服務(wù)端,讓服務(wù)判斷用戶金客是否大于等于1000,如果不滿足條件,就向客戶端提示用戶金客不足1000,不能對(duì)帳,如果滿足條件,就向客戶端詢問(wèn)用戶是否對(duì)帳,如果客戶選擇確定對(duì)帳,就讓服務(wù)端更新數(shù)據(jù)標(biāo)志,如果客戶選擇不對(duì)帳,流程就此完結(jié)。 現(xiàn)在從技術(shù)角度分析一下這個(gè)過(guò)程,B/S架構(gòu)是基于HTTP協(xié)議,這個(gè)協(xié)議的特點(diǎn)是B端有請(qǐng)求,S端才有回應(yīng),一問(wèn)一答的形式,當(dāng)然不問(wèn),也就不答。現(xiàn)在就基于HTTP來(lái)考慮一下,當(dāng)客戶端提交卡號(hào)時(shí),是客戶端的第一次請(qǐng)求;服務(wù)端得到卡號(hào)從數(shù)據(jù)庫(kù)中得到金額,然后跟據(jù)金額是否大于等于1000來(lái)向用戶發(fā)送不同的請(qǐng)求,如果客戶端大于等于1000,向客戶端發(fā)的應(yīng)答是帶有詢問(wèn)提示框的,并有二次請(qǐng)求代碼,如果客戶金額少于1000,應(yīng)答的是一個(gè)提示金額不足的代。 提下來(lái)的焦就是怎么在第一次應(yīng)答中,帶有第二次請(qǐng)求的代碼,這里的代碼是請(qǐng)求的代碼,一定是在客戶端,也當(dāng)然是腳本代碼了。 下面看一下代碼實(shí)現(xiàn): 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中用一個(gè)Hide標(biāo)簽,是用來(lái)存放標(biāo)志的,在第二次客戶端提交是應(yīng)用。 PostPage方法是客戶端向后臺(tái)提交請(qǐng)求的代碼。 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) { //實(shí)現(xiàn)對(duì)前臺(tái)標(biāo)志的判斷 string mark = Request.Form["mark"] != null ? Request.Form["mark"] : ""; if (mark == "mark_value") { WriteMark(); } } } /// <summary> /// 更新數(shù)據(jù)庫(kù),寫(xiě)數(shù)據(jù)據(jù)對(duì)帳標(biāo)志 /// </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) { //按照卡號(hào)查詢金額 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) { //下面的代碼是調(diào)用二次請(qǐng)求的代碼,用confirm來(lái)提示用戶做判斷 string s = " <script language='javascript'>if(confirm('您的金額為:" + amount + "元,確定要對(duì)帳嗎?')){ 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); } } } 數(shù)據(jù)庫(kù)結(jié)構(gòu):
| 列名 | 數(shù)據(jù)類型 | 備注 |
| id | Int | 編號(hào)主鍵,自動(dòng)增長(zhǎng) |
| number | Varchar(50) | 卡號(hào) |
| amount | Float | 金額 |
| accmark | int | 對(duì)帳標(biāo)志 |
轉(zhuǎn)載于:https://blog.51cto.com/axzxs/164269
總結(jié)
以上是生活随笔為你收集整理的一个B/S结构自动二次请求需求的实现的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 简短高冷个性女生游戏名字104个
- 下一篇: 测试你的电脑是否支持Hyper-V