Solved

Why isn't this web form textbox updating after a postback?

Posted on 2012-03-12
8
362 Views
Last Modified: 2012-03-15
The quantity variable receives the original value and not the user changed value after the update button is clicked. Why is it that I am doing wrong? I tried changing Eval to Bind in the TextBox but that didn't seem to help.
ViewCart.aspx
...
<asp:TextBox runat="server" ID="txtQuantity" Columns="5" Text='<%# Eval("Quantity") %>'></asp:TextBox><br /> 
...
<asp:Button runat="server" ID="btnUpdateCart" Text="Update Cart" OnClick="btnUpdateCart_Click" />  
...

ViewCart.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{    
    BindData();
}
...
protected void btnUpdateCart_Click(object sender, EventArgs e) {
    foreach (GridViewRow row in gvShoppingCart.Rows) 
    {
        if (row.RowType == DataControlRowType.DataRow) 
        {
            // We’ll use a try catch block in case something other than a number is typed in
            // If so, we’ll just ignore it.
            try 
            {
                // Get the productId from the GridView’s datakeys
                int productId = Convert.ToInt32(gvShoppingCart.DataKeys[row.RowIndex].Value);
                // Find the quantity TextBox and retrieve the value
                int quantity = int.Parse(((TextBox)row.Cells[1].FindControl("txtQuantity")).Text);
                ShoppingCart cart = ShoppingCart.Instance;
                cart.SetItemQuantity(productId, quantity);
            } catch (FormatException) { }
        }
    }
    BindData();
}

Open in new window

0
Comment
Question by:Eindoofus
  • 4
  • 3
8 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37713158
Can you show the definition of BindData?

Side note:  it would be cheaper to use something like int.TryParse for parsing user input rather than use int.Parse and catch an exception--especially one you simply swallow.
0
 

Author Comment

by:Eindoofus
ID: 37713161
protected void BindData()
{
    // Let’s give the data to the GridView and let it work!
    // The GridView will take our cart items one by one and use the properties
    // that we declared as column names (DataFields)
    ShoppingCart cart = ShoppingCart.Instance;
    gvShoppingCart.DataSource = cart.Items;
    gvShoppingCart.DataBind();
}

Open in new window


http://net.tutsplus.com/tutorials/other/build-a-shopping-cart-in-aspnet/
Found ViewCart.aspx.cs in the comments section
0
 
LVL 7

Expert Comment

by:CloudedTurtle
ID: 37714476
Could you include your entire GridView code.

Also can you provide the GridRowBound event code as well?
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:Eindoofus
ID: 37715760
<asp:GridView runat="server" ID="gvShoppingCart" AutoGenerateColumns="false" EmptyDataText="There is nothing in your shopping cart." GridLines="None" Width="100%" CellPadding="5" ShowFooter="true" DataKeyNames="ProductId" OnRowDataBound="gvShoppingCart_RowDataBound" OnRowCommand="gvShoppingCart_RowCommand">  
	<HeaderStyle HorizontalAlign="Left" BackColor="#3D7169" ForeColor="#FFFFFF" />  
	<FooterStyle HorizontalAlign="Right" BackColor="#6C6B66" ForeColor="#FFFFFF" />  
	<AlternatingRowStyle BackColor="#F8F8F8" />  
	<Columns>  

		<asp:BoundField DataField="Description" HeaderText="Description" />  
		<asp:TemplateField HeaderText="Quantity">  
			<ItemTemplate>  
				<asp:TextBox runat="server" ID="txtQuantity" Columns="5" Text='<%# Eval("Quantity") %>'></asp:TextBox><br />  
				<asp:LinkButton runat="server" ID="btnRemove" Text="Remove" CommandName="Remove" CommandArgument='<%# Eval("ProductId") %>' style="font-size:12px;"></asp:LinkButton>  

			</ItemTemplate>  
		</asp:TemplateField>  
		<asp:BoundField DataField="UnitPrice" HeaderText="Price" ItemStyle-HorizontalAlign="Right" HeaderStyle-HorizontalAlign="Right" DataFormatString="{0:C}" />  
		<asp:BoundField DataField="TotalPrice" HeaderText="Total" ItemStyle-HorizontalAlign="Right" HeaderStyle-HorizontalAlign="Right" DataFormatString="{0:C}" />  
	</Columns>  
</asp:GridView>  

Open in new window

protected void gvShoppingCart_RowDataBound(object sender, GridViewRowEventArgs e) {
	// If we are binding the footer row, let’s add in our total
	if (e.Row.RowType == DataControlRowType.Footer) {
		ShoppingCart cart = ShoppingCart.Instance;
		e.Row.Cells[3].Text = "Total: " + cart.GetSubTotal().ToString("C");
	}

}

Open in new window

0
 
LVL 7

Expert Comment

by:CloudedTurtle
ID: 37716630
put this as your "Page_Load" event code... see if it fixes it.

protected void Page_Load(object sender, EventArgs e)
    {		
		if (!IsPostBack)
			BindData();
    }

Open in new window

0
 

Author Comment

by:Eindoofus
ID: 37716653
Thanks for your response but I already have that in the Page_Load.
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        BindData();
    }

Open in new window

0
 
LVL 7

Accepted Solution

by:
CloudedTurtle earned 500 total points
ID: 37716944
No worries, I didn't see it listed in your initial post.

There isn't any reason that I can see as to why it isn't functioning. I downloaded the sample source from the site that you linked to, and updated to build properly. It works fine for me, in my environment.

Wondering if maybe there is something that is preventing the cart.Instance from applying the updated criteria. If you put a break-point on the btn_update function, can you step through it and determine if the quantity is being updated properly?
0
 

Author Closing Comment

by:Eindoofus
ID: 37726326
I have to move onto another project and don't have time to pursue this further but thanks for testing it out.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question