Datagrid, formatting, carriage returns

Discussion in 'ASP .Net Web Controls' started by Zamdrist, Dec 15, 2005.

  1. Zamdrist

    Zamdrist Guest

    It appears as though, in the transition from a query to the datagrid I
    am losing the carriage returns in an address field which I would like
    to maintain for the purposes of a Word merge.

    Here's the stored procedure. I have to Cast the data from text to
    varchar in order to replace the @@ characters at the beginning of the
    address field. But even if I don't do that and not cast it, I still
    lose the carriage returns. Ideas? Thanks.

    CREATE Procedure sp_UTL_MGAttorneyGroupMerge

    @sAttorney Varchar(50),
    @sGroup Varchar(50)

    As

    Select ContactName=Case FullName When '' Then CompName Else FullName
    End,
    Replace(Replace(Cast(AddrLines As Varchar(500)),'@@',''),'
    ',Char(10)+Char(13)) As AddressLine

    >From Contacts


    Inner Join ContactCategories ContactAtty On ContactAtty.Contacts =
    Contacts.Contacts
    Inner Join ContactCategories ContactGroup On ContactGroup.Contacts =
    Contacts.Contacts
    Where ContactAtty.Category = @sAttorney And ContactGroup.Category =
    @sGroup
    Order By Contacts.LastName
    Zamdrist, Dec 15, 2005
    #1
    1. Advertising

  2. Zamdrist

    addup Guest

    I'm not sure i understand your problem, but

    Use View|Source to look at the HTML rendered, are your carriage returns
    there?
    The UA (web browser) generally ignores all whitespace

    have you tried HTML encoding your address ?
    addup, Dec 16, 2005
    #2
    1. Advertising

  3. Zamdrist

    Zamdrist Guest

    The datasource (SQL Server) holds an address field of a data type of
    text. Therein this field are carriage returns at the appropriate places
    for an address field.

    When bringing in this data to a datagrid in ASP.Net, those carriage
    returns are gone and the data in that field is now just one long
    string.

    I was going to then allow the user to export the datagrid to Excel for
    the purposes of a mergein MS Word...I know it's a round about way, but
    the users are more familar with Excel than database queries. But the
    lose of the carriage returns in a wrench in the plan, it wouldn't
    format correctly.

    In any event, I'm not rendering the HTML in any particular way. It
    doesn't appear in the susequent HTML source the carriage returns are
    'gone'...and I wouldn't know how to HTML encode the data.

    It's the transition from the stored procedure to the datagrid is where
    I lose the carriage returns.

    Thanks
    Zamdrist, Dec 16, 2005
    #3
  4. Zamdrist

    addup Guest

    That's what i suspected.

    HTML normally does not care about carriage returns (or line feeds,
    tabs, spaces etc. collectively known as 'whitespace' )

    The Databind faithfully renderers your carriage returns into HTML, and
    it's the browser that ignores them

    The simplest way (with the least impact on existing code) is probably
    to do
    1. Add the following style code to the top of your page (In the
    <HEAD></HEAD> section

    <STYLE>.WhiteSpaced {white-space:pre;}</STYLE>

    2. Add the following to your datagrid definition
    ItemStyle-CssClass="WhiteSpaced"

    --

    Another way to get the browser to display line breaks is to use the
    HTML <BR/> tag

    I'm not sure of your SP code, but try

    Replace(Replace(Cast(AddrLines As Varchar(500)),'@@',''),'
    ', '<BR/>' ) As AddressLine

    to get SQL server to use '<BR/>' instead of the x0A0D cr/lf

    --
    These are just tow of the MANY ways you could do this :)
    Hope this helps.

    PS: There's nothing wrong with using excel's HTML import features!
    addup, Dec 19, 2005
    #4
  5. Zamdrist

    Zamdrist Guest

    Well I got results, of sorts, with replacing Char(13)+Char(10) with
    '<BR/>' and it looks right on the resulting web page & datagrid. When
    exporting to Excel however, it unfortunately puts each line of the
    address in a row by itself...when has the unwanted side-effect of when
    you merge into Word...each address line is seen as a record of it's
    own...not good

    I'm actually using both Whitespace style as you suggested, and added
    the replacement in the stored procedure when when the WhiteSpace didn't
    seen to do anything at all.

    I certainly appreciate your help! Thanks.
    Zamdrist, Dec 27, 2005
    #5
  6. Zamdrist

    addup Guest

    my bad - The white-space:pre; needs to be applied to the individual
    table cells (TDs) and not the entire table

    change the
    <STYLE>.WhiteSpaced {white-space:pre;}</STYLE>
    to
    <STYLE>.WhiteSpaced, TABLE.WhiteSpaced TD {white-space:pre;}</STYLE>

    and go back to the Char(13)+Char(10)
    This should show the html OK ... I'm not a 100% about the excel import
    (it'll also depend on your EXCEL version)

    --- having said that ---

    You seem to have two separate requirements (presentation and excel -->
    word mailmerge)

    Since your excel needs seem to be largely unformatted, I suggest using
    a simple CSV (comma delimited) format.

    1. Leave the "pretty" HTML/datagrid display intact
    2. Add an "Export to excel" button on your page and generate a csv file
    with "address line 1", "Address Line 2" columns in it (serve this with
    an excel mime type)

    Just my .02
    Hope this helps
    addup, Dec 28, 2005
    #6
  7. Zamdrist

    Zamdrist Guest

    Thanks addup...my first try with this syntax didn't yield the results
    I'm looking for. I event set the <asp:Boundcolumn
    ItemStyle-CssClass...> to WhiteSpaced, and that had no apprent effect.

    Ultimately all that is important is that the merge contain the carriage
    returns so that the form letters format properly or labels for
    instance.

    I appreciate your help and if you have any more ideas that would be
    great, but don't feel overly obligated! :) Thanks
    Zamdrist, Dec 28, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Iceberg

    Carriage Returns and sockets

    Iceberg, Sep 6, 2003, in forum: Perl
    Replies:
    1
    Views:
    1,148
    Iceberg
    Sep 6, 2003
  2. Schroeder
    Replies:
    1
    Views:
    534
    Jim Gibson
    Jan 26, 2005
  3. Mario Vargas

    Replacing Carriage Returns in VB.NET

    Mario Vargas, Oct 21, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    14,501
    Dragos Marian Barbu
    Nov 10, 2003
  4. Doc Wally
    Replies:
    1
    Views:
    119
    Ray at
    Oct 10, 2003
  5. Steve Anderson
    Replies:
    3
    Views:
    251
    Steve Anderson
    Jun 21, 2004
Loading...

Share This Page