我正在使用edititemtemplate
的Gridview。我的编辑和删除操作在同一gridview上。
单击删除链接时,每次都会出现空引用错误,但无法弄清楚原因。删除链接的标记为:
<asp:LinkButton ID="lnkdelete" runat="server" CommandName="delete"
Font-Overline="false" Text="Delete" ToolTip="Delete"
OnClientClick="return confirm('Do you want to delete this record?');">
</asp:LinkButton>
我的服务器端代码如下:
protected void GridView_arealist_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
try
{
int areaId = Convert.ToInt32(((Label)(GridView_arealist.Rows[e.RowIndex].FindControl("lblareaIdEdit"))).Text);
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["MCC"].ConnectionString;
con.Open();
string deleteQuery = "delete from map where id=@id";
SqlCommand objCmd = new SqlCommand(deleteQuery, con);
objCmd.Parameters.AddWithValue("@id", areaId);
objCmd.ExecuteNonQuery();
objCmd.Dispose();
con.Close();
LoadAreaDetails();
}
catch (Exception ex)
{
throw;
}
}
我在下面的代码段中得到空引用异常:
int areaId = Convert.ToInt32(((Label)(GridView_arealist.Rows[e.RowIndex].FindControl("lblareaIdEdit"))).Text);
在其他函数中也使用相同的代码段来获取ID,效果很好。