Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > BOOK: Beginning ASP.NET 4 : in C# and VB
| Search | Today's Posts | Mark Forums Read
BOOK: Beginning ASP.NET 4 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4: in C# and VB by Imar Spaanjaars; ISBN: 9780470502211
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4 : in C# and VB section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old January 16th, 2012, 10:13 PM
Authorized User
Points: 173, Level: 3
Points: 173, Level: 3 Points: 173, Level: 3 Points: 173, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2011
Posts: 39
Thanks: 9
Thanked 0 Times in 0 Posts
Default How to bind a control to entitydatasource that uses code behind to bind data?

I have a grid that takes LINQ code behind to load up the data on page load and I created a dropdown and tried to bind it to the grid like in chapter 14 on p.521 but it doesn't seem to work. I can easily do it using SQL datasources but I am really digging the Entity Framework.

Here is my code. I am using a Telerik grid at the moment but it shouldn't matter much right? If it does, I could try it in a listview or gridview. (I only need it to be read-only)
I do have autopostback enabled.

HTML Code:
  <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" 
    CellSpacing="0" GridLines="None">
    <MasterTableView DataKeyNames="ID">
      <Columns>
        <telerik:GridBoundColumn DataField="Winner" DataType="System.Int16" 
          FilterControlAltText="Filter Winner column" HeaderText="Winner" 
          SortExpression="Winner" UniqueName="Winner">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="Loser" DataType="System.Int16" 
          FilterControlAltText="Filter Loser column" HeaderText="Loser" 
          SortExpression="Loser" UniqueName="Loser">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="Result" 
          FilterControlAltText="Filter Result column" HeaderText="Result" 
          SortExpression="Result" UniqueName="Result">
        </telerik:GridBoundColumn>
         <telerik:GridTemplateColumn HeaderText="FOTN" UniqueName="FOTN" SortExpression="FOTN">
              <ItemTemplate>
                <asp:Image ID="Image1" ImageUrl='<%# Bool.GetImgPath( Eval("FOTN") ) %>' AlternateText="FOTN result" ImageAlign="Middle" runat="server"  />
              </ItemTemplate>
            </telerik:GridTemplateColumn>    
            <telerik:GridTemplateColumn HeaderText="KOTN" UniqueName="KOTN" SortExpression="KOTN">
              <ItemTemplate>
                <asp:Image ID="Image2" ImageUrl='<%# Bool.GetImgPath( Eval("KOTN") ) %>' AlternateText="KOTN result" ImageAlign="Middle" runat="server"  />
              </ItemTemplate>
            </telerik:GridTemplateColumn>    
            <telerik:GridTemplateColumn HeaderText="SOTN" UniqueName="SOTN" SortExpression="SOTN">
              <ItemTemplate>
                <asp:Image ID="Image3" ImageUrl='<%# Bool.GetImgPath( Eval("SOTN") ) %>' AlternateText="SOTN result" ImageAlign="Middle"  runat="server" />
              </ItemTemplate>
            </telerik:GridTemplateColumn>    
            <telerik:GridTemplateColumn HeaderText="TitleFight" UniqueName="TitleFight" SortExpression="TitleFight">
              <ItemTemplate>
                <asp:Image ID="Image4" ImageUrl='<%# Bool.GetImgPath( Eval("TitleFight") ) %>' AlternateText="TitleFight result" ImageAlign="Middle"  runat="server" />
              </ItemTemplate>
            </telerik:GridTemplateColumn>    
        <telerik:GridBoundColumn DataField="Event" DataType="System.Int16" 
          FilterControlAltText="Filter Event column" HeaderText="Event" 
          SortExpression="Event" UniqueName="Event">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="WeightClass" DataType="System.Int16" 
          FilterControlAltText="Filter WeightClass column" HeaderText="WeightClass" 
          SortExpression="WeightClass" UniqueName="WeightClass">
        </telerik:GridBoundColumn>
      </Columns>
    </MasterTableView>
  </telerik:RadGrid>

  <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
    DataSourceID="EntityDataSource2" DataTextField="Class" DataValueField="ID">
  </asp:DropDownList>
  
  <asp:EntityDataSource ID="EntityDataSource2" runat="server" 
    ConnectionString="name=UFCEntities" DefaultContainerName="UFCEntities" 
    EnableFlattening="False" EntitySetName="WEIGHTs" Select="it.[ID], it.[Class]">
  </asp:EntityDataSource>
  
  <asp:EntityDataSource ID="EntityDataSource1" runat="server" 
    ConnectionString="name=UFCEntities" DefaultContainerName="UFCEntities" 
    EnableFlattening="False" EntitySetName="FIGHTs" 
    AutoGenerateWhereClause="True" EntityTypeFilter="" Select="" Where="">
    <WhereParameters>
      <asp:ControlParameter ControlID="DropDownList1" 
        Name="WeightClass" PropertyName="SelectedValue" Type="Int32" />
    </WhereParameters>
  </asp:EntityDataSource>
Code:
Code Behind:
using (UFCEntities myEntity = new UFCEntities())
{
       var myFights = from a in myEntity.FIGHTs
                     // gets all the objects I need from inner joins
                     join b in myEntity.FIGHTERs on a.Winner equals b.ID
                     join c in myEntity.FIGHTERs on a.Loser equals c.ID
                     join d in myEntity.EVENTs on a.EventID equals d.ID
                     join f in myEntity.WEIGHTs on a.WeightClass equals f.ID
                     // query the objects and by setting them to variables
                     select new
                       {
                        ID = a.ID,
                        Winner = b.Name,
                        Loser = c.Name,
                        Result = a.Result,
                        FOTN = a.FOTN,
                        KOTN = a.KOTN,
                        SOTN = a.SOTN,
                        TitleFight = a.TitleFight,
                        Event = d.Name,
                        WeightClass = f.Class
                      };

      RadGrid1.DataSource = myFights;
      RadGrid1.DataBind();
}

Last edited by IceThatJaw; January 17th, 2012 at 03:08 PM..


Similar Threads
Thread Thread Starter Forum Replies Last Post
ListView EditItemTemplate doesn't bind data in declarative code muybn ASP.NET 3.5 Basics 2 September 16th, 2010 10:09 PM
How to Bind Two Variables to LinkButton Control cyberstore05 .NET Framework 2.0 1 October 31st, 2008 02:50 AM
Bind Data to a Single Label Control: Possible? jumpseatnews ASP.NET 2.0 Basics 1 April 6th, 2006 11:12 PM
Bind records from the table to a datalist control macupryk General .NET 1 October 13th, 2004 11:59 AM
How can I make an bind control that accepts update Franklin66 Beginning VB 6 1 December 10th, 2003 09:04 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.