locked
ObjectDataSource on FormView RRS feed

  • Question

  • User1888482730 posted

    I have a formview that uses object datasource, and EditItemTemplate or both Insert/Upate

                <tr> 
                    <td class="tdShort"><asp:Label ID="LastName" ClientIDMode="Static" runat="server" AssociatedControlID="LastNametxt">Last name:</asp:Label></td>
                    <td class="Normal"><asp:TextBox ID="LastNametxt" Text='<%# Bind("LastName") %>'  ClientIDMode="Static" runat="server" CssClass="textMedium"></asp:TextBox></td>
                </tr>
               <tr>  
                    <td class="tdShort"><asp:Label ID="lblFirstName" AssociatedControlID="FirstNametxt" ClientIDMode="Static" runat="server" >First Name:</asp:Label></td>
                    <td class="Normal"><asp:TextBox ID="FirstNametxt" Text='<%# Bind("FirstName") %>'  ClientIDMode="Static" runat="server" CssClass="textMedium"></asp:TextBox>&nbsp;&nbsp;Initials:&nbsp;<asp:TextBox ID="txtInitials" runat="server" CssClass="textShort" /></td>
                </tr>   

    And Params for insert examples

            <InsertParameters>           
                <asp:Parameter Name="IdNumber" Type="Int64" ConvertEmptyStringToNull="true" />
                <asp:Parameter Name="TitleId" Type="Int32" ConvertEmptyStringToNull="true" />        
                <asp:Parameter Name="LastName" Type="String" />
                <asp:Parameter Name="FirstName" Type="String" />
                <asp:Parameter Name="Initials" Type="String" ConvertEmptyStringToNull="true" />
                <asp:Parameter Name="Gender" Type="String" ConvertEmptyStringToNull="true" />         
            </InsertParameters>

    It calls this

           [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Insert, true)]
            public virtual int CreateNewMember(MemberFormView mfv) {
    
                if (mfv == null)
                    throw (new ArgumentNullException("new member"));
    
                return mfv.MemberId;
            }


    mfv has empty values..... ObjectDataSource or FormView is not submitting info from my editItemTemples
    I have been truobleshooting for hours BUT NO luck

    Any help please on what am I missing ?


    Monday, November 19, 2012 7:33 AM

Answers

  • User1888482730 posted

    I finally managed to make it work.

    Thanks a lot for your help.

    Problem was Page.DataBind(); that I was calling on Page_Load event of Site.Master.cs , I called it cause I am using ResolveUrl on my aspx of Site.Master. So it was preventing bindings on child pages.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 21, 2012 12:07 AM

All replies

  • User3866881 posted

    Hello,

    Would you mind showing us your detailled codes including:

    1)Your aspx codes of FormView and ObjectDataSource's defination?

    2)Your cs codes defination?

    3)Your MemberFormView's defination?

    Monday, November 19, 2012 9:10 PM
  • User1888482730 posted

    Even the testing one that I created doesnt pass data from Templates to the receiver object

    ODT

    namespace Test.Core.Domain
    {
        public class TestDetails
        {
            public virtual int TestId { get; set; }
            public virtual string FirstName { get; set; }
            public virtual string LastName { get; set; }
            public virtual string Title { get; set; }
        }
    }

    Type name used by ObjectDataSource

    using Test.Core.Domain;
    
    namespace Test.Services.Common
    {
        public class TestService
        {
            public TestService() { }
    
            public virtual TestDetails getTest(int TestId)
            {
                return new TestDetails();
            }
    
            public virtual int AddTest(TestDetails data) {
                return new TestDetails { TestId = 3 }.TestId;
            }
    
            public virtual void UpdateTest(int TestId, string FirstName, string LastName, string Title) { 
            }
    
            public virtual void DeleteTest(int TestId)
            { 
            }
        }
    }

    ASPX page

    <%@ Page Title="" Language="C#" MasterPageFile="~/TwoColumns.master" AutoEventWireup="true" CodeBehind="Member-Info.aspx.cs" Inherits="Parlour.Cares.myPCares.Forms.Member.Member_Info" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
        <asp:FormView ID="fvMemberInfo" EnableViewState="true" runat="server" DataKeyNames="TestId" DataSourceID="odsMemberInfo">
            <EditItemTemplate>
                FirstName:
                <asp:TextBox ID="FirstNameTextBox" runat="server" 
                    Text='<%# Bind("FirstName") %>' />
                <br />
                LastName:
                <asp:TextBox ID="LastNameTextBox" runat="server" 
                    Text='<%# Bind("LastName") %>' />
                <br />
                Title:
                <asp:TextBox ID="TitleTextBox" runat="server" Text='<%# Bind("Title") %>' />
                <br />
                <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" 
                    CommandName="Update" Text="Update" />
                &nbsp;<asp:LinkButton ID="UpdateCancelButton" runat="server" 
                    CausesValidation="False" CommandName="Cancel" Text="Cancel" />
            </EditItemTemplate>
            <InsertItemTemplate>
                 FirstName:
                <asp:TextBox ID="FirstNameTextBox" runat="server" 
                    Text='<%# Bind("FirstName") %>' />
                <br />
                LastName:
                <asp:TextBox ID="LastNameTextBox" runat="server" 
                    Text='<%# Bind("LastName") %>' />
                <br />
                Title:
                <asp:TextBox ID="TitleTextBox" runat="server" Text='<%# Bind("Title") %>' />
                <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" />
            </InsertItemTemplate>
            <ItemTemplate>
                TestId:
                <asp:Label ID="TestIdLabel" runat="server" Text='<%# Bind("TestId") %>' />
                <br />
                FirstName:
                <asp:Label ID="FirstNameLabel" runat="server" Text='<%# Bind("FirstName") %>' />
                <br />
                LastName:
                <asp:Label ID="LastNameLabel" runat="server" Text='<%# Bind("LastName") %>' />
                <br />
                Title:
                <asp:Label ID="TitleLabel" runat="server" Text='<%# Bind("Title") %>' />
                <br />
    
            </ItemTemplate>
        
    </asp:FormView>
        <asp:ObjectDataSource ID="odsMemberInfo" runat="server" 
            DataObjectTypeName="Test.Core.Domain.TestDetails" 
            DeleteMethod="DeleteTest" InsertMethod="AddTest" 
            onselecting="odsMemberInfo_Selecting" ConvertNullToDBNull="true" SelectMethod="getTest" 
            TypeName="Test.Services.Common.TestService" UpdateMethod="UpdateTest">
            <DeleteParameters>
                <asp:Parameter Name="TestId" Type="Int32" />
            </DeleteParameters>
            <SelectParameters>
                <asp:RouteParameter Name="TestId" RouteKey="test_id" Type="Int32" />
            </SelectParameters>
            <UpdateParameters>
                <asp:Parameter Name="TestId" Type="Int32" />
                <asp:Parameter Name="FirstName" Type="String" />
                <asp:Parameter Name="LastName" Type="String" />
                <asp:Parameter Name="Title" Type="String" />
            </UpdateParameters>
            <InsertParameters>
                 <asp:Parameter Name="FirstName" Type="String" />
                <asp:Parameter Name="LastName" Type="String" />
                <asp:Parameter Name="Title" Type="String" />
            </InsertParameters>
        </asp:ObjectDataSource>
    </asp:Content>
    
    When clicking on insert link button/ asp:button, it pass null values, so what am I missing ?





    Tuesday, November 20, 2012 1:05 AM
  • User3866881 posted

    Hello again,

    I think there's something wrong with your Select statement, and you must assign DataKeyNames in the FormView:

    【aspx】

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="CSharp.WebForm1" %>
     
    <!DOCTYPE html>
     
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        <asp:FormView ID="fvMemberInfo" runat="server" DataSourceID="odsMemberInfo" DefaultMode="Insert" 
                OnItemInserted="fvMemberInfo_ItemInserted" DataKeyNames="TestId"> 
            <EditItemTemplate> 
                TestId: 
                <asp:TextBox ID="TestIdTextBox" runat="server"  
                    Text='<%# Bind("TestId"%>' /> 
                <br /> 
                FirstName: 
                <asp:TextBox ID="FirstNameTextBox" runat="server"  
                    Text='<%# Bind("FirstName"%>' /> 
                <br /> 
                LastName: 
                <asp:TextBox ID="LastNameTextBox" runat="server" 
                    Text='<%# Bind("LastName"%>' /> 
                <br /> 
                Title:
                <asp:TextBox ID="TitleTextBox" runat="server" Text='<%# Bind("Title"%>' />
                <br />
                <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True"  
                    CommandName="Update" Text="Update" /> 
                &nbsp;<asp:LinkButton ID="UpdateCancelButton" runat="server"  
                    CausesValidation="False" CommandName="Cancel" Text="Cancel" /> 
            </EditItemTemplate> 
            <InsertItemTemplate> 
                 FirstName: 
                <asp:TextBox ID="FirstNameTextBox" runat="server"  
                    Text='<%# Bind("FirstName"%>' /> 
                <br /> 
                 LastName: 
                <asp:TextBox ID="LastNameTextBox" runat="server" 
                     Text='<%# Bind("LastName"%>' /> 
                <br /> 
                 Title:
                 <asp:TextBox ID="TitleTextBox" runat="server" Text='<%# Bind("Title"%>' />
                 <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" /> 
            </InsertItemTemplate> 
            <ItemTemplate> 
                TestId: 
                <asp:Label ID="TestIdLabel" runat="server" Text='<%# Bind("TestId"%>' /> 
                <br /> 
                FirstName: 
                <asp:Label ID="FirstNameLabel" runat="server" Text='<%# Bind("FirstName"%>' /> 
                <br /> 
                LastName: 
                <asp:Label ID="LastNameLabel" runat="server" Text='<%# Bind("LastName"%>' /> 
                <br /> 
                Title: 
                <asp:Label ID="TitleLabel" runat="server" Text='<%# Bind("Title"%>' /> 
                <br /> 
     
                <asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" 
                    CommandName="Edit" Text="Edit" />
                &nbsp;<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" 
                    CommandName="Delete" Text="Del" />
                &nbsp;<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" 
                    CommandName="New" Text="New" />
     
            </ItemTemplate> 
         
    </asp:FormView> 
        <asp:ObjectDataSource ID="odsMemberInfo" runat="server"  
            DataObjectTypeName="Test.Core.Domain.TestDetails"  
            DeleteMethod="DeleteTest" InsertMethod="AddTest"  ConvertNullToDBNull="True" SelectMethod="getAllTest"  
            TypeName="Test.Services.Common.TestService" UpdateMethod="UpdateTest"> 
            <DeleteParameters> 
                <asp:Parameter Name="TestId" Type="Int32" /> 
            </DeleteParameters> 
            <UpdateParameters> 
                <asp:Parameter Name="TestId" Type="Int32" /> 
                <asp:Parameter Name="FirstName" Type="String" /> 
                <asp:Parameter Name="LastName" Type="String" /> 
                <asp:Parameter Name="Title" Type="String" /> 
            </UpdateParameters> 
            <InsertParameters> 
                 <asp:Parameter Name="FirstName" Type="String" /> 
                <asp:Parameter Name="LastName" Type="String" /> 
                <asp:Parameter Name="Title" Type="String" /> 
            </InsertParameters> 
     
        </asp:ObjectDataSource> 
     
        </div>
        </form>
    </body>
    </html>
    

    【logic codes】

    namespace Test.Services.Common
    {
        public class TestService
        {
            static List<TestDetails> tds = new List<TestDetails>();
     
            public TestService() { }
     
            public virtual List<TestDetails> getAllTest()
            {
                return tds;
            }
     
            [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Insert, true)] 
            public virtual int AddTest(TestDetails data)
            {
                data.TestId = 1;
                tds.Add(data);
                return 1;
            }
     
            public virtual void UpdateTest(int TestId, string FirstName, string LastName, string Title)
            {
            }
     
            public virtual void DeleteTest(int TestId)
            {
            }
        }
    }

    【model】

    namespace Test.Core.Domain
    {
        public class TestDetails
        {
            public virtual int TestId { getset; }
            public virtual string FirstName { getset; }
            public virtual string LastName { getset; }
            public virtual string Title { getset; }
        }
    }
    Tuesday, November 20, 2012 2:11 AM
  • User1888482730 posted

    I do have DatakeyName

    <asp:FormView ID="fvMemberInfo" EnableViewState="true" runat="server" DataKeyNames="TestId" DataSourceID="odsMemberInfo">


    And had this on my aspx.cs

    public partial class Member_Info : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            protected void odsMemberInfo_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
            {
                string editMode = Page.RouteData.Values["action_mode"] as string;
    
                if (editMode == "view")
                    fvMemberInfo.ChangeMode(FormViewMode.ReadOnly);
                else if (editMode == "edit")
                    fvMemberInfo.ChangeMode(FormViewMode.Edit);
                else if (editMode == "new")
                    fvMemberInfo.ChangeMode(FormViewMode.Insert);
                else
                    throw new InvalidOperationException("error");
    
    
                if (fvMemberInfo.CurrentMode == FormViewMode.Insert)
                    e.Cancel = true;
            }        
        }

    Is your sample working ? I will try it now and see. I will let you know in few minutes

    Thanks for your help this far

    Tuesday, November 20, 2012 2:21 AM
  • User1888482730 posted

    This is for getting a specific record to edit, so now which methos on ODS should I put it to ?

    [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, false)]
            public virtual TestDetails getTest(int TestId)
            {
                return new TestDetails();
            }



    Tuesday, November 20, 2012 2:25 AM
  • User3866881 posted

    Would you mind trying my sample first?

    It's OK on me.

    Tuesday, November 20, 2012 2:26 AM
  • User1888482730 posted

    I have added the getAllTest method on selectMethos of ODS ......BUT still its not passing data to AddTest method

    What so you have on this : fvMemberInfo_ItemInserted ?? in your sample code ?

    Tuesday, November 20, 2012 2:36 AM
  • User3866881 posted

    What so you have on this : fvMemberInfo_ItemInserted ?? in your sample code ?

    That's just a test only. I've deleted that.

    Have you checked my codes with yours?

    Please check carefully——especially your Select method.

    Tuesday, November 20, 2012 2:38 AM
  • User3866881 posted

    If you still cannot solve ur problem, plz leave your email and I'll send a demo for you.

    Please make sure that you are using VS2012.

    Tuesday, November 20, 2012 2:40 AM
  • User1888482730 posted

    Man, still NO luck............ I dont know now... !I still get nulls, I mean I am puttting Firstnam, lastname, title on the fields, and hit insert link button, but nothing get posted

    And I am using yuor code sample. I am using VS 2010 Premium

    bhekister@gmail.com

    Tuesday, November 20, 2012 2:50 AM
  • User3866881 posted

    Would you mind using hotmail, my mail has been junked by your google mail:(

    Tuesday, November 20, 2012 2:53 AM
  • User1888482730 posted

    If I use the sinngle page like in your sample code it does pass data

    BUT -  as I am using a page in a Master page it does not work!!

    Why is that ?

    bheki.nzuza@hotmail.com

    Tuesday, November 20, 2012 7:01 AM
  • User3866881 posted

    BUT -  as I am using a page in a Master page it does not work!!

    Well……What do you mean by Master page?How did you code?Can you show us your aspx codes?

    Tuesday, November 20, 2012 8:07 PM
  • User1888482730 posted

    I finally managed to make it work.

    Thanks a lot for your help.

    Problem was Page.DataBind(); that I was calling on Page_Load event of Site.Master.cs , I called it cause I am using ResolveUrl on my aspx of Site.Master. So it was preventing bindings on child pages.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 21, 2012 12:07 AM