bool
うまくいく合計(数量*価格)を計算する方法があります。1つのボタンからメソッドを呼び出すと、正常に機能します。
protected void btnupdate_Click(object sender, EventArgs e)
{
calculate_sum();
}
また、別のボタンからもう一度呼び出す必要があります。だから私はこれを書こうとします:
if(calculate_sum())
{
something
}
else
{
something
}
問題は、2番目のボタンで、ボタンbtnupdate
がクリックされたときに以前に計算された値を取得するだけのメソッドを実行せず、再度実行しないことです。
aspx
ページはトリガーでラップされています多分それupdatepanels
と関係がありますか?
メソッドのコードは次のとおりです。
public bool calculate_sum()
{
bool is_ok = true; ;
double totalsum = 0;
int qty = 0; ;
string p = "";
for (int i = 0; i < GridView2.Rows.Count; i++)
{
if (GridView2.Rows[i].RowType == DataControlRowType.DataRow)
{
TextBox txtqty = GridView2.Rows[i].FindControl("lblquantity") as TextBox;
p = GridView2.Rows[i].Cells[0].Text.ToString();
if (Int32.TryParse(txtqty.Text, out qty))
{
if (qty > 0)
{
double pr = Convert.ToDouble(p.ToString());
//total per row
double tcost = qty * pr;
totalsum = totalsum + tcost;
GridView2.Rows[i].Cells[6].Text = Convert.ToString((tcost));
}
else
{
lblGrandTotal.Visible = true;
lblGrandTotal.Text = "enter only numbers greater than 0";
lblGrandTotal.ForeColor = System.Drawing.Color.Red;
is_ok = false;
}
}
else
{
lblGrandTotal.Visible = true;
lblGrandTotal.Text = "enter only numbers greater than 0";
lblGrandTotal.ForeColor = System.Drawing.Color.Red;
is_ok = false;
}
}
}
if (is_ok)
{
lblGrandTotal.ForeColor = System.Drawing.Color.Black;
lblGrandTotal.Text = Convert.ToString((totalsum));
return is_ok;
}
else
{
return is_ok;
}
}
ここにaspxページがあります:
<%@ Page Title="Check out" Language="C#" MasterPageFile="~/MasterPage/MasterPage.master" AutoEventWireup="true" CodeFile="CheckOut.aspx.cs" Inherits="CheckOut" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server">
</asp:ScriptManagerProxy>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="False"
UpdateMode="Conditional">
<ContentTemplate>
<asp:HiddenField ID="hfGrandTotal" runat="server" />
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" >
<Columns>
<asp:BoundField DataField="Price" HeaderText="Price" ItemStyle-CssClass="price" >
<ItemStyle CssClass="price"></ItemStyle>
</asp:BoundField>
<asp:TemplateField HeaderText="ProductID" Visible="False">
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%# Eval("ProductID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ProductName">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("ProductName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Summary">
<ItemTemplate>
<asp:Label ID="lblSum" runat="server" Text='<%# Eval("Summary") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="picPath" Visible="False">
<ItemTemplate>
<asp:Label ID="lblPic" runat="server" Text='<%# Eval("picPath") %>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText = "quantity">
<ItemTemplate>
<asp:TextBox ID="lblquantity" runat="server" >1</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText = "Total">
<ItemTemplate>
<asp:Label ID="lblTotal" runat="server" ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
total sum:
<asp:Button ID="btnupdate" runat="server" Text="update quantity"
onclick="btnupdate_Click" ValidationGroup="panel1" />
<asp:Label ID="lblGrandTotal" runat="server" ></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnupdate" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
<br />
<br />
<br />
<br />
<br />
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Table ID="Table1" runat="server" CellPadding="7" CellSpacing="7"
CaptionAlign="Bottom" HorizontalAlign="Center" BorderStyle="Solid" BorderWidth="1"
BorderColor="Black" Height="305px" Width="408px">
<asp:TableHeaderRow BorderStyle="Solid" BorderWidth="1" BorderColor="Black">
<asp:TableCell ColumnSpan="3" BorderStyle="Solid" BorderWidth="1" BorderColor="Black">customer details</asp:TableCell>
</asp:TableHeaderRow>
<asp:TableRow runat="server" >
<asp:TableCell runat="server" >
<asp:TextBox ID="TextBox1" runat="server" Width="150px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ForeColor="Red" ErrorMessage="חובה" ControlToValidate="TextBox1" Text="requierd" ValidationGroup="panel2"></asp:RequiredFieldValidator>
</asp:TableCell>
<asp:TableCell runat="server">
<asp:Label ID="Label1" runat="server" Text="name"></asp:Label>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow ID="TableRow1" runat="server">
<asp:TableCell ID="TableCell1" runat="server">
<asp:TextBox ID="TextBox4" runat="server" Width="150px" CausesValidation="True"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ForeColor="Red" ErrorMessage="חובה" ControlToValidate="TextBox4" Text="requierd" ValidationGroup="panel2"></asp:RequiredFieldValidator>
<br /> <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="הקלד מספר טלפון" ControlToValidate="TextBox4" ForeColor="Red" ValidationExpression="^(02|03|04|08|09|050|052|054|057|074|077)\-\d{7,8}$"></asp:RegularExpressionValidator>
</asp:TableCell>
<asp:TableCell ID="TableCell2" runat="server">
<asp:Label ID="Label4" runat="server" Text="phone"></asp:Label>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow runat="server">
<asp:TableCell runat="server">
<asp:TextBox ID="TextBox2" runat="server" Width="150px" CausesValidation="True"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ForeColor="Red" ErrorMessage="חובה" ControlToValidate="TextBox2" Text="requierd" ValidationGroup="panel2"></asp:RequiredFieldValidator>
</asp:TableCell>
<asp:TableCell>
<asp:Label ID="Label2" runat="server" Text="address"></asp:Label>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow runat="server">
<asp:TableCell runat="server">
<asp:TextBox ID="TextBox3" runat="server" Width="150px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ForeColor="Red" ErrorMessage="חובה" ControlToValidate="TextBox3" Text="requierd" ValidationGroup="panel2"></asp:RequiredFieldValidator>
<br />
<asp:RangeValidator ID="RangeValidator1" runat="server" ErrorMessage="only digits" ControlToValidate="TextBox3" ForeColor="Red" MinimumValue="1000" MaximumValue="9999999999999999" ValidationGroup="panel2"></asp:RangeValidator>
</asp:TableCell>
<asp:TableCell runat="server">
<asp:Label ID="Label3" runat="server" Text="number"></asp:Label>
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell ColumnSpan="3">
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="pay" Width="150" ValidationGroup="panel2" />
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell ColumnSpan="3">
<asp:Label ID="lblmessage" runat="server" Text="Label"
Font-Size="XX-Large" Visible="False"></asp:Label>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
<asp:Button ID="btnPrint" runat="server" Text="print"
onclick="btnPrint_Click" Visible="False" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnPrint" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
</asp:Content>