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
Solved

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

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

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

856 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