网格视图中的复选框导致删除按钮出错(Web表单c#)
我在web应用程序中有一个网格视图,它使用C#从列表中获取数据。我想删除栅格视图的选定行。我尝试了很多代码,但在“if((CheckBox)row.FindControl(“chkAll”).Checked)”和“if((CheckBox)row.FindControl(“chkBox”)).Checked”行中出现错误“Object reference not set to a instance of a Object.”。如果有人能帮助我,让我知道问题出在哪里,我将不胜感激。非常感谢你网格视图中的复选框导致删除按钮出错(Web表单c#),c#,html,gridview,C#,Html,Gridview,我在web应用程序中有一个网格视图,它使用C#从列表中获取数据。我想删除栅格视图的选定行。我尝试了很多代码,但在“if((CheckBox)row.FindControl(“chkAll”).Checked)”和“if((CheckBox)row.FindControl(“chkBox”)).Checked”行中出现错误“Object reference not set to a instance of a Object.”。如果有人能帮助我,让我知道问题出在哪里,我将不胜感激。非常感谢你 My
My Html Code:
<asp:GridView ID="GridViewProducts" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductID"
AllowPaging="True" EnableViewState="False" CssClass="mydatagrid" PagerStyle-CssClass="pager"
HeaderStyle-CssClass="header" RowStyle-CssClass="rows" ShowFooter="True" OnPageIndexChanging="OnPaging"
PageSize="10" OnRowDataBound = "RowDataBound" >
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="checkAll" runat="server" onclick = "checkAll(this);" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chk" runat="server" onclick = "Check_Click(this)" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ProductName" HeaderText="Product" ReadOnly="True" SortExpression="ProductName" />
<asp:BoundField DataField="ProductID" HeaderText="Product ID" ReadOnly="True" SortExpression="ProductID" />
<asp:BoundField DataField="UnitPrice" HeaderText="Unit Price" HtmlEncode="False" SortExpression="UnitPrice" />
<asp:BoundField DataField="Quantity" HeaderText="Quantity" ReadOnly="True" SortExpression="Quantity" />
<asp:BoundField DataField="TotalPrice" HeaderText="Total Price" ReadOnly="True" SortExpression="TotalPrice" />
<asp:CommandField ShowDeleteButton="true" />
</Columns>
<HeaderStyle BackColor="#bd62dd" BorderStyle="Double" BorderColor="#49093c" Font-Bold="True" ForeColor="White"/>
<FooterStyle BackColor="#bd62dd" BorderStyle="Outset" BorderColor="#49093c" Font-Bold="True" ForeColor="White" />
</asp:GridView>
My back code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridView();
}
}
private void BindGridView()
{
List<CartItem> item = new List<CartItem>();
item = Session["listOfOrder"] as List<CartItem>;
int count = 0;
bool English = true;
if(item != null)
{
int Total = 0;
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("ProductName", typeof(string)));
dt.Columns.Add(new DataColumn("ProductID", typeof(string)));
dt.Columns.Add(new DataColumn("UnitPrice", typeof(string)));
dt.Columns.Add(new DataColumn("Quantity", typeof(int)));
dt.Columns.Add(new DataColumn("TotalPrice", typeof(string)));
foreach (var items in item)
{
DataRow dr = dt.NewRow();
dr["ProductName"] = items.Name;
dr["ProductID"] = items.Id;
dr["UnitPrice"] = items.UnitPrice;
dr["Quantity"] = items.Quantity;
dr["TotalPrice"] = items.ItemTotal;
dt.Rows.Add(dr);
Total += Total + items.ItemTotal;
count++;
}
ViewState["datatable"] = dt;
GridViewProducts.DataSource = dt;
GridViewProducts.DataBind();
GridViewProducts.FooterRow.Cells[4].Text = "Total";
GridViewProducts.FooterRow.Cells[4].HorizontalAlign = HorizontalAlign.Right;
GridViewProducts.FooterRow.Cells[5].Text = Total;
lblDeleteResults.Visible = true;
lblDeleteResults.Text = "List is empty.";
lblDeleteResults.ForeColor = System.Drawing.Color.Red;
}
protected void btnDelete_Click(object sender, EventArgs e)
{
List<CartItem> item = Session["listOfOrder"] as List<CartItem>;
int Count = 1;
BindGridView();
foreach (GridViewRow row in GridViewProducts.Rows)
{
if (((CheckBox)row.FindControl("chkAll")).Checked)
{
item = null;
}
if (((CheckBox)row.FindControl("chkBox")).Checked)
{
item.Remove(item[Count]);
Count++;
}
}
Session["listOfOrder"] = item;
BindGridView();
}
我的Html代码:
我的后台代码:
受保护的无效页面加载(对象发送方、事件参数e)
{
如果(!IsPostBack)
{
BindGridView();
}
}
私有void BindGridView()
{
列表项=新列表();
项目=会话[“listOfOrder”]作为列表;
整数计数=0;
布尔英语=真;
如果(项!=null)
{
int-Total=0;
DataTable dt=新的DataTable();
添加(新数据列(“ProductName”,typeof(string));
Add(新的数据列(“ProductID”,typeof(string));
添加(新数据列(“单价”,类型为(字符串));
添加(新数据列(“数量”,类型(int));
添加(新数据列(“TotalPrice”,typeof(string));
foreach(项目中的var项目)
{
DataRow dr=dt.NewRow();
dr[“ProductName”]=项目名称;
dr[“ProductID”]=items.Id;
dr[“单价”]=项目单价;
dr[“数量”]=项目数量;
dr[“TotalPrice”]=items.ItemTotal;
dt.Rows.Add(dr);
合计+=合计+项目。项目合计;
计数++;
}
视图状态[“数据表”]=dt;
GridViewProducts.DataSource=dt;
GridViewProducts.DataBind();
GridViewProducts.FooterRow.Cells[4].Text=“总计”;
GridViewProducts.FooterRow.Cells[4]。HorizontalAlign=HorizontalAlign.Right;
GridViewProducts.FooterRow.Cells[5].Text=Total;
lblDeleteResults.Visible=true;
lblDeleteResults.Text=“列表为空。”;
lblDeleteResults.ForeColor=System.Drawing.Color.Red;
}
受保护的无效BTN删除\单击(对象发送者,事件参数e)
{
列表项=会话[“listOfOrder”]作为列表;
整数计数=1;
BindGridView();
foreach(GridViewProducts.Rows中的GridViewRow行)
{
if(((复选框)row.FindControl(“chkAll”)).Checked)
{
item=null;
}
if(((复选框)row.FindControl(“chkBox”)).Checked)
{
项目。移除(项目[计数]);
计数++;
}
}
会话[“listOfOrder”]=项目;
BindGridView();
}
没有看到您的行。非常感谢您的回复。很抱歉,它似乎被删除了,我没有注意到。我已经添加了代码。非常感谢您事先的帮助。如果有人能回答我,我将不胜感激。我很紧急。我感谢您的帮助朋友。非常感谢。