Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2012-03-12
8
Medium Priority
?
368 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Interactive Way of Training for the AWS CSA Exam

An interactive way of learning that will help you visualize core concepts so that you can be more effective when taking your AWS certification exam.  Built for students by a student to help them understand the concepts that they are being taught.

 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

715 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