Asp.net使用Entity框架工作并在网格视图中按下删除按钮时删除该行,然后显示此项"该对象无法删除,因为在ObjectStateManager中找不到该对象。" " 这是ascx代码:
<%@ Control Language="C#" AutoEventWireup="true"
CodeBehind="ctrl_ResearcherMyResearch.ascx.cs"
Inherits="ITI.GeoXera.ASP.User_Controls.ctrl_ResearcherMyResearch" %>
<asp:GridView ID="grd_Researcher" runat="server" AutoGenerateColumns="False"
DataKeyNames="Id,Title" DataSourceID="Researcher"
OnRowDeleting="grd_Researcher_RowDeleting"
OnRowEditing="grd_Researcher_RowEditing"
OnRowUpdating="grd_Researcher_RowUpdating" PageSize="3" Width="900px"
AllowPaging="True" CellPadding="4" ForeColor="#333333" GridLines="None"
HorizontalAlign="Center" >
<AlternatingRowStyle HorizontalAlign="Center" VerticalAlign="Middle"
BackColor="White" />
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="True"
SortExpression="Id" >
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:BoundField>
<asp:BoundField DataField="Title" HeaderText="Title"
SortExpression="Title" >
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:BoundField>
<asp:BoundField DataField="Author" HeaderText="Author"
SortExpression="Author" >
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:BoundField>
<asp:BoundField DataField="Description" HeaderText="Description"
SortExpression="Description" >
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:BoundField>
<asp:BoundField DataField="LatitudeFrom" HeaderText="Lat"
SortExpression="LatitudeFrom" >
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
</asp:BoundField>
<asp:BoundField DataField="LongitudeFrom" HeaderText="Long"
SortExpression="LongitudeFrom" />
<asp:BoundField DataField="FkUser" HeaderText="FkUser"
SortExpression="FkUser" Visible="False" ReadOnly="True" />
<asp:BoundField DataField="Link" HeaderText="Link" SortExpression="Link"
/>
<asp:CommandField ButtonType="Button" DeleteText="" InsertText=""
NewText="" SelectText="" ShowEditButton="True" />
<asp:CommandField ButtonType="Button" InsertText="" NewText=""
SelectText="" ShowDeleteButton="True" />
</Columns>
<EditRowStyle HorizontalAlign="Center" VerticalAlign="Middle" Height="30px"
BackColor="#2461BF" Width="20px" Wrap="True" />
<EmptyDataRowStyle HorizontalAlign="Center" VerticalAlign="Middle" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Height="30px"
BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerSettings PageButtonCount="5" />
<PagerStyle VerticalAlign="Middle" BackColor="#2461BF" ForeColor="White"
HorizontalAlign="Center" />
<RowStyle HorizontalAlign="Center" VerticalAlign="Middle"
BackColor="#EFF3FB" Font-Strikeout="False" Width="50px" />
<SelectedRowStyle HorizontalAlign="Center" VerticalAlign="Middle"
BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
<asp:ObjectDataSource ID="Researcher" runat="server"
DataObjectTypeName="ITI.GeoXera.Model.Models.Research" DeleteMethod="Delete"
InsertMethod="Add" SelectMethod="GetAllBind"
TypeName="ITI.GeoXera.Data.Repositories.ResearchRepository"
UpdateMethod="Update"></asp:ObjectDataSource>
<p>
</p>
<div class="box box-info">
<asp:Panel ID="Panel1" runat="server" GroupingText=" Research Information">
<div class="box-body">
<div class="form-group">
<asp:Label CssClass="col-sm-2 control-label" ID="lbl_Title"
runat="server" Text="Title"></asp:Label>
<div class="col-sm-10">
<asp:TextBox CssClass="form-control" ID="txt_Title"
runat="server" AutoCompleteType="Disabled"></asp:TextBox>
<asp:RequiredFieldValidator ID="vldreq_Title" runat="server"
ControlToValidate="txt_Title" ForeColor="#CC0000" ToolTip="Please Enter
Research Title">*</asp:RequiredFieldValidator>
</div>
</div>
<div class="form-group">
<asp:Label ID="lbl_Author" runat="server" CssClass="col-sm-2
control-label" Text="Author"></asp:Label>
<div class="col-sm-10">
<asp:TextBox ID="txt_Author" runat="server"
AutoCompleteType="Disabled" CssClass="form-control" ></asp:TextBox>
<asp:RequiredFieldValidator ID="vldreq_Author" runat="server"
ErrorMessage="*" ForeColor="#CC0000" ToolTip="Please Enter Author Name"
ControlToValidate="txt_Author"></asp:RequiredFieldValidator>
</div>
</div>
<div class="form-group">
<asp:Label ID="lbl_Description" runat="server" CssClass="col-sm-2
control-label" Text="Description"></asp:Label>
<div class="col-sm-10">
<asp:TextBox ID="txt_Description" runat="server"
AutoCompleteType="Disabled" CssClass="form-control" ></asp:TextBox>
<asp:RequiredFieldValidator ID="vldreq_Description"
runat="server" ErrorMessage="*" ForeColor="#CC0000" ToolTip="Please Enter
Description" ControlToValidate="txt_Description">
</asp:RequiredFieldValidator>
</div>
</div>
<div class="form-group">
<asp:Label CssClass="col-sm-2 control-label" ID="lbl_LatitudeFrom"
runat="server" Text="Latitude From"></asp:Label>
<div class="col-sm-4">
<asp:TextBox CssClass="form-control" ID="txt_LatitudeFrom"
runat="server" AutoCompleteType="Disabled"></asp:TextBox>
<asp:RequiredFieldValidator ID="vlrLatFrom" runat="server"
ControlToValidate="txt_LatitudeFrom" ForeColor="#FF3300" ToolTip="Please
Enter
Required Data">*</asp:RequiredFieldValidator>
</div>
<asp:Label CssClass="col-sm-2 control-label" ID="lbl_LatitudeTo"
runat="server" Text="Latitude To"></asp:Label>
<div class="col-sm-4">
<asp:TextBox CssClass="form-control" ID="txt_LatitudeTo"
runat="server" AutoCompleteType="Disabled"></asp:TextBox>
<asp:RequiredFieldValidator ID="vlrLatTo" runat="server"
ControlToValidate="txt_LatitudeTo" ForeColor="#FF3300" ToolTip="Please Enter
Required Data">*</asp:RequiredFieldValidator>
</div>
</div>
<div class="form-group">
<asp:Label CssClass="col-sm-2 control-label" ID="lbl_LongitudeFrom"
runat="server" Text="Longitude From"></asp:Label>
<div class="col-sm-4">
<asp:TextBox CssClass="form-control" ID="txt_LongitudeFrom"
runat="server" AutoCompleteType="Disabled"></asp:TextBox>
<asp:RequiredFieldValidator ID="vlrLongFrom" runat="server"
ControlToValidate="txt_LongitudeFrom" ForeColor="#FF3300" ToolTip="Please
Enter
Required Data">*</asp:RequiredFieldValidator>
</div>
<asp:Label CssClass="col-sm-2 control-label" ID="lbl_LongitudeTo"
runat="server" Text="Longitude To"></asp:Label>
<div class="col-sm-4">
<asp:TextBox CssClass="form-control" ID="txt_LongitudeTo"
runat="server" AutoCompleteType="Disabled"></asp:TextBox>
<asp:RequiredFieldValidator ID="vlrLongTo" runat="server"
ControlToValidate="txt_LongitudeTo" ForeColor="#FF3300" ToolTip="Please
Enter Required Data">*</asp:RequiredFieldValidator>
</div>
</div>
<div class="form-group">
<asp:Label CssClass="col-sm-2 control-label" ID="lbl_Link"
runat="server" Text="Link"></asp:Label>
<div class="col-sm-10">
<asp:TextBox CssClass="form-control" ID="txt_Link"
runat="server" AutoCompleteType="Disabled"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
runat="server" ControlToValidate="txt_Link" ForeColor="#FF3300"
ToolTip="Please Enter Link Path">*</asp:RequiredFieldValidator>
</div>
</div>
</div>
<div class="box-footer">
<asp:Button CssClass="btn btn-info pull-right" ID="btn_SaveInfo"
runat="server" Text="Save" OnClick="btn_SaveInfo_OnClick" />
</div>
ascx.cs代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Windows.Forms;
using ITI.GeoXera.Data.InfraStructure;
using ITI.GeoXera.Data.Repositories;
using ITI.GeoXera.Model.Models;
namespace ITI.GeoXera.ASP.User_Controls
{
public partial class ctrl_ResearcherMyResearch : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//grd_Researcher.DataBind();
}
}
protected void grd_Researcher_RowUpdating(object sender,
GridViewUpdateEventArgs e)
{
using (UnitOfWork uof = new UnitOfWork())
{
int index = e.RowIndex;
Research research = new Research();
User user = new User();
research.Id = Convert.ToInt32(grd_Researcher.DataKeys[index]
["Id"]);
research.Title = e.NewValues["Title"].ToString();
research.Description = e.NewValues["Description"].ToString();
research.Author = e.NewValues["Author"].ToString();
research.LatitudeFrom =
Convert.ToDouble(e.NewValues["LatitudeFrom"]);
research.LatitudeTo =
Convert.ToDouble(e.NewValues["LatitudeTo"]);
research.LongitudeFrom =
Convert.ToDouble(e.NewValues["LongitudeFrom"]);
research.LongitudeTo =
Convert.ToDouble(e.NewValues["LongitudeTo"]);
research.Link = e.NewValues["Link"].ToString();
research.FkUser = 1;
uof.ResearchRepository.Update(research);
grd_Researcher.EditIndex = -1;
grd_Researcher.DataBind();
uof.Commit();
}
}
protected void grd_Researcher_RowEditing(object sender,
GridViewEditEventArgs e)
{
using (UnitOfWork uof = new UnitOfWork())
{
grd_Researcher.EditIndex = e.NewEditIndex;
grd_Researcher.DataBind();
}
}
protected void grd_Researcher_RowDeleting(object sender,
GridViewDeleteEventArgs e)
{
string Title =
Convert.ToString(grd_Researcher.DataKeys[e.RowIndex].Values[1]);
using (UnitOfWork uof = new UnitOfWork())
{
if (Title != null || Title != "")
{
Research research =
uof.ResearchRepository.GetAll().Where(res => res.Title ==
Title).FirstOrDefault();
if (research != null)
{
uof.ResearchRepository.Delete(research);
uof.Commit();
//grd_Researcher.DataSource =
uof.ResearchRepository.GetAllBind();
MessageBox.Show("Deleted successfully.");
}
else
{
MessageBox.Show("Not Found");
}
}
grd_Researcher.EditIndex = -1;
grd_Researcher.DataBind();
}
}
protected void btn_SaveInfo_OnClick(object sender, EventArgs e)
{
using (UnitOfWork uof = new UnitOfWork())
{
User u = new User();
bool entryFound = false;
Research research = new Research();
foreach (GridViewRow row in grd_Researcher.Rows)
{
object val1 = row.Cells[1].Text;
if (val1 != null && val1.ToString() == txt_Title.Text)
{
MessageBox.Show("Entry already exist");
entryFound = true;
break;
}
}
if (!entryFound)
{
research.Title = txt_Title.Text;
research.Author = txt_Author.Text;
research.Description = txt_Description.Text;
research.LatitudeFrom =
Convert.ToDouble(txt_LatitudeFrom.Text);
research.LatitudeTo = Convert.ToDouble(txt_LatitudeTo.Text);
research.LongitudeFrom =
Convert.ToDouble(txt_LongitudeFrom.Text);
research.LongitudeTo =
Convert.ToDouble(txt_LongitudeTo.Text);
research.Link = txt_Link.Text;
research.FkUser = Convert.ToInt32(Session["UserID"]);
uof.ResearchRepository.Add(research);
grd_Researcher.DataBind();
uof.Commit();
MessageBox.Show("Data Added");
foreach (var control in this.Controls)
{
System.Web.UI.WebControls.TextBox tb = control as
System.Web.UI.WebControls.TextBox;
if (tb != null)
{
tb.Text = string.Empty;
}
}
uof.ResearchRepository.GetAllBind();
}
}
}
}
}
提前致谢
答案 0 :(得分:0)
您确定要删除的行在&#34;标题&#34;中有值。您使用if (Title != null || Title != "")
发布的代码存在允许null和空值的错误。如果你想阻止它,它应该是if (Title != null && Title != "")
。