2011-11-05 329 views
2
Private Function StoreCouponCode() As String 
     Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("DBConnectionString").ConnectionString) 
     Dim sql As New SqlCommand("", con) 
     Dim sqlGetSlNo As New SqlCommand("SELECT MAX(SlNo) FROM Coupons WHERE CustID=" & Convert.ToInt16(HFCustID.Value) & " AND BusiID=" & Convert.ToInt16(HFBusiID.Value) & " AND OfferID=" & Convert.ToInt16(HFOfferID.Value), con) 
     Dim sNo, UserID, couponCode, offerCheck As String 
     Dim slNo As Int16 

     Try 
      UserID = getCurrentUserID() 
      con.Open() 
      sNo = Convert.ToString(sqlGetSlNo.ExecuteScalar)  'fteches latest serial no for the coupon 
      If sNo = "" Then 
       slNo = 0 
      Else 
       slNo = Convert.ToInt16(sNo) + 1 
      End If 
      couponCode = MakeCouponCode(slNo) 
      offerCheck = couponCode.Substring(13, 3) 
      sql.CommandText = "INSERT INTO Coupons VALUES (" & _ 
            HFCustID.Value & "," & HFBusiID.Value & "," & HFOfferID.Value & ",'" & _ 
            Today.Date & "'," & Convert.ToString(slNo) & "," & offerCheck & ",'" & couponCode & "','" & _ 
            UserID & "'," & LblPayInAdv.Text & "," & LblPayLater.Text & ",'" & Convert.ToString(Date.Now) & "')" 
      sql.ExecuteNonQuery() 
      con.Close() 
     Catch ex As Exception 
      couponCode = "N/A" 
     Finally 
      con.Dispose() 
      sql.Dispose() 
     End Try 
     Return couponCode 
    End Function 





Protected Sub CmdGetCoupon_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles CmdGetCoupon.Click 
     If User.Identity.Name = "" Then 
      LblMessage.Text = "You need to login first!" 
      Exit Sub 
     End If 
     Dim couponCode As String = StoreCouponCode() 
     Response.Redirect("~/Coupon.aspx?CouponCode=" & couponCode, True) 
    End Sub 

當在CmdGetCoupon用戶點擊它需要一些時間來redirect..so在CmdGetCoupon不止一次,這導致了多個優惠券產生從單個用戶帳戶的用戶點擊。禁用按鈕後點擊

我想顯示一條消息「請等待,而您的優惠券正在生成」,並禁用CmdGetCoupon,以便用戶不能多次點擊....任何想法??怎麼做?

+1

爲什麼你標記的C#? – Maysam

回答

5

在javascript中執行此操作 - 使用OnClientClick調用禁用傳入的表單元素的函數。

// On button 
... OnClientClick="DisableMe(this);" ... 

// Javascript 
function DisableMe(elem) 
{ 
    elem.disabled = true; 
    // more code to show message 
} 
+0

elem.disabled = true? – Damith

+0

@UnhandledException - 感謝您的更正。 – Oded

+1

我試過了你的代碼,但頁面沒有重定向... :( –

1

您可以禁用點擊處理程序中的按鈕,因爲您要重定向到另一個頁面,所以不要關心任何其他邏輯。 否則,如果您正在執行任何異步操作,請再次單擊它並在異步回調處理程序中將其禁用,具體取決於邏輯要麼將其啓用或以其他方式正確處理。

2

在HTML端,你必須將它添加在OnClientClick屬性:

<asp:Button ID="CmdGetCoupon" runat="server" onClientClick="this.disabled=true;" /> 

編輯:
由於@Oded如此準確地指出,我的第一個答案沒有解決的消息顯示,這裏有幾個選項。

<span id="PleaseWaitContainer"></span> 
<asp:Button ID="CmdGetCoupon" runat="server" onClientClick="this.disabled=true;document.getElementById('PleaseWaitContainer').innerText='Please Wait While Your Coupon Is Being Generated';" /> 

或者

<asp:Button ID="CmdGetCoupon" runat="server" onClientClick="this.disabled=true;alert('Please wait while your coupon is being generated...';" /> 
+0

簡明扼要,是這將不會顯示消息用戶,作爲OP的請求 – Oded

+0

@Oded:我看到你通過添加了'//更多的代碼來顯示消息'而增加了額外的一英里我假定你的回答也不符合OP的請求 –

+0

夠公平的......: ) – Oded