【发布时间】:2020-01-09 03:33:49
【问题描述】:
我的 apsx
<section class="sec1" style="height:100vh;">
<link href="../Css/masterStyle.css" rel="stylesheet" />
<link href="../Css/cartStyle.css" rel="stylesheet" />
<h1>Cart</h1>
<p class="sec1_p1">Your Food</p>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" EnableTheming="True" ShowFooter="True" OnRowDeleting="GridView1_RowDeleting" >
<Columns>
<asp:BoundField DataField="sno" HeaderText="sno" Visible="False" />
<asp:BoundField DataField="restaurantID" HeaderText="restaurantID" Visible="False" />
<asp:BoundField DataField="foodID" HeaderText="foodID" Visible="False">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="foodName" HeaderText="Name">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="foodPrice" HeaderText="Price">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="quantity" HeaderText="Quantity">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="totalPrice" HeaderText="Total ">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:CommandField DeleteText="Remove" ShowDeleteButton="True" />
</Columns>
<HeaderStyle BackColor="#52E770" ForeColor="White" />
</asp:GridView>
<asp:Label ID="test" runat="server" Text="Label"></asp:Label>
</section>
我的 .cs
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
DataTable dt = new DataTable();
dt = (DataTable)Session["buyitems"];
for (int i = 0; i <= dt.Rows.Count - 1; i++)
{
int sr;
int sr1;
string qdata;
string dtdata;
sr = Convert.ToInt32(dt.Rows[i]["sno"].ToString());
TableCell cell = GridView1.Rows[e.RowIndex].Cells[0];
qdata = cell.Text;
dtdata = sr.ToString();
sr1 = Int32.Parse(qdata); //fixed
if (sr == sr1)
{
dt.Rows[i].Delete();
dt.AcceptChanges();
//Label1.Text = "Item Has Been Deleted From Shopping Cart";
break;
}
}
for (int i = 1; i <= dt.Rows.Count; i++)
{
dt.Rows[i - 1]["sno"] = i;
dt.AcceptChanges();
}
Session["buyitems"] = dt;
Response.Redirect("AddToCart.aspx");
}
我在 Visual Studio 中使用了 Add Wacth,我得到了这个结果
1.$exception {"输入字符串的格式不正确。"} System.FormatException
2.qdata = cell.Text 这个表达式会产生副作用,不会被求值
3.TableCell 单元格 = GridView1.Rows[e.RowIndex].Cells[0];错误 CS1073:意外的令牌“单元格”
【问题讨论】:
-
sr1 = Int32.Parse(qdata); //Wrong为什么你把Parse变成Int32而sr1的类型是string? -
@Anonymous:不,
sr1的类型是int。 -
改用
Int32.TryParse。 -
此错误意味着您的字符串(在您的情况下为 qdata)不是有效的整数。要修复它,您可以使用
TryParse -
能否请您调试以查看
qdata = cell.Text的值。恐怕不是数字