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 someone
swin
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--------------------------------------------------------------
Hi Mike,Do you know what file?
Any file that is part of the patch (see the screen shot)
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?
I sent you PM
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
Fixed in SVN544.
Thanks
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; }}
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
In the previous message, you show:
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
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.
Nice. Thanks.
Out of curiosity, why do the checks for the placeholder strings? Why not just:
if