【发布时间】:2013-02-27 14:05:24
【问题描述】:
单击动态加载的 FormView 模板上的插入按钮时出现以下错误。默认表单模式设置为插入。我确实看到插入模板加载正常。我有 TypeName="WebApplication1.Repository.AccountRepo" 和 DataObjectTypeName="WebApplication1.Model.Account" 来更正类型。有人可以帮忙吗?
我得到的错误是 ObjectDataSource 'ObjectDataSource1' 没有要插入的值。检查“值”字典是否包含值。
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<h2>
Welcome to ASP.NET!
</h2>
<p>
To learn more about ASP.NET visit <a href="http://www.asp.net" title="ASP.NET Website">
www.asp.net</a>.
</p>
<p>
You can also find <a href="http://go.microsoft.com/fwlink/?LinkID=152368&clcid=0x409"
title="MSDN ASP.NET Docs">documentation on ASP.NET at MSDN</a>.
<uc1:WebUserControl1 ID="WebUserControl11" runat="server" />
</p>
<asp:FormView ID="FormView1" runat="server" DataSourceID="ObjectDataSource1"
OnLoad="FormView1_Load" OnModeChanged="FormView1_ModeChanged"
oninit="FormView1_Init" ViewStateMode="Enabled" DefaultMode="Insert">
</asp:FormView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" DataObjectTypeName="WebApplication1.Model.Account"
DeleteMethod="Delete" SelectMethod="Select" TypeName="WebApplication1.Repository.AccountRepo"
UpdateMethod="Update" oninserting="ObjectDataSource1_Inserting"
InsertMethod="Insert">
<InsertParameters>
<asp:Parameter Name="acct" Type="Object" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="acct" Type="Object"/>
</UpdateParameters>
</asp:ObjectDataSource>
</asp:Content>
页面初始化方法中的以下代码
public partial class _Default : System.Web.UI.Page
{
protected void Page_Init(object sender, EventArgs e)
{
//ITemplate control = Page.LoadTemplate("~/WebUserControl2.ascx");
FormView1.EditItemTemplate = Page.LoadTemplate("~/WebUserControl2.ascx");
FormView1.InsertItemTemplate = Page.LoadTemplate("~/WebUserControl2.ascx");
FormView1.ItemTemplate = Page.LoadTemplate("~/WebUserControl2.ascx");
}
}
我在 WebUserControl2.ascx 中有以下 HTML 片段
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl2.ascx.cs"
Inherits="WebApplication1.WebUserControl2" ViewStateMode="Enabled" %>
Name:
<asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>' />
<br />
Address:
<asp:TextBox ID="AddressTextBox" runat="server" Text='<%# Bind("Address") %>' />
<br />
<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert"
Text="Insert" />
<asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False"
CommandName="Cancel" Text="Cancel" />
这是业务类设置为 ObjectDataSource1 的示例存储库类
public class AccountRepo
{
private static Model.Account _account;
public void Delete(Model.Account acct)
{
_account = new Model.Account();
}
public Model.Account Update(Model.Account acct)
{
_account = acct;
return _account;
}
public Model.Account Insert(Model.Account acct)
{
_account = new Model.Account() { Name = "new" };
return _account;
}
public Model.Account Select()
{
if (_account == null)
_account = new Model.Account() { Name = "BrandNew" };
return _account;
}
}
这是帐户类
public class Account
{
public string Name { get; set; }
public string Address {get;set;}
public static Account CreateAccount(){
return new Account();
}
}
【问题讨论】:
-
我感觉
InsertParameters有问题,但我很难确定是什么。你的代码怎么知道“acct”是什么? -
是Insert和Update方法的参数名。
-
但是,如果我把 WebUserControl2.ascx 的内容放到
它没有错误,只有在动态加载模板时它无法创建表单值集合? -
哦,这是一个有趣的细节。是的,我从未尝试过以这种方式加载模板。
-
我看到了这个问题forums.asp.net/t/1069339.aspx,看起来这家伙在 5 年前遇到了和我一样的问题,似乎没有回答他的问题。
标签: asp.net objectdatasource formview