CodeSmith Community
Your Code. Your Way. Faster!

New EntityLabel control

rated by 0 users
This post has 13 Replies | 2 Followers

Top 10 Contributor
Posts 925
Points 35,460
swin Posted: 03-08-2007 4:07 PM

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 -------------------------------------------------
Top 10 Contributor
Posts 742
Points 17,965

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
Top 10 Contributor
Posts 925
Points 35,460

Hi Mike,

Do you know what file?

swin 

------------------------------------------------- Member of the .NetTiers team -------------------------------------------------
  • | Post Points: 35
Top 10 Contributor
Posts 742
Points 17,965

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
Top 10 Contributor
Posts 925
Points 35,460

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
Top 10 Contributor
Posts 742
Points 17,965

swin,

I sent you PM 

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

  • | Post Points: 5
Top 10 Contributor
Posts 742
Points 17,965
Ok. The EntityLabel has been commited to SVN (rev. 515)

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

  • | Post Points: 5
Not Ranked
Posts 5
Points 145

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
Top 10 Contributor
Posts 925
Points 35,460

Fixed in SVN544.

Thanks

swin 

------------------------------------------------- Member of the .NetTiers team -------------------------------------------------
  • | Post Points: 5
Not Ranked
Posts 2
Points 40

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;
  }
}

 

Top 10 Contributor
Posts 925
Points 35,460

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
Top 100 Contributor
Posts 46
Points 1,450

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
Top 10 Contributor
Posts 925
Points 35,460

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
Top 100 Contributor
Posts 46
Points 1,450

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