I have an ecomerce site I am trying to update a cell value for Cases when the value for totalpounds is changed. and then update in the data base and return the new gridview. here is my code - I have been working on the onupdateing to try and get this to
work but as of yet have not been successful. I'm using vb.
thanks
richard
<%@ Page Language="VB" MasterPageFile="~/Store.master" Title="Shopping Cart" %>
<script runat="server">
Private _total As Decimal = 0
Private _totalLbs As Decimal = 0
Private _totalquantity As Integer
Protected Sub rowUpdateing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs)
Dim records(e.NewValues.Count - 1) As DictionaryEntry
Dim entry As DictionaryEntry
Dim totalLbs As String
Dim lblTotalQuantity As Integer
Dim lbs As Integer
e.NewValues.CopyTo(records, 0)
For Each entry In records
grdShoppingCart.Rows(grdShoppingCart.EditIndex).Cells(2).FindControl("lblLbs").ToString()
lbs = CType(grdShoppingCart.Rows(grdShoppingCart.EditIndex).Cells(2).FindControl("lblLbs"), Label).Text()
totalLbs = entry.Key.ToString
lblTotalQuantity = totalLbs / lbs
lblTotalQuantity = CType(grdShoppingCart.Rows(grdShoppingCart.EditIndex).Cells(3).FindControl("lblTotalQuantity"), Label).Text()
DataBind()
Next
End Sub
''' <summary>
''' Calculate running total as rows are bound to the GridView
''' </summary>
Protected Sub grdShoppingCart_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim price As Decimal = CType(DataBinder.Eval(e.Row.DataItem, "Price"), Decimal)
Dim lbs As Integer = CType(DataBinder.Eval(e.Row.DataItem, "Lbs"), Integer)
Dim totallbs As Integer = CType(DataBinder.Eval(e.Row.DataItem, "TotalLbs"), Integer)
_total += (price * totallbs)
_totalLbs += (totallbs)
End If
End Sub
''' <summary>
''' Show price total and check out link
''' </summary>
Protected Sub grdShoppingCart_DataBound(ByVal sender As Object, ByVal e As EventArgs)
If _total = 0 And _totalLbs = 0 Then
lnkCheckOut.Visible = False
Else
Dim lblTotal As Label = CType(grdShoppingCart.FooterRow.FindControl("lblTotal"), Label)
Dim lblTotalLbs As Label = CType(grdShoppingCart.FooterRow.FindControl("lblTotalLbs"), Label)
lblTotal.Text = _total.ToString("c")
lblTotalLbs.Text = _totalLbs.ToString() & " lbs"
End If
End Sub
</script>
<asp:Content ID="Content1" ContentPlaceHolderID="contentMiddle" Runat="Server">
<asp:GridView
id="grdShoppingCart"
DataSourceID="srcCart"
DataKeyNames="id"
AutoGenerateColumns="False"
AutoGenerateDeleteButton="True"
AutoGenerateEditButton="True"
EmptyDataText="There are no items in your shopping cart. Login to view your saved shopping cart."
CssClass="cartBig"
AlternatingRowStyle-CssClass="cartAlternating"
GridLines="None"
ShowFooter="True"
Runat="server" OnDataBound="grdShoppingCart_DataBound" OnRowUpdating="rowUpdateing"
OnRowDataBound="grdShoppingCart_RowDataBound">
<AlternatingRowStyle CssClass="cartAlternating"></AlternatingRowStyle>
<Columns>
<asp:TemplateField HeaderText="Product Name">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("Name") %>' Width="200"></asp:Label>
</ItemTemplate>
<HeaderStyle Width="100px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="lbs">
<ItemTemplate>
<asp:Label ID="lblLbs" runat="server" Text='<%# Bind("Lbs") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblLbs1" runat="server" Text='<%# Eval("Lbs") %>'></asp:Label>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Cases">
<ItemTemplate>
<asp:Label ID="lbl3" runat="server" Text='<%# Bind("TotalQuantity") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblTotalQuantity" runat="server" Text='<%# Eval("TotalQuantity") %>'></asp:Label>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total Pounds">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("TotalLbs") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox
ID="TextBox1"
runat="server"
Text='<%# Bind("TotalLbs") %>'
Width="50"
ForeColor="Red"
BackColor="Yellow"
BorderStyle="Inset">
</asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:Label ID="lblTotalLbs" Runat="server" BackColor="#CCFFCC" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Price per pound">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("TotalLbs") %>' Width="80"></asp:Label>
</ItemTemplate>
<ItemTemplate>
<%# Eval("Price", "{0:c3}") %>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblTotal" Runat="server" BackColor="#CCFFCC" />
</FooterTemplate>
<FooterStyle CssClass="cartTotalFooter" />
<FooterStyle CssClass="cartTotalFooter" />
</asp:TemplateField>
</Columns>
</asp:GridView>
<br /><br />
<asp:Hyperlink
id="lnkCheckOut"
Text="Check Out"
NavigateUrl="~/CheckOut/Default.aspx"
Runat="server" />
<asp:ObjectDataSource
id="srcCart"
TypeName="AspNetUnleashed.ShoppingCart"
SelectMethod="Select"
UpdateMethod="Update"
DeleteMethod="Delete"
Runat="server" />
</asp:Content>
<asp:Content ContentPlaceHolderID="contentRight" runat="server" />
You can save the original total value in Session in the RowDataBound or DataBound event. It depends on which total value you means. Then in the start of RowUpdating event you can check the current total value with the original one from the Session to decide
whether do updating or not.
I am not sure which total value you want to check whether it is changed or not. Just save the original one into Session. When you need to compare, then get it from Session and compare with the new one. Just put the related code into the related event. nothing
special. Like:
'for saving Session("total") = original_total
'for comparing Dim o_total As Integer = Convert.ToInt32(Session("total"))
If o_total <> new_total Then 'doing something End If
What I was trying to do is, when my customer clicks on the edit in the gridview row, he can change only the total number of pounds, also in that row is the number of cases, what I was wanting is the number of cases to change when the customer hits the update
button and the new number of cases be saved to the database. the gridview comes back up for the customer to change another row if he likes. I really don't need to compare anything I don't think. just need to know how to change and save the cases when the customer
updates the total pounds orders (for that row).
You can place a HiddenField control beside the TextBox of total number of pounds, or number of case. To bind the same fields of TextBox to the Value property of HiddenField. Then in the update event by clicking update button you can access the TextBox text
and also the value of HiddenField to compare whether it has been changed or not, then do updating. The HiddenField is invisible, so it won't affect you anything, but you can access it.
Thanks, I believe that might work, can you show me how to access the text box in the gridview (since there are several lines) i keep getting error that it is not set to a control.
Here is what I tried, but I have an error the operands "<>" and "/" are not defined for controls textbox and label:
Protected Sub rowUpdateing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs)
For Each gvr As GridViewRow In grdShoppingCart.Rows
Dim totalpounds As TextBox = DirectCast(gvr.FindControl("TextBox1"), TextBox)
Dim changedpounds As TextBox = DirectCast(gvr.FindControl("LastAmount"), TextBox)
Dim lbs As Label = DirectCast(gvr.FindControl("lblLbs1"), Label)
Dim cases As TextBox = DirectCast(gvr.FindControl("lblTotalQuantity"), TextBox)
If totalpounds <> changedpounds Then cases = totalpounds / lbs
End if
DataBind()
Next
Thanks
End Sub
You cannot use TextBox control to do this. You need their texts and convert them into integer or double type for calculation.
Something like this:
Dim totalpounds As TextBox = DirectCast(gvr.FindControl("TextBox1"), TextBox)
Dim total_p As Integer = Convert.ToInt32(totalpounds.Text)
Dim changedpounds As TextBox = DirectCast(gvr.FindControl("LastAmount"), TextBox)
Dim total_c As Integer = Convert.ToInt32(changedpounds.Text)
Dim lbs As Integer = Convert.ToInt32(DirectCast(gvr.FindControl("lblLbs1"), Label).Text) '... other control
If total_p <> total_c Then
cases = total_p \ lbs
End If
If you use different data type for total and other values, just change to Convert.othertype() method to cast.
richard1970
Member
5 Points
22 Posts
Gridview: editing one cell and updating based on calculation
Dec 23, 2010 09:10 PM|LINK
I have an ecomerce site I am trying to update a cell value for Cases when the value for totalpounds is changed. and then update in the data base and return the new gridview. here is my code - I have been working on the onupdateing to try and get this to work but as of yet have not been successful. I'm using vb.
thanks
richard
<%@ Page Language="VB" MasterPageFile="~/Store.master" Title="Shopping Cart" %> <script runat="server"> Private _total As Decimal = 0 Private _totalLbs As Decimal = 0 Private _totalquantity As Integer Protected Sub rowUpdateing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Dim records(e.NewValues.Count - 1) As DictionaryEntry Dim entry As DictionaryEntry Dim totalLbs As String Dim lblTotalQuantity As Integer Dim lbs As Integer e.NewValues.CopyTo(records, 0) For Each entry In records grdShoppingCart.Rows(grdShoppingCart.EditIndex).Cells(2).FindControl("lblLbs").ToString() lbs = CType(grdShoppingCart.Rows(grdShoppingCart.EditIndex).Cells(2).FindControl("lblLbs"), Label).Text() totalLbs = entry.Key.ToString lblTotalQuantity = totalLbs / lbs lblTotalQuantity = CType(grdShoppingCart.Rows(grdShoppingCart.EditIndex).Cells(3).FindControl("lblTotalQuantity"), Label).Text() DataBind() Next End Sub ''' <summary> ''' Calculate running total as rows are bound to the GridView ''' </summary> Protected Sub grdShoppingCart_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) If e.Row.RowType = DataControlRowType.DataRow Then Dim price As Decimal = CType(DataBinder.Eval(e.Row.DataItem, "Price"), Decimal) Dim lbs As Integer = CType(DataBinder.Eval(e.Row.DataItem, "Lbs"), Integer) Dim totallbs As Integer = CType(DataBinder.Eval(e.Row.DataItem, "TotalLbs"), Integer) _total += (price * totallbs) _totalLbs += (totallbs) End If End Sub ''' <summary> ''' Show price total and check out link ''' </summary> Protected Sub grdShoppingCart_DataBound(ByVal sender As Object, ByVal e As EventArgs) If _total = 0 And _totalLbs = 0 Then lnkCheckOut.Visible = False Else Dim lblTotal As Label = CType(grdShoppingCart.FooterRow.FindControl("lblTotal"), Label) Dim lblTotalLbs As Label = CType(grdShoppingCart.FooterRow.FindControl("lblTotalLbs"), Label) lblTotal.Text = _total.ToString("c") lblTotalLbs.Text = _totalLbs.ToString() & " lbs" End If End Sub </script> <asp:Content ID="Content1" ContentPlaceHolderID="contentMiddle" Runat="Server"> <asp:GridView id="grdShoppingCart" DataSourceID="srcCart" DataKeyNames="id" AutoGenerateColumns="False" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" EmptyDataText="There are no items in your shopping cart. Login to view your saved shopping cart." CssClass="cartBig" AlternatingRowStyle-CssClass="cartAlternating" GridLines="None" ShowFooter="True" Runat="server" OnDataBound="grdShoppingCart_DataBound" OnRowUpdating="rowUpdateing" OnRowDataBound="grdShoppingCart_RowDataBound"> <AlternatingRowStyle CssClass="cartAlternating"></AlternatingRowStyle> <Columns> <asp:TemplateField HeaderText="Product Name"> <ItemTemplate> <asp:Label ID="Label2" runat="server" Text='<%# Eval("Name") %>' Width="200"></asp:Label> </ItemTemplate> <HeaderStyle Width="100px" /> </asp:TemplateField> <asp:TemplateField HeaderText="lbs"> <ItemTemplate> <asp:Label ID="lblLbs" runat="server" Text='<%# Bind("Lbs") %>'></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:Label ID="lblLbs1" runat="server" Text='<%# Eval("Lbs") %>'></asp:Label> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Cases"> <ItemTemplate> <asp:Label ID="lbl3" runat="server" Text='<%# Bind("TotalQuantity") %>'></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:Label ID="lblTotalQuantity" runat="server" Text='<%# Eval("TotalQuantity") %>'></asp:Label> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Total Pounds"> <ItemTemplate> <asp:Label ID="Label3" runat="server" Text='<%# Bind("TotalLbs") %>'></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("TotalLbs") %>' Width="50" ForeColor="Red" BackColor="Yellow" BorderStyle="Inset"> </asp:TextBox> </EditItemTemplate> <FooterTemplate> <asp:Label ID="lblTotalLbs" Runat="server" BackColor="#CCFFCC" /> </FooterTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Price per pound"> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Bind("TotalLbs") %>' Width="80"></asp:Label> </ItemTemplate> <ItemTemplate> <%# Eval("Price", "{0:c3}") %> </ItemTemplate> <FooterTemplate> <asp:Label ID="lblTotal" Runat="server" BackColor="#CCFFCC" /> </FooterTemplate> <FooterStyle CssClass="cartTotalFooter" /> <FooterStyle CssClass="cartTotalFooter" /> </asp:TemplateField> </Columns> </asp:GridView> <br /><br /> <asp:Hyperlink id="lnkCheckOut" Text="Check Out" NavigateUrl="~/CheckOut/Default.aspx" Runat="server" /> <asp:ObjectDataSource id="srcCart" TypeName="AspNetUnleashed.ShoppingCart" SelectMethod="Select" UpdateMethod="Update" DeleteMethod="Delete" Runat="server" /> </asp:Content> <asp:Content ContentPlaceHolderID="contentRight" runat="server" />Qin Dian Tan...
All-Star
113532 Points
12480 Posts
Microsoft
Re: Gridview: editing one cell and updating based on calculation
Dec 27, 2010 07:37 AM|LINK
Hi richard1970,
You can save the original total value in Session in the RowDataBound or DataBound event. It depends on which total value you means. Then in the start of RowUpdating event you can check the current total value with the original one from the Session to decide whether do updating or not.
Thanks,
If you have any feedback about my replies, please contactmsdnmg@microsoft.com.
Microsoft One Code Framework
richard1970
Member
5 Points
22 Posts
Re: Gridview: editing one cell and updating based on calculation
Dec 27, 2010 09:42 PM|LINK
ok, how do I do that in the code?
Thanks
Qin Dian Tan...
All-Star
113532 Points
12480 Posts
Microsoft
Re: Gridview: editing one cell and updating based on calculation
Dec 28, 2010 01:29 AM|LINK
Hi richard1970,
I am not sure which total value you want to check whether it is changed or not. Just save the original one into Session. When you need to compare, then get it from Session and compare with the new one. Just put the related code into the related event. nothing special. Like:
'for saving
Session("total") = original_total
'for comparing
Dim o_total As Integer = Convert.ToInt32(Session("total"))
If o_total <> new_total Then
'doing something
End If
Thanks,
If you have any feedback about my replies, please contactmsdnmg@microsoft.com.
Microsoft One Code Framework
richard1970
Member
5 Points
22 Posts
Re: Gridview: editing one cell and updating based on calculation
Dec 28, 2010 01:54 AM|LINK
Hi Qin Dian Tang
What I was trying to do is, when my customer clicks on the edit in the gridview row, he can change only the total number of pounds, also in that row is the number of cases, what I was wanting is the number of cases to change when the customer hits the update button and the new number of cases be saved to the database. the gridview comes back up for the customer to change another row if he likes. I really don't need to compare anything I don't think. just need to know how to change and save the cases when the customer updates the total pounds orders (for that row).
Thanks
Qin Dian Tan...
All-Star
113532 Points
12480 Posts
Microsoft
Re: Gridview: editing one cell and updating based on calculation
Dec 28, 2010 05:01 AM|LINK
Hi richard1970,
You can place a HiddenField control beside the TextBox of total number of pounds, or number of case. To bind the same fields of TextBox to the Value property of HiddenField. Then in the update event by clicking update button you can access the TextBox text and also the value of HiddenField to compare whether it has been changed or not, then do updating. The HiddenField is invisible, so it won't affect you anything, but you can access it.
Thanks,
If you have any feedback about my replies, please contactmsdnmg@microsoft.com.
Microsoft One Code Framework
richard1970
Member
5 Points
22 Posts
Re: Gridview: editing one cell and updating based on calculation
Dec 28, 2010 02:06 PM|LINK
Thanks, I believe that might work, can you show me how to access the text box in the gridview (since there are several lines) i keep getting error that it is not set to a control.
Qin Dian Tan...
All-Star
113532 Points
12480 Posts
Microsoft
Re: Gridview: editing one cell and updating based on calculation
Dec 29, 2010 12:39 AM|LINK
Hi richard1970,
If you want to access the TextBoxes in multiple rows, you can loop the GridView and access it from each row:
For Each gvr As GridViewRow In GridView1.Rows
Dim tb As TextBox = DirectCast(gvr.FindControl("TextBox1"), TextBox)
Next
You can use the same way to access any control from ItemTemplate in GridView.
Thanks,
If you have any feedback about my replies, please contactmsdnmg@microsoft.com.
Microsoft One Code Framework
richard1970
Member
5 Points
22 Posts
Re: Gridview: editing one cell and updating based on calculation
Dec 29, 2010 12:57 PM|LINK
Here is what I tried, but I have an error the operands "<>" and "/" are not defined for controls textbox and label:
Protected Sub rowUpdateing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) For Each gvr As GridViewRow In grdShoppingCart.Rows Dim totalpounds As TextBox = DirectCast(gvr.FindControl("TextBox1"), TextBox) Dim changedpounds As TextBox = DirectCast(gvr.FindControl("LastAmount"), TextBox) Dim lbs As Label = DirectCast(gvr.FindControl("lblLbs1"), Label) Dim cases As TextBox = DirectCast(gvr.FindControl("lblTotalQuantity"), TextBox) If totalpounds <> changedpounds Then cases = totalpounds / lbs End if DataBind() Next Thanks End SubQin Dian Tan...
All-Star
113532 Points
12480 Posts
Microsoft
Re: Gridview: editing one cell and updating based on calculation
Dec 30, 2010 12:25 AM|LINK
Hi richard1970,
You cannot use TextBox control to do this. You need their texts and convert them into integer or double type for calculation.
Something like this:
Dim totalpounds As TextBox = DirectCast(gvr.FindControl("TextBox1"), TextBox)
Dim total_p As Integer = Convert.ToInt32(totalpounds.Text)
Dim changedpounds As TextBox = DirectCast(gvr.FindControl("LastAmount"), TextBox)
Dim total_c As Integer = Convert.ToInt32(changedpounds.Text)
Dim lbs As Integer = Convert.ToInt32(DirectCast(gvr.FindControl("lblLbs1"), Label).Text)
'... other control
If total_p <> total_c Then
cases = total_p \ lbs
End If
If you use different data type for total and other values, just change to Convert.othertype() method to cast.
Thanks,
If you have any feedback about my replies, please contactmsdnmg@microsoft.com.
Microsoft One Code Framework