in

CodeSmith Community

Your Code. Your Way. Faster!

One thread to rule them all!

Last post 02-28-2007 5:13 AM by swin. 4 replies.
Page 1 of 1 (5 items)
Sort Posts: Previous Next
  • 02-27-2007 3:14 PM

    • Gonzo
    • Top 75 Contributor
    • Joined on 07-21-2006
    • Glasgow, Scotland
    • Posts 57
    • Points 1,480

    One thread to rule them all!

    Guys, I think i suggested this before, but ill ask again.

    I think it would be highly benifitial for current, and future nettiers users to have a forum on here that ONLY has code snippets and examples, so that we do not have to trawl through hundreds of pages in search results trying to find a solution to a common request.

    The reason that i am suggesting this is that i have moved from doing things in code behind to the typed data sources on front end view for my current project.

    Things i would personally like to see is working examples of many to many, returning single lookup values, filters, deeploading etc.

    Currently I am searching to find out how to return and display a value on front end based on key value:

    tableTitles
    PKID
    Name

    on front ent view all i want to do is say something like, take my TitleID value from the current users profile, and display its name value on screen - and i can not find any examples of how to do this.

    I hope to see this in place in the near future - as i feel it would be highly benifitial to everyone.

    David Lawton

    David Lawton
    Hyperion Technologies Ltd (UK)
    • Post Points: 35
  • 02-28-2007 2:46 AM In reply to

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

    Re: One thread to rule them all!

    David,

    A "proper" wiki is coming soon and so I expect that'll become the best place for example code.

    With regards to your coding query, if I understand you correctly you just want to display the "Name" of your Title in a label then you would probably be best to just do this in code-behind i.e.

    Title myTitle = DataRepository.TitleProvider.GetByTilteId( profile.TitleId );
    myTitleLabel.Text = myTitle.Name;

    If you're looking to do this declaratively then as the Label control doesn't support a datasource you have to wrap it in one that does i.e. FormView, Repeater, GridView etc etc and then link it to a datassource that has a selectmethod of  "GetyTitleId", specifying a parameter to pick up your inoput value - which may be overkill for something like this. 
     

    hth

    swin

    PS. Did you see my reply to your PM? 

    -------------------------------------------------
    Member of the .NetTiers team
    -------------------------------------------------
    • Post Points: 5
  • 02-28-2007 3:31 AM In reply to

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

    Re: One thread to rule them all!

    David,

    I just had a further thought, in that you could use the new EntityDropDownList that I added to the templates last week.  By setting the control to readonly the DropDownList is actually just rendered as a label...Big Smile

    So it would look something like this...

    My value=<data:EntityDropDownList runat="server" ID="ddl" DataSourceID="titleDS" DataTextField="Name" DataValueField="TitleId" ReadOnly="true" />
            <data:TitleDataSource runat="server" ID="titleDS" SelectMethod="GetByTitleId" >
            <Parameters>
                <data:CustomParameter Name="TitleId" DefaultValue="2" Type="Int32" />
            </Parameters>
            </data:TitleDataSource>


    Obviously you'd have to replace the CustomParameter with a ProfileParameter (I've not tried that but I can't see why it wouldn't work).

    hth

    swin 

    -------------------------------------------------
    Member of the .NetTiers team
    -------------------------------------------------
    • Post Points: 35
  • 02-28-2007 4:48 AM In reply to

    • Gonzo
    • Top 75 Contributor
    • Joined on 07-21-2006
    • Glasgow, Scotland
    • Posts 57
    • Points 1,480

    Re: One thread to rule them all!

    Boooooo Yaaaa!!!!

     

    Cheers Swin!

    David Lawton
    Hyperion Technologies Ltd (UK)
    • Post Points: 35
  • 02-28-2007 5:13 AM In reply to

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

    Re: One thread to rule them all!

    You're welcome!

    Actually it got the old grey matter working overtime and I thought it might be quite easy to create a proper control to do this.... and it was.

    Below is the code for the EntityLabel - I haven't got time at the mo to add it to the templates proper (maybe someone else will oblige??Wink).

    It works just like the EntityDropDownList but is designed to just work with a datasource that returns a single item. I hope its useful.

    /// <summary>
        /// Specialised DataBoundControl that renders as a Label
        /// <remarks>
        /// DataSource should return 1 row
        /// </remarks>
        /// </summary>
        public class EntityLabel : DataBoundControl
        {
            #region Properties
            /// <summary>
            /// Gets or sets the data value field.
            /// </summary>
            /// <value>The data value field.</value>
            public virtual string DataValueField
            {
                get
                {
                    object o = ViewState[ "DataValueField" ];
                    if ( o == null )
                    {
                        return string.Empty;
                    }
                    return (string)o;
                }
                set
                {
                    ViewState[ "DataValueField" ] = value;
                }
            }

            /// <summary>
            /// Gets or sets the data text field.
            /// </summary>
            /// <value>The data text field.</value>
            public virtual string DataTextField
            {
                get
                {
                    object o = ViewState[ "DataTextField" ];
                    if ( o == null )
                    {
                        return string.Empty;
                    }
                    return (string)o;
                }
                set
                {
                    ViewState[ "DataTextField" ] = value;
                }
            }

            /// <summary>
            /// Gets or sets the data text format string.
            /// </summary>
            /// <value>The data text format string.</value>
            public virtual string DataTextFormatString
            {
                get
                {
                    object o = ViewState[ "DataTextFormatString" ];
                    if ( o == null )
                    {
                        return string.Empty;
                    }
                    return (string)o;
                }
                set
                {
                    ViewState[ "DataTextFormatString" ] = value;
                }
            }

            /// <summary>
            /// Gets or sets the text.
            /// </summary>
            /// <value>The text.</value>
            public virtual string Text
            {
                get
                {
                    object o = ViewState[ "Text" ];
                    if ( o == null )
                    {
                        return string.Empty;
                    }
                    return (string)o;
                }
                set
                {
                    ViewState[ "Text" ] = value;
                }
            }

            /// <summary>
            /// Gets or sets the value.
            /// </summary>
            /// <value>The value.</value>
            public virtual string Value
            {
                get
                {
                    object o = ViewState[ "value" ];
                    if ( o == null )
                    {
                        return string.Empty;
                    }
                    return (string)o;
                }
                set
                {
                    ViewState[ "Value" ] = value;
                }
            }
            #endregion

            #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 ) )
                    {
                        this.Text = DataBinder.GetPropertyValue( e.Current, DataTextField ) as string;
                    }

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

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

            #region Override Control methods
            /// <summary>
            /// Renders the control to the specified HTML writer.
            /// </summary>
            /// <param name="writer">The <see cref="T:System.Web.UI.HtmlTextWriter"></see> object that receives the control content.</param>
            protected override void Render( HtmlTextWriter writer )
            {
                if ( !string.IsNullOrEmpty( this.DataTextFormatString ) )
                {
                    writer.Write( string.Format( this.DataTextFormatString, Text ) );
                }
                else
                {
                    writer.Write( Text );
                }
            }
            #endregion
        }

     

    hth

    swin 

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