Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2012-03-12
8
Medium Priority
?
369 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 1000 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…

824 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