First let me preface this with the fact that I know this question has been asked a slew of times. I've tried implementing everything I've read and have not resolved my issue yet.
I have a shopping cart being displayed in a GridView and I am trying to get the update function to work. After I click edit (on the row in question) and I change the value in the textbox, and then click update, I am always getting an empty string returned.
I've read countless posts about it being a PostBack issue, however I have encapsulated my DataBind method within an if(!Page.IsPostBack){}
so I dont believe that is my issue.
These are the relevant methods in my CodeFile:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["thisCart"] == null)
{
Session["thisCart"] = new ShoppingCart();
}
thisCart = (ShoppingCart)Session["thisCart"];
if (!Page.IsPostBack)
{
FillData();
}
}
private void FillData()
{
gvShoppingCart.DataSource = thisCart.Items;
gvShoppingCart.DataBind();
if (thisCart.Items.Count == 0)
{
lblGrandTotal.Visible = false;
}
else
{
lblGrandTotal.Text = string.Format("Grand Total = {0,19:C}", thisCart.GrandTotal);
lblGrandTotal.Visible = true;
}
}
protected void gvShoppingCart_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox txtQuantity = (TextBox)gvShoppingCart.Rows[e.RowIndex].Cells[3].Controls[0];
int quantity = Int32.Parse(txtQuantity.Text);
thisCart.Update(e.RowIndex, quantity);
gvShoppingCart.EditIndex = -1;
FillData();
}
And this is the aspx file:
<asp:GridView ID="gvShoppingCart" runat="server" AutoGenerateColumns="False" OnRowCancelingEdit="gvShoppingCart_RowCancelingEdit" OnRowDeleting="gvShoppingCart_RowDeleting" OnRowEditing="gvShoppingCart_RowEditing" OnRowUpdating="gvShoppingCart_RowUpdating" ClientIDMode="AutoID">
<Columns>
<asp:BoundField DataField="NAME" HeaderText="Product name" ReadOnly="True" />
<asp:ImageField DataImageUrlField="IMAGE" DataImageUrlFormatString="~\Images\{0}" HeaderText="Image" ReadOnly="True">
</asp:ImageField>
<asp:BoundField DataField="PRICE" HeaderText="Price" ReadOnly="True" />
<asp:BoundField DataField="QUANTITY" HeaderText="Quantity" />
<asp:TemplateField HeaderText="Total">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Double.Parse(Eval("PRICE").ToString()) * Int32.Parse(Eval("QUANTITY").ToString()) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>
I'm usually pretty good at finding answers, but this one has got me stumped.
The site crashes after clicking update because the returned value from int quantity = Int32.Parse(txtQuantity.Text);
is "" and obviously you cant parse an int from that.
As for the tag of possible duplicate of "ASP.NET validation error message to change labels text." I was unable to find anything that points me in the direction of an answer.
Call FillData()
in Page_PreInit
. Otherwise, when the ASP.Net runtime goes to restore ViewState for the grid, including your new Textbox data, there's nowhere to put it yet. Remember that every single server event uses a brand new instance of your page class and must rebuild the entire page from scratch.
This was a royal pain to figure out since I'm just learning ASP.net and C#.
When I was able to make this work on an earlier assignment I wasn't using a Master page, with this project I am. The problem lies in the fact that I am using a <form runat="server">
tag in the Master page. Since you can only have one <form runat="server">
tag per page, my GridView wasn't wrapped in one. Once I removed the Master page and added the <form runat="server">
wrapper around the GridView, everything works as intended.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.