FormView动态模板不会在插入/更新时发送表单值

时间:2013-02-27 14:05:25

标签: asp.net objectdatasource formview

当我在FormView的动态加载模板上单击“插入”按钮时出现以下错误。默认表单模式设置为insert。我确实看到插入模板加载正常。我有TypeName =“WebApplication1.Repository.AccountRepo”和DataObjectTypeName =“WebApplication1.Model.Account”来更正类型。有人可以帮忙。

我得到的错误是 ObjectDataSource'ObjectDataSource1'没有要插入的值。检查'values'字典是否包含值。

    <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&amp;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>

在页面init方法中使用代码

    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" />
    &nbsp;
    <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();
        }

    }

1 个答案:

答案 0 :(得分:0)

在此页面上找到解决方案。做作者的建议并且有效。 http://www.developerfusion.com/code/4721/dynamically-loading-an-ibindabletemplate/