2011-11-05 288 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