locked
Is it possible to use Dynamic data without a datasource control? RRS feed

  • Question

  • User2142095193 posted

    I dont want to use LinqDataSource or EntityDataSource or ObjectDataSource or xyzDataSource.  I just want to bind my controls using methods of my business logic layer i.e.:

    gridview1.datasource = Employees.Select(e => e.City == "Los Angeles");

    gridview1.databind;

    When I try something like the above I get:

    You need to have a DynamicManager control on the page and register your data control with it in order to use a DynamicQueryStringParameter.

    Of course I have a DynamicDataManager control and I'm not using a DynamicQueryStringParameter that I know of.

     

    Friday, February 13, 2009 6:24 PM

Answers

  • User-330204900 posted

    DD Requires a xDataSource to work, the nearest thing I have seen would be the BusinessLogicDataSource you will need to look at the ASP.Net 4.0 Dynamic Data Preview 2 on codeplex to get some Idea if that would be a good compromise.

    Incidentaly the BusinessLogicDataSource does not work with DD yet but will in the future.

    There are some prerequisites for DD to work

    1. A compatible DataContext
    2. A compatible DataSource

    So if you want to bypass any of the above you are stuck at the moment.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, February 14, 2009 6:17 AM

All replies

  • User-330204900 posted

    If all you want to do is filter your data comming in the you should use the:

    GridDataSource.WhereParameters.Add(new Parameter("CreatedBy", DbType.String, User.Identity.Name));

    If you want to shape the data i.e. filter which columns are returned the you can't as they will not match an entity in th edata model, that is also the reason you get that "not so helpfull error" trying to filter without using the LinqDataSource as the DynamicDataManager looks at the LinqDataSource (or EntityDataSource) for the entity type to sort the UI out.

    I think that's about right, if I'm wrong maybe one of the team will correct me [:D]

    Friday, February 13, 2009 7:27 PM
  • User2142095193 posted

     All I want to do is use my business logic layer as I have in the past. I've never used an xDataSource in the past, I just bind to my BLL as I described in my orignal post.  In trying out the new L2E/L2S/DD features I thought I would see if any of the new DataSource controls would work with a repository style BLL.  Apparently not, but thats OK, now I just want to get back to basics.  I'll give up the DataSource controls -  I dont want to give up DD as I have 150+  pages ahead of me and DD might save me weeks of time if I can make it work.  I dont want to scatter my business logic all over the place.   Below is an example of my standard issue BLL - I dont want to give that up or bastardize it. 

    The page below (which is not complete) is the current thorn in my side.  The ItemUpdating and ItemInserting events dont fire.

     

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Data.Linq;
    using System.Linq.Expressions;
    /// <summary>
    /// Summary description for Employees
    /// </summary>
    public class Employees 
    {
        public static bool Insert(ref Employee empl, ErrorHandler errorHandler)
        {
            CafeDataContext db = new CafeDataContext();
            empl.Signature = Security.Signature();
            db.Employees.InsertOnSubmit(empl);
    
            try
            {
                db.SubmitChanges();
            }
            catch (Exception ex)
            {
                errorHandler("An error occured while adding an employee record.  The error message is: " + ex.Message);
            }
            return (empl.EmployeeID > 0);
        }
    
        public static bool Update(ref Employee empl, ErrorHandler errorHandler)
        {
            CafeDataContext db = new CafeDataContext();
            bool success = true;
            empl.Signature = Security.Signature();
    
            try
            {
                db.Employees.Attach(empl);
                db.Refresh(RefreshMode.KeepCurrentValues, empl);
                db.SubmitChanges();
            }
            catch (Exception ex)
            {
                errorHandler("An error occured while updating an employee record.  The error message is: " + ex.Message);
                success = false;
            }
            return (success);
        }
    
        public static bool Delete(int id, ErrorHandler errorHandler)
        {
            CafeDataContext db = new CafeDataContext();
            bool success = true;
            
            try
            {
                db.Employees.DeleteOnSubmit(Employees.SelectByID(id).FirstOrDefault());
            }
            catch (Exception ex)
            {
                errorHandler("An error occured while deleting an employee record.  The error message is: " + ex.Message);
                success = false;
            }
            return (success);
        }
    
        public static IQueryable<employee> Select()
        {
            CafeDataContext db = new CafeDataContext();
            return from p in db.Employees select p;
        }
    
        public static IQueryable<employee> Select(Expression<func><employee,>bool>> expr)
        {
            CafeDataContext db = new CafeDataContext();
            return from p in db.Employees.Where(expr) select p;
        }
    
    
        public static IQueryable<employee> SelectByID(int id)
        {
            CafeDataContext db = new CafeDataContext();
            return (from e in db.Employees where e.EmployeeID == id select e);
        }
    }
    </employee></employee,></func></employee></employee>

      

     

     

    <%@ Page Language="C#" MasterPageFile="~/Site.master" CodeFile="Edit.aspx.cs" Inherits="Edit" %>
    
    
    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
        <asp:DynamicDataManager ID="DynamicDataManager1" runat="server" AutoLoadForeignKeys="true" />
        <center>
        <asp:ScriptManagerProxy runat="server" ID="ScriptManagerProxy1" />
    
        <asp:ValidationSummary ID="ValidationSummary1" runat="server" EnableClientScript="true"
            HeaderText="List of validation errors" />
        <asp:DynamicValidator runat="server" ID="DetailsViewValidator" ControlToValidate="DetailsView1" Display="None" />
    
        <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="false" GridLines="None"
            AutoGenerateEditButton="True" AutoGenerateInsertButton="true" 
            OnItemCommand="DetailsView1_ItemCommand" OnItemUpdated="DetailsView1_ItemUpdated"
            CssClass="DataEntryForm" FieldHeaderStyle-CssClass="FieldCaption" CommandRowStyle-HorizontalAlign="Center"
                OnItemInserting="DetailsView1_ItemInserting" OnItemUpdating="DetailsView1_ItemUpdating" 
            >
            
            <Fields>
                <asp:DynamicField DataField="FirstName" HeaderText="First Name:"  />
                <asp:DynamicField DataField="LastName" HeaderText="Last Name:"  />
                <asp:DynamicField DataField="PlantLocation" HeaderText="Location:"  />
                <asp:DynamicField DataField="EmployeeDepartment" HeaderText="Department:"  />
                <asp:DynamicField DataField="Supervisor" HeaderText="Supervisor:"  />
                <asp:DynamicField DataField="Email" HeaderText="Email:"  />
                <asp:DynamicField DataField="OfficePhone" HeaderText="Office Phone:"  />
                <asp:DynamicField DataField="CellPhone" HeaderText="Cell Phone:"  />
                <asp:DynamicField DataField="UserName" HeaderText="User Name:"  />
                <asp:DynamicField DataField="Password" HeaderText="Password:"  />
                <asp:DynamicField DataField="EmployeePermissions" HeaderText="Permissions:"  />
                <asp:DynamicField DataField="LaborRateType" HeaderText="Labor Rate Type:"  />
                <asp:DynamicField DataField="BillRate" HeaderText="Bill Rate:"  />
                <asp:DynamicField DataField="OTBillRate" HeaderText="OT Bill Rate:"  />
                <asp:DynamicField DataField="LastLogin" HeaderText="Last Login:" />
                <asp:DynamicField DataField="Active" HeaderText="Active:"  />
            </Fields>
        </asp:DetailsView>
    
    <%--    <asp:LinqDataSource ID="DetailsDataSource" runat="server" EnableUpdate="true" 
                oninserted="DetailsDataSource_Inserted" EnableInsert="True" 
                oninserting="DetailsDataSource_Inserting" 
                onselecting="DetailsDataSource_Selecting" 
                onupdating="DetailsDataSource_Updating" 
                onupdated="DetailsDataSource_Updated">
            <WhereParameters>
                <asp:DynamicQueryStringParameter />
            </WhereParameters>
        </asp:LinqDataSource>
            
            <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
                 TypeName="Employees" SelectMethod="SelectById"
                oninserting="ObjectDataSource1_Inserting" 
                >
                <SelectParameters >
                 <asp:Parameter Name="EmployeeID" Type="Int32" /> 
                </SelectParameters>
            </asp:ObjectDataSource>
    --%>        
            
            
    </center>
    </asp:Content>
    

      

     

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Xml.Linq;
    using System.Web.DynamicData;
    
    public partial class Edit : System.Web.UI.Page {
        
        protected MetaTable table;
    
        protected void Page_Init(object sender, EventArgs e) 
        {
            DynamicDataManager1.RegisterControl(DetailsView1);
            table = MetaModel.Default.GetTable(typeof(Employee));
        }
    
        protected void Page_Load(object sender, EventArgs e) {
            
            if (!IsPostBack)
            {
                if (Request.QueryString.ToString().Contains("=0"))
                    DetailsView1.DefaultMode = DetailsViewMode.Insert;
                else
                    DetailsView1.DefaultMode = DetailsViewMode.Edit;
    
                BindData();    
            }
            
        }
    
        protected void BindData()
        {
            int id = Convert.ToInt32(Request.QueryString["EmployeeID"]);
            DetailsView1.DataSource = Employees.Select(e => e.EmployeeID == id);
            DetailsView1.DataBind();
        }
    
        protected void DisplayErrorMsg(string errorMsg)
        { 
        
        }
    
        protected void DetailsView1_ItemCommand(object sender, DetailsViewCommandEventArgs e) {
            
            if (e.CommandName == DataControlCommands.CancelCommandName) {
                Response.Redirect(table.ListActionPath);
            }
        }
    
        protected void DetailsView1_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e) {
        
        }
    
        protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
        {
            Employee empl = GetDataValues(e.Values);
            
        }
    
        protected void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)  {
            Employee empl = GetDataValues(e.NewValues);
        }
    
        protected Employee GetDataValues(System.Collections.Specialized.IOrderedDictionary values)  {
            Employee empl = new Employee();
            empl.FirstName = values["FirstName"].ToString();
            empl.LastName = values["LastName"].ToString();
            empl.PlantLocationID = Convert.ToInt32(values["PlantLocationID"]);
            //empl.EmployeeDepartmentID = values[""].ToString();
            //empl.Active = values[""].ToString();
            //empl.BillRate = values[""].ToString();
            //empl.OTBillRate = values[""].ToString();
            //empl.LaborRateTypeID = values[""].ToString();
            //empl.Supervisor = values[""].ToString();
            //empl.Email = values[""].ToString();
            //empl.OfficePhone = values[""].ToString();
            //empl.CellPhone = values[""].ToString();
            //empl.UserName = values[""].ToString();
            //empl.Password = values[""].ToString();
            return empl;
        }
    }
    
      

     

     

    Friday, February 13, 2009 8:41 PM
  • User-330204900 posted

    DD Requires a xDataSource to work, the nearest thing I have seen would be the BusinessLogicDataSource you will need to look at the ASP.Net 4.0 Dynamic Data Preview 2 on codeplex to get some Idea if that would be a good compromise.

    Incidentaly the BusinessLogicDataSource does not work with DD yet but will in the future.

    There are some prerequisites for DD to work

    1. A compatible DataContext
    2. A compatible DataSource

    So if you want to bypass any of the above you are stuck at the moment.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, February 14, 2009 6:17 AM
  • User-843003697 posted

    Try:

     

    using System.Web.DynamicData;
    //...
    protected void BindData()
    {
    int id = Convert.ToInt32(Request.QueryString["EmployeeID"]);
    DetailsView1.SetMetaTable(table);
    DetailsView1.DataSource = Employees.Select(e => e.EmployeeID == id);
    DetailsView1.DataBind();

    Monday, February 21, 2011 6:03 AM