在ASP.NET Grid中编辑第一行Row,RowUpdating事件的时候,如果这样写:
经过反复跟踪调试,我发现如果这样以来好像第一行的对象内部的字段没有被ActiveRecord赋上值,所以出错。
现在我想了个笨办法把问题解决了,不知道有没有更好的办法。
方法的思路是这样的:在Update的时候SessionScope已经用过了,那么在获得DataSource的时候就再新用一个SessionScope。用下面这样的代码就解决问题了:
代码中涉及一些在ASP.NET用使用ActiveRecord的方法和机制,请参看http://www.castleproject.com/
protected void grdCatalog_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
this.grdCatalog.SelectedIndex = e.RowIndex;
int id = (int)this.grdCatalog.SelectedValue;
Catalog ca = CatalogDataAccess.FindCatalogByID(id);
ca.Name = (grdCatalog.Rows[e.RowIndex].Cells[1].Controls[0] as TextBox).Text;
ca.Comment = (grdCatalog.Rows[e.RowIndex].Cells[2].Controls[0] as TextBox).Text;
ca.Culture =
(grdCatalog.Rows[e.RowIndex].Cells[3].FindControl("drpCulture") as DropDownList).SelectedValue;
ca.Update();
this.grdCatalog.EditIndex = -1;
this.grdCatalog.DataSource = CatalogDataAccess.FindAll(this.PageIndex, pageSize);
this.grdCatalog.DataBind();
}
那么在DataBind()的时候就说报告一个类型不匹配的错误。如果编辑第一行以外的行都正常,就第一行不行。{
this.grdCatalog.SelectedIndex = e.RowIndex;
int id = (int)this.grdCatalog.SelectedValue;
Catalog ca = CatalogDataAccess.FindCatalogByID(id);
ca.Name = (grdCatalog.Rows[e.RowIndex].Cells[1].Controls[0] as TextBox).Text;
ca.Comment = (grdCatalog.Rows[e.RowIndex].Cells[2].Controls[0] as TextBox).Text;
ca.Culture =
(grdCatalog.Rows[e.RowIndex].Cells[3].FindControl("drpCulture") as DropDownList).SelectedValue;
ca.Update();
this.grdCatalog.EditIndex = -1;
this.grdCatalog.DataSource = CatalogDataAccess.FindAll(this.PageIndex, pageSize);
this.grdCatalog.DataBind();
}
经过反复跟踪调试,我发现如果这样以来好像第一行的对象内部的字段没有被ActiveRecord赋上值,所以出错。
现在我想了个笨办法把问题解决了,不知道有没有更好的办法。
方法的思路是这样的:在Update的时候SessionScope已经用过了,那么在获得DataSource的时候就再新用一个SessionScope。用下面这样的代码就解决问题了:
protected void grdCatalog_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
this.grdCatalog.SelectedIndex = e.RowIndex;
int id = (int)this.grdCatalog.SelectedValue;
Catalog ca = CatalogDataAccess.FindCatalogByID(id);
SessionScope scope = HttpContext.Current.Items["nh.sessionscope"] as SessionScope;
ca.Name = (grdCatalog.Rows[e.RowIndex].Cells[1].Controls[0] as TextBox).Text;
ca.Comment = (grdCatalog.Rows[e.RowIndex].Cells[2].Controls[0] as TextBox).Text;
ca.Culture =
(grdCatalog.Rows[e.RowIndex].Cells[3].FindControl("drpCulture") as DropDownList).SelectedValue;
ca.Update();
if (scope != null)
{
scope.Dispose();
}
using(scope =new SessionScope())
{
HttpContext.Current.Items.Remove("nh.sessionscope");
HttpContext.Current.Items.Add("nh.sessionscope",scope);
this.grdCatalog.EditIndex = -1;
this.grdCatalog.DataSource = CatalogDataAccess.FindAll(this.PageIndex, pageSize);
this.grdCatalog.DataBind();
}
}
{
this.grdCatalog.SelectedIndex = e.RowIndex;
int id = (int)this.grdCatalog.SelectedValue;
Catalog ca = CatalogDataAccess.FindCatalogByID(id);
SessionScope scope = HttpContext.Current.Items["nh.sessionscope"] as SessionScope;
ca.Name = (grdCatalog.Rows[e.RowIndex].Cells[1].Controls[0] as TextBox).Text;
ca.Comment = (grdCatalog.Rows[e.RowIndex].Cells[2].Controls[0] as TextBox).Text;
ca.Culture =
(grdCatalog.Rows[e.RowIndex].Cells[3].FindControl("drpCulture") as DropDownList).SelectedValue;
ca.Update();
if (scope != null)
{
scope.Dispose();
}
using(scope =new SessionScope())
{
HttpContext.Current.Items.Remove("nh.sessionscope");
HttpContext.Current.Items.Add("nh.sessionscope",scope);
this.grdCatalog.EditIndex = -1;
this.grdCatalog.DataSource = CatalogDataAccess.FindAll(this.PageIndex, pageSize);
this.grdCatalog.DataBind();
}
}
代码中涉及一些在ASP.NET用使用ActiveRecord的方法和机制,请参看http://www.castleproject.com/