ObjectDataSource 插入抛出错误(使用业务对象)
快速概述
- 尝试使用 ObjectDataSource(在 GridView 上)插入业务对象
- 出现以下错误
ObjectDataSource 'ObjectDataSource1' 没有要插入的值。 检查“值”字典是否包含值。
项目设置
- Person - 简单的虚拟业务对象(姓名和年龄)
- PersonBinder - 保存对象数据源的方法(在本例中为选择和插入)
- InsertGrid - 简单的网格(继承 GridView)添加“添加”页脚上的按钮,依次调用数据源的插入
- 默认 - ASPX 页面,保存网格和数据源(将插入参数应用于数据源)
注意我添加了 TODO 注释,我认为是关键区域
代码
个人绑定器 这里漏掉了这个人(它有 2 个属性)是活页夹
/// <summary>
/// A binding Class.
/// </summary>
public class PersonBinder
{
public IEnumerable<Person> Select()
{
List<Person> people = new List<Person>();
for (int i = 0; i < 9; i++)
{
Person person = new Person();
person.Name = "Name " + i.ToString();
person.Age = i;
people.Add(person);
}
return people;
}
public void Insert(Person p)
{
//TODO: the Insert Method
//errors before this.
}
}
InsertGrid
public class InsertGrid : GridView
{
protected override void OnInit(System.EventArgs e)
{
base.OnInit(e);
ShowFooter = true;
DataBind();
}
/// <summary>
/// here to handle button clicks.
/// </summary>
private void ModeCommand(object sender, CommandEventArgs e)
{
switch (e.CommandName)
{
case "Add":
//ObjectDataSource objectDataSource = DataSource as ObjectDataSource;
ObjectDataSource objectDataSource = Parent.FindControl(DataSourceID) as ObjectDataSource;
if (objectDataSource != null)
{
//TODO: Errors HERE!
objectDataSource.Insert();
}
break;
}
}
/// <summary>
/// Raises the <see cref="E:System.Web.UI.WebControls.GridView.RowDataBound"/> event.
/// </summary>
/// <param name="e">A <see cref="T:System.Web.UI.WebControls.GridViewRowEventArgs"/> that contains event data.</param>
protected override void OnRowDataBound(GridViewRowEventArgs e)
{
base.OnDataBound(e);
//add an insert button
if (e.Row.RowType == DataControlRowType.Footer)
{
ImageButton ibtnAdd = new ImageButton();
ibtnAdd.ID = "Add";
ibtnAdd.CommandName = "Add";
ibtnAdd.ToolTip = "Add new Item";
ibtnAdd.ImageAlign = ImageAlign.AbsMiddle;
ibtnAdd.Style.Add("cursor", "pointer");
ibtnAdd.CausesValidation = true;
ibtnAdd.Command += ModeCommand;
ibtnAdd.Enabled = true;
e.Row.Cells[0].Controls.Add(ibtnAdd);
}
}
}
Default HTML
<form id="form1" runat="server">
<div>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
DataObjectTypeName="GridViewSample.Person" InsertMethod="Insert"
SelectMethod="Select" TypeName="GridViewSample.PersonBinder">
</asp:ObjectDataSource>
</div>
<br />
<cc1:InsertGrid ID="InsertGrid1" runat="server" AutoGenerateColumns="False"
DataSourceID="ObjectDataSource1">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Age" HeaderText="Age" SortExpression="Age" />
</Columns>
</cc1:InsertGrid>
</form>
Default CodeBehind
public partial class _Default : System.Web.UI.Page
{
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
//TODO: here is the insert PARAMs. what am i missing.
ObjectDataSource1.InsertParameters.Add("Name", "");
ObjectDataSource1.InsertParameters.Add("Age", "0");
}
protected void Page_Load(object sender, EventArgs e)
{
//TODO: Tried this too
IDataSource ds = ObjectDataSource1;
DataSourceView view = ds.GetView(InsertGrid1.DataMember);
Dictionary<string, string> values = new Dictionary<string, string>();
values.Add("Name", "");
//values.Add("Age", "0");
view.Insert(values, delegate { return false; });
}
}
Quick overview
- Trying to insert a Business Object using ObjectDataSource (on a GridView)
- Get the following Error
ObjectDataSource 'ObjectDataSource1' has no values to insert. Check that the 'values' dictionary contains values.
Project SetUp
- Person - simple dummy Business Object (Name and Age)
- PersonBinder - Holds the methods for the Object DataSource (Select and Insert in this case)
- InsertGrid - simple Grid (inherits GridView) add an "Add" button on the Footer, which inturn calls the DataSource's Insert
- Default - ASPX page, holds the Grid and Datasource, (applies Insert params to the DataSorce)
Note I added TODO comments around, what i think are key areas
The Code
Person Binder
Missing out the person (its get 2 properties) here is the binder
/// <summary>
/// A binding Class.
/// </summary>
public class PersonBinder
{
public IEnumerable<Person> Select()
{
List<Person> people = new List<Person>();
for (int i = 0; i < 9; i++)
{
Person person = new Person();
person.Name = "Name " + i.ToString();
person.Age = i;
people.Add(person);
}
return people;
}
public void Insert(Person p)
{
//TODO: the Insert Method
//errors before this.
}
}
InsertGrid
public class InsertGrid : GridView
{
protected override void OnInit(System.EventArgs e)
{
base.OnInit(e);
ShowFooter = true;
DataBind();
}
/// <summary>
/// here to handle button clicks.
/// </summary>
private void ModeCommand(object sender, CommandEventArgs e)
{
switch (e.CommandName)
{
case "Add":
//ObjectDataSource objectDataSource = DataSource as ObjectDataSource;
ObjectDataSource objectDataSource = Parent.FindControl(DataSourceID) as ObjectDataSource;
if (objectDataSource != null)
{
//TODO: Errors HERE!
objectDataSource.Insert();
}
break;
}
}
/// <summary>
/// Raises the <see cref="E:System.Web.UI.WebControls.GridView.RowDataBound"/> event.
/// </summary>
/// <param name="e">A <see cref="T:System.Web.UI.WebControls.GridViewRowEventArgs"/> that contains event data.</param>
protected override void OnRowDataBound(GridViewRowEventArgs e)
{
base.OnDataBound(e);
//add an insert button
if (e.Row.RowType == DataControlRowType.Footer)
{
ImageButton ibtnAdd = new ImageButton();
ibtnAdd.ID = "Add";
ibtnAdd.CommandName = "Add";
ibtnAdd.ToolTip = "Add new Item";
ibtnAdd.ImageAlign = ImageAlign.AbsMiddle;
ibtnAdd.Style.Add("cursor", "pointer");
ibtnAdd.CausesValidation = true;
ibtnAdd.Command += ModeCommand;
ibtnAdd.Enabled = true;
e.Row.Cells[0].Controls.Add(ibtnAdd);
}
}
}
Default HTML
<form id="form1" runat="server">
<div>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
DataObjectTypeName="GridViewSample.Person" InsertMethod="Insert"
SelectMethod="Select" TypeName="GridViewSample.PersonBinder">
</asp:ObjectDataSource>
</div>
<br />
<cc1:InsertGrid ID="InsertGrid1" runat="server" AutoGenerateColumns="False"
DataSourceID="ObjectDataSource1">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Age" HeaderText="Age" SortExpression="Age" />
</Columns>
</cc1:InsertGrid>
</form>
Default CodeBehind
public partial class _Default : System.Web.UI.Page
{
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
//TODO: here is the insert PARAMs. what am i missing.
ObjectDataSource1.InsertParameters.Add("Name", "");
ObjectDataSource1.InsertParameters.Add("Age", "0");
}
protected void Page_Load(object sender, EventArgs e)
{
//TODO: Tried this too
IDataSource ds = ObjectDataSource1;
DataSourceView view = ds.GetView(InsertGrid1.DataMember);
Dictionary<string, string> values = new Dictionary<string, string>();
values.Add("Name", "");
//values.Add("Age", "0");
view.Insert(values, delegate { return false; });
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
下面的确实有效。
我需要做的是删除/更换
The following does actually work.
what I needed to do was to remove/replace the