CodeSmith Community
Your Code. Your Way. Faster!

New EntityLabel control

Latest post 09-19-2007 7:31 PM by donig. 13 replies.
  • 03-08-2007 4:07 PM

    • swin
    • Top 10 Contributor
    • Joined on 06-14-2006
    • London, UK
    • Posts 924
    • Points 34,750

    New EntityLabel control

    This was inspired by this thread... http://community.codesmithtools.com/forums/thread/22638.aspx

    The EntityLabel is a simple control that allows you to bind a single result from a datasource to a Label.  If the datasource contains > 1 row then only the first is used.

    Of course it would be simple enough to do this in code behind, but sometimes its nice just to define it declaratively.

    <data:EntityLabel runat="server" id=myEntityLabel" DataSourceId="myDataSource" DataTextField="MyTextField" DataValueField="MyValueField"  DataTextFormatString="My text value={0}"/>

    Hey, not earth shattering I know, but it maybe useful to someoneHuh?

    swin

    ------------------------------------------------- Member of the .NetTiers team -------------------------------------------------
    Filed under:
    • Post Points: 95
  • 03-09-2007 11:16 AM In reply to

    • mike123
    • Top 10 Contributor
    • Joined on 02-25-2005
    • Toronto, Ontario
    • Posts 734
    • Points 17,040

    Re: New EntityLabel control

    swin,

    I was getting errors trying to apply your patch, saying it couldn't find file specified 

     

    Mike Shatny
    --------------------------------------------------------------
    Member of the .netTiers team http://www.nettiers.com
    --------------------------------------------------------------

    • Post Points: 35
  • 03-09-2007 11:18 AM In reply to

    • swin
    • Top 10 Contributor
    • Joined on 06-14-2006
    • London, UK
    • Posts 924
    • Points 34,750

    Re: New EntityLabel control

    Hi Mike,

    Do you know what file?

    swin 

    ------------------------------------------------- Member of the .NetTiers team -------------------------------------------------
    • Post Points: 35
  • 03-09-2007 1:12 PM In reply to

    • mike123
    • Top 10 Contributor
    • Joined on 02-25-2005
    • Toronto, Ontario
    • Posts 734
    • Points 17,040

    Re: New EntityLabel control

    swin,

    Any file that is part of the patch (see the screen shot) 

     


    Mike Shatny
    --------------------------------------------------------------
    Member of the .netTiers team http://www.nettiers.com
    --------------------------------------------------------------

    • Post Points: 35
  • 03-09-2007 1:37 PM In reply to

    • swin
    • Top 10 Contributor
    • Joined on 06-14-2006
    • London, UK
    • Posts 924
    • Points 34,750

    Re: New EntityLabel control

    Hmmm, I dunno.  I created this patch the same way as I did the others - although it was from a different pc.

    What do you think I should do?

    swin

    ------------------------------------------------- Member of the .NetTiers team -------------------------------------------------
    • Post Points: 35
  • 03-09-2007 2:57 PM In reply to

    • mike123
    • Top 10 Contributor
    • Joined on 02-25-2005
    • Toronto, Ontario
    • Posts 734
    • Points 17,040

    Re: New EntityLabel control

    swin,

    I sent you PM 

    Mike Shatny
    --------------------------------------------------------------
    Member of the .netTiers team http://www.nettiers.com
    --------------------------------------------------------------

    • Post Points: 5
  • 03-09-2007 8:11 PM In reply to

    • mike123
    • Top 10 Contributor
    • Joined on 02-25-2005
    • Toronto, Ontario
    • Posts 734
    • Points 17,040

    Re: New EntityLabel control

    Ok. The EntityLabel has been commited to SVN (rev. 515)

    Mike Shatny
    --------------------------------------------------------------
    Member of the .netTiers team http://www.nettiers.com
    --------------------------------------------------------------

    • Post Points: 5
  • 04-24-2007 9:10 AM In reply to

    • dudleya
    • Not Ranked
    • Joined on 04-17-2007
    • Posts 5
    • Points 145

    EntityLabel Observation

    When the DataTextField or DataValueField is set to a non-string property, the label contains an empty Text/Value.  I've modified the PerformDataBinding method in my local version of EntityLabel.cs to use the ToString() method instead of the as operator.  Thoughts anyone?

     
            #region Override Data methods
            /// <summary>
            /// When overridden in a derived class, binds data from the data source to the control.
            /// </summary>
            /// <param name="data">The <see cref="T:System.Collections.IEnumerable"></see> list of data returned from a <see cref="M:System.Web.UI.WebControls.DataBoundControl.PerformSelect"></see> method call.</param>
            protected override void PerformDataBinding( System.Collections.IEnumerable data )
            {
                if( data == null )
                {
                    return;
                }

                // get the next data item
                IEnumerator e = data.GetEnumerator();
                while( e.MoveNext() )
                {
                    // get the text item if specified
                    if( !string.IsNullOrEmpty( DataTextField ) )
                    {
                        //Returns null if property object was not a string
                        //this.Text = DataBinder.GetPropertyValue( e.Current, DataTextField ) as string;
                        this.Text = DataBinder.GetPropertyValue( e.Current, DataTextField ).ToString();
                    }

                    // get the value item if specified
                    if( !string.IsNullOrEmpty( DataValueField ) )
                    {
                        //Returns null if property object was not a string
                        //this.Text = DataBinder.GetPropertyValue( e.Current, DataValueField ) as string;
                        this.Text = DataBinder.GetPropertyValue( e.Current, DataValueField ).ToString();
                    }

                    // we only do the first item
                    break;
                }
            }
            #endregion

    • Post Points: 35
  • 04-27-2007 10:36 AM In reply to

    • swin
    • Top 10 Contributor
    • Joined on 06-14-2006
    • London, UK
    • Posts 924
    • Points 34,750

    Re: EntityLabel Observation

    Fixed in SVN544.

    Thanks

    swin 

    ------------------------------------------------- Member of the .NetTiers team -------------------------------------------------
    • Post Points: 5
  • 06-29-2007 2:38 PM In reply to

    • krishnaM
    • Not Ranked
    • Joined on 04-30-2007
    • Posts 2
    • Points 40

    When you specify both DataTextField & DataValueField the label gets the contents of the value field...

    Hi,

    First. Thanks a lot for this 1nderful work. Time saver. Love it.
    I think i found a bug (not sure, if it was addressed earlier or in a separate thread. I could be wrong also...)

    Problem:
    When you specify both the DataTextField and the DataValueField the label ends up getting the contents of the value field.

    (Cant seem to upload images, Admin please send me an email so that i can send an image to you)

    Solution:
    I think a break is needed inside the first if statement.

    Can some gurus comment please.
    Thanks once again

    Int the .aspx page or .ascx   control if you have this...

    <data:EntityLabel
     ID="label1"
     runat="server"
     DataSourceID="SomeDataSource"
     DataTextField="somefieldName"
     DataValueField="somefieldId"
     DataTextFormatString="My text value={0}">
    </data:EntityLabel>

     Then the EntityLabel.cs has this...

    /// <summary>
    /// When overridden in a derived class, binds data from the data source to the control.
    /// </summary>
    /// <param name="data">The <see cref="T:System.Collections.IEnumerable"></see> list of data returned from a <see cref="M:System.Web.UI.WebControls.DataBoundControl.PerformSelect"></see> method call.</param>
    protected override void PerformDataBinding(System.Collections.IEnumerable data)
    {
      if (data == null)
      {
     return;
      }

      // get the next data item
      IEnumerator e = data.GetEnumerator();
      while (e.MoveNext())
      {
     // get the text item if specified
     if (!string.IsNullOrEmpty(DataTextField))
     {
       this.Text = DataBinder.GetPropertyValue(e.Current, DataTextField).ToString();

      // I think we need a break; here
     }

     // get the value item if specified
     if (!string.IsNullOrEmpty(DataValueField))
     {
       this.Text = DataBinder.GetPropertyValue(e.Current, DataValueField).ToString();
     }

     // we only do the first item
     break;
      }
    }

     

    Filed under:
    • Post Points: 35
  • 08-19-2007 1:34 PM In reply to

    • swin
    • Top 10 Contributor
    • Joined on 06-14-2006
    • London, UK
    • Posts 924
    • Points 34,750

    Re: When you specify both DataTextField & DataValueField the label gets the contents of the value field...

    I've updated the EntityLabel so that it uses the Text and Value fields seperately and now also allows you to specify an output format eg:

    <data:EntityLabel runat="server" ID="l1" DataSourceID="pds" DataTextField="ProductName"
                    DataValueField="UnitPrice" DataTextFormatString="Product={0} - Price={1}"/>
    <data:ProductsDataSource runat="server" ID="pds" SelectMethod="GetAll" />

    Updated in SVN613.

    hth

    swin

     

    ------------------------------------------------- Member of the .NetTiers team -------------------------------------------------
    • Post Points: 35
  • 09-18-2007 7:00 PM In reply to

    • donig
    • Top 100 Contributor
    • Joined on 02-16-2004
    • Virginia, USA
    • Posts 41
    • Points 1,230

    Is there any way to specify a format for the entity label?

    In the previous message, you show:

     <data:EntityLabel runat="server" ID="l1" DataSourceID="pds" DataTextField="ProductName"
                    DataValueField="UnitPrice" DataTextFormatString="Product={0} - Price={1}"/>
    <data:ProductsDataSource runat="server" ID="pds" SelectMethod="GetAll" />

     It looks nice, but then I tried to format the price as currency:

    DataTextFormatString="Product={0} - Price={1:C}"

    and it doesn't work...

    Looking at the EntityLabel source, the Render method assumes both DataTextField and DataValueField are strings. Is it possible to make it strongly typed to the field it binds to, so it could format DateTimes, numbers, etc? Or am I missing something?

     Thanks,

    Doni

    ------------------------------------------------- Member of the .NetTiers team -------------------------------------------------
    • Post Points: 35
  • 09-19-2007 10:07 AM In reply to

    • swin
    • Top 10 Contributor
    • Joined on 06-14-2006
    • London, UK
    • Posts 924
    • Points 34,750

    Re: Is there any way to specify a format for the entity label?

    Doni,

    I've updated the control (SVN638) so that it now supports objects of any type and you should be able to use more complex formatting string that just the basic placeholders.

    hth

    swin 

    ------------------------------------------------- Member of the .NetTiers team -------------------------------------------------
    • Post Points: 35
  • 09-19-2007 7:31 PM In reply to

    • donig
    • Top 100 Contributor
    • Joined on 02-16-2004
    • Virginia, USA
    • Posts 41
    • Points 1,230

    Re: Is there any way to specify a format for the entity label?

    Nice. Thanks.

    Out of curiosity, why do the checks for the placeholder strings? Why not just:

    if ( !string.IsNullOrEmpty( this.DataTextFormatString )) {
            writer.Write( string.Format( this.DataTextFormatString, Text, Value ) );
    }

    Doni

    ------------------------------------------------- Member of the .NetTiers team -------------------------------------------------
    • Post Points: 5
Page 1 of 1 (14 items) | RSS
Copyright © 2008 CodeSmith Tools, LLC
Powered by Community Server (Commercial Edition), by Telligent Systems