I have a bool method which calculates the sum (quantity*price) it works fine.
When I call the method from one button it works fine.
protected void btnupdate_Click(object sender, EventArgs e)
{
calculate_sum();
}
I also need to call it again from different button.
So I try to write this:
if(calculate_sum())
{
something
}
else
{
something
}
The problem is with the second button it doesn’t perform the method it just takes the value which was calculated before when button btnupdate was clicked and doesn’t comes in to perform it again.
The aspx page is wrapped in updatepanels with triggers maybe there’s something to do with that?
Here is the code of the method:
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;
}
}
Here the aspx page:
<%@ 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>
Your question still isn’t very clear…
Is the “second button” you’re referring to named “Button1” ?
If so – notice that it is in a different UpdatePanel then the Grid and the “Update” button.
When the second UpdatePanel is triggered (from Button1), changes that were made in the grid (which is in the other UpdatePanel) are not necessarily passed along with it back to the server.
I would recommend:
Temporarily remove both UpdatePanels and see if it works as you want. If it does, you know the culprit and changing the UpdatePanel’s UpdateMode to Always might solve your problem.
If this does NOT solve your problem, do as @JonSkeet suggested in the comments and create a short demo that shows the problem so we can better help you out…