Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4.5 > ASP.NET 4.5 General Discussion
|
ASP.NET 4.5 General Discussion For ASP.NET 4.5 discussions not relating to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 4.5 General Discussion 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
 
Old April 26th, 2013, 08:56 AM
Registered User
 
Join Date: Mar 2013
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Assign variable in Listview from code behind

Hi,

I'm developing user page where he can view his uploaded files. I'm using LINQ and ADO.NET entity framework.

In the aspx page I have Listview control and Entity data source. My ListView looks like:

HTML Code:
 <asp:ListView ID="ListView1" runat="server" DataKeyNames="Id" DataSourceID="EntityDataSource1" ItemType="MyWebsiteModel.Image">
        <ItemTemplate>
          <li>
            Upload date:
            <asp:Label ID="CreateDateTimeLabel" runat="server" Text='<%# Item.CreateDateTime %>' />
            <br />
            Category:
            <asp:Label ID="CategoryLabel" runat="server" Text='<%# CategoryName %>' />
            <br />
            <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="Delete" />
          </li>
        </ItemTemplate>
      </asp:ListView>
For example in code behind I'm having a code:

Code:
using (MyWebsiteEntities1 Entities = new MyWebsiteEntities1())
      {
        MyWebsiteModel.Image myImage = (MyWebsiteModel.Image)e.Entity;

        var Name = from category in Entities.Categories
                 where category.Id == myImage.CategoryId
                 select category.Name;
        string CategoryName = Name.Single();
      }
So as you see in code behind I have variable CategoryName. How can I show the value of this variable on this label:

<asp:Label ID="CategoryLabel" runat="server" Text='<%# CategoryName %>' />
 
Old April 26th, 2014, 11:57 AM
Friend of Wrox
 
Join Date: Feb 2014
Posts: 136
Thanks: 1
Thanked 10 Times in 10 Posts
Default

first you need to fix your binding expressions


Code:
 
<asp:ListView ID="ListView1" runat="server" DataKeyNames="Id" DataSourceID="EntityDataSource1" ItemType="MyWebsiteModel.Image">
   <ItemTemplate>
      <li>
         Upload date:
         <asp:Label ID="CreateDateTimeLabel" runat="server" Text='<%# Eval("CreateDateTime") %>' />
         <br />
         Category:
         <asp:Label ID="CategoryLabel" runat="server" Text='<%# Eval("CategoryName") %>' />
         <br />
         <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="Delete" />
      </li>
   </ItemTemplate>
</asp:ListView>

now what is your behind code, where is it located... in page_load... where exactly? what was its intended purpose? Not sure what it is for since you have a data source attahced to the listview. data sources takes care of the handling most operation if configured correctly.

Last edited by mmorgan30; April 26th, 2014 at 12:11 PM..





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to assign value to a xslt variable XSLUSer XSLT 1 March 25th, 2009 09:10 AM
assign XSLT variable to a wml variable arunagottimukkala XSLT 2 October 19th, 2007 05:15 AM
assign javascript variable to asp variable manjunath_c_k Classic ASP Basics 1 September 14th, 2006 07:35 AM
assign a recordset to a variable biggen55 VB How-To 1 March 26th, 2004 02:26 PM





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