2017-09-17 8 views
1

[計算]ボタンの前に[確認]ボタンがクリックされた場合にのみ、lblmessageを表示させるにはどうすればよいですか?

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Confirm.aspx.cs" Inherits="XEx04Quotation.Confirm" %> 
 

 
<!DOCTYPE html> 
 

 
<html xmlns="http://www.w3.org/1999/xhtml"> 
 
<head runat="server"> 
 
    <title>Confirm quotation</title> 
 
    <meta name="viewport" content="width=device-width, initial-scale=1" /> 
 
    <script src="Scripts/jquery-1.9.1.min.js"></script> 
 
    <script src="Scripts/bootstrap.min.js"></script> 
 
    <link href="Content/bootstrap.min.css" rel="stylesheet" /> 
 
    <link href="Content/site.css" rel="stylesheet" /> 
 
</head> 
 
<body> 
 
    <form id="form1" runat="server" class="form-horizontal"> 
 

 
     <main class="container"> 
 

 
      <h1 class="jumbotron">Quotation confirmation</h1> 
 

 
      <div class="form-group"> 
 
       <label class="col-sm-3 control-label">Sales price</label> 
 
       <asp:Label ID="lblSalesPrice" runat="server" CssClass="col-sm-3 bold"></asp:Label> 
 
      </div> 
 

 
      <div class="form-group"> 
 
       <label class="col-sm-3 control-label">Discount amount</label> 
 
       <asp:Label ID="lblDiscountAmount" runat="server" CssClass="col-sm-3 bold"></asp:Label> 
 
      </div> 
 

 
      <div class="form-group"> 
 
       <label class="col-sm-3 control-label">Total price</label> 
 
       <asp:Label ID="lblTotalPrice" runat="server" CssClass="col-sm-3 bold"></asp:Label> 
 
      </div> 
 

 
      <div class="row"> 
 
       <h3 class="col-sm-offset-2 col-sm-10">Send confirmation to</h3> 
 
      </div> 
 
      
 
      <div class="form-group"> 
 
       <label class="col-sm-3 control-label">Name</label> 
 
       <div class="col-sm-3"> 
 
        <asp:TextBox ID="txtName" runat="server" CssClass="form-control"></asp:TextBox> 
 
       </div> 
 
       <div class="col-sm-6"> 
 
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtName" 
 
         Display="Dynamic" ErrorMessage="Required" CssClass="text-danger"></asp:RequiredFieldValidator> 
 
       </div> 
 
      </div> 
 
      
 
      <div class="form-group"> 
 
       <label class="col-sm-3 control-label">Email address</label> 
 
       <div class="col-sm-3"> 
 
        <asp:TextBox ID="txtEmail" runat="server" CssClass="form-control"></asp:TextBox> 
 
       </div> 
 
       <div class="col-sm-6"> 
 
        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtEmail" 
 
         Display="Dynamic" ErrorMessage="Required" CssClass="text-danger"></asp:RequiredFieldValidator> 
 
       </div> 
 
      </div> 
 

 
      <%-- Quotation and Return buttons --%> 
 
      <div class="form-group"> 
 
       <div class="col-sm-offset-3 col-sm-9"> 
 
        <%-- buttons go here --%> 
 
        <asp:Button ID="Button1" runat="server" Text="Send Quotation" CssClass="btn btn-primary"/> 
 
        <asp:Button ID="Button2" runat="server" Text="Return" CssClass="btn btn-primary" PostBackUrl="~/Default.aspx" CausesValidation="false"/> 
 
       </div> 
 
      </div> 
 
      
 
      <%-- message label --%> 
 
      <div class="form-group"> 
 
       <div class="col-sm-offset-1 col-sm-11"> 
 
        <%-- message label goes here --%> 
 
        <asp:Label ID="lblmessage" runat="server" Text="Click the Send Quotation button to send the quotation via email" CssClass="text-info"></asp:Label> 
 
       </div> 
 
      </div> 
 

 
     </main> 
 

 
    </form> 
 
</body> 
 
</html>

using System; 
 
using System.Collections.Generic; 
 
using System.Linq; 
 
using System.Web; 
 
using System.Web.UI; 
 
using System.Web.UI.WebControls; 
 

 
namespace XEx04Quotation 
 
{ 
 
    public partial class Default : System.Web.UI.Page 
 
    { 
 
     protected void Page_Load(object sender, EventArgs e) 
 
     { 
 
      UnobtrusiveValidationMode = UnobtrusiveValidationMode.None; 
 
     } 
 

 
     protected void btnCalculate_Click(object sender, EventArgs e) 
 
     { 
 
      if (IsValid) 
 
      { 
 
       decimal salesPrice = Convert.ToDecimal(txtSalesPrice.Text); 
 

 
       // save the salesPrice into a session state variable 
 
       Session["SalesPrice"] = salesPrice; 
 

 

 
       decimal discountPercent = Convert.ToDecimal(txtDiscountPercent.Text)/100; 
 

 
    
 
       decimal discountAmount = salesPrice * discountPercent; 
 

 
       // save the discountAmount into a session state variable 
 
       Session["Discount"] = discountAmount; 
 

 
       
 
       decimal totalPrice = salesPrice - discountAmount; 
 

 
       // save the totalPrice into a session state variable 
 

 
       Session["TotalPrice"] = totalPrice; 
 

 
       lblDiscountAmount.Text = discountAmount.ToString("c"); 
 
       lblTotalPrice.Text = totalPrice.ToString("c"); 
 
      } 
 
     } 
 

 
     protected void Button1_Confirm(object sender, EventArgs e) 
 
     { 
 
      if(Session["SalesPrice"] != null) 
 
      { 
 
       Response.Redirect("Confirm.aspx"); 
 
      } 
 
      else 
 
      { 
 
       // This is the part I am concerned about 
 
       lblmessage2.Text = "Click the Calculate button before you click confirm"; 
 
      } 
 
      
 

 

 
     } 
 
    } 
 
}

みなさん、こんにちは、私はDefault.aspxを、Default.aspx.csとConfirm.aspxを用いたマルチWebアプリケーションのページを構築してい

およびConfirm.aspx.cs。私が抱えている問題は、「計算」をクリックする前に「確認」をクリックすると、ページの下部にある「確認の前に計算ボタンをクリックする」というイベントハンドラが表示されることです。これは、[計算]ボタンが[確認]の前にクリックされていても、ページをリロードしたときに再び表示されても実行されます。 SalesPriceのセッション状態値がnullの場合、これを表示するにはどうすればよいですか?それ以外の場合は、確認ページにリダイレクトされます。ここにDefault.aspx.csと他のファイルがあります:

+0

のみの代わりに、アプリケーション全体の*動作しない*あなたのコードを定義します -

アップデート1以下を試してみてください、コードをテストしました。 – AsifAli72090

+0

@ Asif.Ali Confirm.aspxファイルとConfirm.aspx.csファイルを含めることができますか? – dorakta

+0

@ Asif.Aliアップデートが表示されますか? – dorakta

答えて

2

あなたは1つのことをすることができます。あなたが最初の合計の計算を取得したい場合は、[確認]ボタンを無効にして、次のように計算]ボタンをクリックすると、ボタンが有効になります。

<asp:Button ID="Button1" runat="server" Enabled="false" /> 

protected void btnCalculate_Click(object sender, EventArgs e) 
{ 
    Button1.Enabled = True; 

    if(Session["TotalPrice"] != null) 
    { 
     lblMsg.Text = "Please click here to see total calculation"; 
    } 
} 

protected void Button1_Confirm(object sender, EventArgs e) 
{ 
    if(Session["SalesPrice"] != null) 
    { 
     Response.Redirect("Confirm.aspx"); 
    } 
} 

:イベント名の命名規則に従ってください。 Button1 To btnConfirm

<asp:Button ID="btnCalculate" runat="server" Text="Calculate" OnClick="btnCalculate_Click" /> 

<asp:Button ID="btnConfirm" runat="server" Text="Confirm" OnClick="btnConfirm_Click" /> 

<asp:Label ID="lblMsg" runat="server"></asp:Label> 


private int buttonWasClicked = 0; //Kept a variable type of int and with a default value 0 

protected void btnCalculate_Click(object sender, EventArgs e) 
{ 
    buttonWasClicked = 1; //If clicked, then 1 
    Session["value"] = Convert.ToInt32(buttonWasClicked); //Then kept in session 

    lblMsg.Text = "Total Price - 10,000"; 
} 

protected void btnConfirm_Click(object sender, EventArgs e) 
{ 
    if (Session["value"] != null) //If session not null, then redirect page 
    { 
     Response.Redirect("CustomerDetails.aspx"); 
     Session["value"] = null; //Clears the session 
    } 
    else 
    { 
     lblMsg.Text = "Click the calculate button first"; 
    } 
} 
+0

- btnCalculate_Click()イベントハンドラ内で "確認の前に計算ボタンをクリックする"というメッセージをどのように書きますか? – dorakta

+0

どのようなメッセージを入れたいですか? –

+0

"確認の前に計算ボタンをクリックしてください。" – dorakta

関連する問題