[英]Unable to Update GridView in ASP.NET C#
我正在嘗試更新並顯示GridView中存在的值。 它的作用是獲取標簽中存在的值,並在我嘗試更新它時切換到TextBox 。 之后,我想在GridView之外的標簽中顯示該特定值。
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%#Eval("Product_Quantity") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%#Eval("Product_Quantity") %>'></asp:TextBox>
</EditItemTemplate>
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
dt = new DataTable();
dt = (DataTable)Session["anime"];
dt.Rows[e.RowIndex]["Product_Quantity"] = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2")).Text;
Session["anime"] = dt;
GridView1.EditIndex = -1;
FillGrid();
Response.Redirect("view_cart.aspx");
}
protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView drv = e.Row.DataItem as DataRowView;
if (drv != null)
{
sum += Convert.ToInt32(((Label)e.Row.Cells[4].FindControl("Label4")).Text);
results.Text = sum.ToString();
}
}
}
每當我嘗試更新該行時,它將返回:“對象引用未設置為對象的實例。” 總和== Convert.ToInt32((((Label)e.Row.Cells [4] .FindControl(“ Label4”))。Text);
關於如何解決這個問題的任何想法?
編輯行時,將使用EditTemplate。 並且在其中沒有Label4。 因此找不到控件,並且代碼將引發異常。 然后,您必須搜索TextBox2控件。
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%#Eval("Product_Quantity") %>'></asp:TextBox>
</EditItemTemplate>
通過執行以下操作,我設法解決了這個問題:
protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowState == DataControlRowState.Edit)
{
}
else if (e.Row.RowType == DataControlRowType.DataRow)
{
sum += Convert.ToInt32(((Label)e.Row.Cells[4].FindControl("Label4")).Text);
results.Text = sum.ToString();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.