CodeSmith Community
Your Code. Your Way. Faster!

Use OnSelected to format data ?

Latest post 11-17-2006 4:28 PM by velum. 8 replies.
  • 11-15-2006 7:21 PM

    • velum
    • Top 25 Contributor
    • Joined on 07-14-2006
    • Montréal, Qc, Canada
    • Posts 189
    • Points 4,731

    Use OnSelected to format data ?

    Hi!

    I need to format data before it is displayed (Eg. Phone numbers). Is it possible to use the OnSelected event method of a Strongly Typed DataSource to achieve this? If so, is it clean to do so? And how do I access the data to format it? I know I could use the code behind of the page where the data is being displayed to do the formatting, but the same formatting will be used on different pages with various databound controls, so I thought it would be better to format it as close to the source as possible.

    By the way, the format string that can be provided to the Bind method is not enough for what I would like to do. 

    Cheers!

    JF
     

    • Post Points: 5
  • 11-15-2006 8:59 PM In reply to

    • velum
    • Top 25 Contributor
    • Joined on 07-14-2006
    • Montréal, Qc, Canada
    • Posts 189
    • Points 4,731

    Re: Use OnSelected to format data ?

    Reply |Contact |Answer

    Hi!

    I guess I answered my own question. By trial and error, I found out that I could use the e.ReturnValue of my OnSelected method. e being  of type ObjectDataSourceStatusEventArgs, and e.ReturnValue containing a TList of the selected entities.

    So, it is possible to do formatting here, but since my entity setters for the fields I would like to format remove the formatting, this is not a solution. Eg:

        myEntity.Phone = FormatPhone(myEntity.Phone);

    The above line formats the Phone Number, but then, the setter for myEntity.Phone removes the formatting. So, I guess I will have to introduce formatting in all web controls where fields requiring formatting is needed.

    JF


     

     

    • Post Points: 35
  • 11-16-2006 2:02 AM In reply to

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

    Re: Use OnSelected to format data ?

    velum,

    You could always add an extra ([Bindable]) property to your entity and do your formatting there.

    swin

    ------------------------------------------------- Member of the .NetTiers team -------------------------------------------------
    • Post Points: 35
  • 11-16-2006 8:54 AM In reply to

    • jcteague
    • Top 25 Contributor
    • Joined on 03-10-2005
    • Austin, Tx
    • Posts 442
    • Points 10,925

    Re: Use OnSelected to format data ?

    Do you want to retain the formatting in your getter and setters?  if so, override the original phone property to handle the formatting, then it would be formatted with every use. If you only need it in some situations, swin's approach would be better.

    On 11/16/06, swin <bounce-swin@codesmithsupport.com> wrote:

    velum,

    You could always add an extra ([Bindable]) property to your entity and do your formatting there.

    swin





    Thanks, John Teague ------------------------------ Member of the .NetTiers team http://www.nettiers.com ------------------------------

    • Post Points: 35
  • 11-16-2006 9:21 AM In reply to

    • velum
    • Top 25 Contributor
    • Joined on 07-14-2006
    • Montréal, Qc, Canada
    • Posts 189
    • Points 4,731

    Re: Use OnSelected to format data ?

    Hi John!

    I need the formatting in all situations, except one: When the data is being stored in the database. At first, I tried the follwoing:

            public override string TelephoneSF
            {
                get
                {
                    return Phone.formatPhoneForDisplay(base.TelephoneSF);
                }
                set
                {
                    base.TelephoneSF = Phone.formatPhoneForDB(value);
                }
            }

    However, this did not work, since the method that is saving (inserting/updating) the entity in the database is also using the getter, so the data was getting formatted for display when it was actually being stored in the database.  A major point that I had overseen.

    For now, I decided to remove the formatting in the setter, and to add in the OnDatabinding method of the TextBoxes where the data is being displayed. I will consider Swin's approach though if I realize that this data needs to be displayed on various Forms. 

    Cheers!

    JF 

    • Post Points: 35
  • 11-16-2006 9:29 AM In reply to

    Re: Use OnSelected to format data ?

    Hi,

    You would only format on the way out from your property since you do not want to save a formatted string in the DB.
    So, you would only have the format method in the getter portion of the property.



    On 11/16/06, velum <bounce-velum@codesmithsupport.com> wrote:

    Hi John!

    I need the formatting in all situations, except one: When the data is being stored in the database. At first, I tried the follwoing:

            public override string TelephoneSF
            {
                get
                {
                    return Phone.formatPhoneForDisplay(base.TelephoneSF);
                }
                set
                {
                    base.TelephoneSF = Phone.formatPhoneForDB(value);
                }
            }

    However, this did not work, since the method that is saving (inserting/updating) the entity in the database is also using the getter, so the data was getting formatted for display when it was actually being stored in the database.  A major point that I had overseen.

    For now, I decided to remove the formatting in the setter, and to add in the OnDatabinding method of the TextBoxes where the data is being displayed. I will consider Swin's approach though if I realize that this data needs to be displayed on various Forms. 

    Cheers!

    JF 






    Robert Hinojosa
    -------------------------------------
    Member of the Codesmith Tools, .netTiers, teams
    http://www.nettiers.com
    -------------------------------------
    • Post Points: 35
  • 11-16-2006 9:53 AM In reply to

    • velum
    • Top 25 Contributor
    • Joined on 07-14-2006
    • Montréal, Qc, Canada
    • Posts 189
    • Points 4,731

    Re: Use OnSelected to format data ?

    Hi Robert!

    I am not sure that I am followoing you. Right now, I have the follwing:

            public override string TelephoneSF
            {
                get
                {
                    return base.TelephoneSF;
                }
                set
                {
                    base.TelephoneSF = Phone.formatPhoneForDB(value);
                }
            }

     formatPhoneForDB() actually removes the formatting.

    In the ASPX page the property is two-way bound:

    <asp:TextBox ID="tbTelephoneSF" runat="server" Text='<%# Bind("TelephoneSF") %>' OnDataBinding="FormatTextBoxAsPhone" />

    So if instead I put the format method formatPhoneForDisplay() in the getter, the phone number will be formatted when it is stored to the database, since the function that is building the INSERT or the UPDATE query is using myEntity.TelephoneSF and thus it is using the getter to retrieve the information. This is what I had overseen the first time. Those getters and setters are really confusing sometimes.

    Cheers!

    JF
     

    • Post Points: 35
  • 11-16-2006 10:58 AM In reply to

    Re: Use OnSelected to format data ?

    Oh, Ok,  I guess I got a tad confused then. 

    On 11/16/06, velum <bounce-velum@codesmithsupport.com > wrote:

    Hi Robert!

    I am not sure that I am followoing you. Right now, I have the follwing:

            public override string TelephoneSF
            {
                get
                {
                    return base.TelephoneSF;
                }
                set
                {
                    base.TelephoneSF = Phone.formatPhoneForDB(value);
                }
            }

     formatPhoneForDB() actually removes the formatting.

    In the ASPX page the property is two-way bound:

    <asp:TextBox ID="tbTelephoneSF" runat="server" Text='<%# Bind("TelephoneSF") %>' OnDataBinding="FormatTextBoxAsPhone" />

    So if instead I put the format method formatPhoneForDisplay() in the getter, the phone number will be formatted when it is stored to the database, since the function that is building the INSERT or the UPDATE query is using myEntity.TelephoneSF and thus it is using the getter to retrieve the information. This is what I had overseen the first time. Those getters and setters are really confusing sometimes.

    Cheers!

    JF
     






    Robert Hinojosa
    -------------------------------------
    Member of the Codesmith Tools, .netTiers, teams
    http://www.nettiers.com
    -------------------------------------
    • Post Points: 35
  • 11-17-2006 4:28 PM In reply to

    • velum
    • Top 25 Contributor
    • Joined on 07-14-2006
    • Montréal, Qc, Canada
    • Posts 189
    • Points 4,731

    Re: Use OnSelected to format data ?

    Do you think it would be possible to overload the Eval() and Bind() methods (as in <%# Eval("CustomerName") %> and <%# Bind("CustomerName") %>) ? These two methods take a format string as second parameter, but I would like to add Eval() and Bind() methods with an IFormatProvider as third parameter. This would be a good way to provide Custom Formatting to DataBinding.

    Happy Friday!

    JF
     

    • Post Points: 5
Page 1 of 1 (9 items) | RSS
Copyright © 2008 CodeSmith Tools, LLC
Powered by Community Server (Commercial Edition), by Telligent Systems