Solved

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

Posted on 2012-03-12
8
361 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 74

Expert Comment

by:käµfm³d 👽
Comment Utility
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
Comment Utility
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
Comment Utility
Could you include your entire GridView code.

Also can you provide the GridRowBound event code as well?
0
 

Author Comment

by:Eindoofus
Comment Utility
<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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 7

Expert Comment

by:CloudedTurtle
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now