从SQL数据源渲染HTML中的换行符

时间:2014-02-06 20:19:51

标签: asp.net sql sql-server datasource

我将一列的多行作为一个单行和一列返回:

SELECT STUFF((
                SELECT '<br/>' + Notes 
                FROM DailyTaskHours
                WHERE Notes IS NOT NULL
                    AND Notes <> ''
                    AND NonScrumStoryId = DTH.NonScrumStoryId
                FOR XML PATH('')
                ), 1, 1, '')
                )

我想在ASP.NET中连接的每一行之间插入换行符,但我的方法不起作用:

enter image description here

正如您所见,正在用数据库引擎替换&lt;br /&gt;如何在数据库中获取换行符,以便在网页上呈现间隔?

以下是我正在处理的整个控件供参考:

        <asp:GridView CssClass="hoursGrid" ID="hoursReportGridView" runat="server" AutoGenerateColumns="False" BackColor="#DEBA84" BorderColor="#DEBA84"
            BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" DataSourceID="SqlDataSource2" OnRowDataBound="hoursReportGridView_OnRowDataBound" DataKeyNames="DifferentUsers, DoubleBookedFlag, PointPerson, Person">
            <Columns>
                <asp:BoundField DataField="Person" HeaderText="Person" SortExpression="Project" />
                <asp:BoundField DataField="Project" HeaderText="Project" SortExpression="Project" />
                <asp:BoundField DataField="ProjectType" HeaderText="Project Type" ReadOnly="True" SortExpression="Sprint" ItemStyle-HorizontalAlign="Center" />
                <asp:BoundField DataField="Theme" HeaderText="Theme" ReadOnly="True" SortExpression="Theme" ItemStyle-HorizontalAlign="Center" />
                <asp:BoundField DataField="StoryNumber" HeaderText="Story Number" SortExpression="Story" ItemStyle-Width="6%" ItemStyle-HorizontalAlign="Center" />
                <asp:BoundField DataField="StoryTitle" HeaderText="Story Title" SortExpression="Story" ItemStyle-Width="20%" />
                <asp:BoundField DataField="Effort" HeaderText="Effort" SortExpression="Effort" ItemStyle-HorizontalAlign="Right" />
                <asp:BoundField DataField="Task" HeaderText="Task" SortExpression="Task"  ItemStyle-Width="20%" />
                <asp:BoundField DataField="OriginalEstimateHours" HeaderText="Original Estimate" SortExpression="OriginalEstimateHours" ItemStyle-HorizontalAlign="Right" />
                <asp:BoundField DataField="Monday" HeaderText="Mon" ReadOnly="True" SortExpression="Monday" ItemStyle-HorizontalAlign="Right" />
                <asp:BoundField DataField="Tuesday" HeaderText="Tues" ReadOnly="True" SortExpression="Tuesday" ItemStyle-HorizontalAlign="Right" />
                <asp:BoundField DataField="Wednesday" HeaderText="Wed" ReadOnly="True" SortExpression="Wednesday" ItemStyle-HorizontalAlign="Right" />
                <asp:BoundField DataField="Thursday" HeaderText="Thurs" ReadOnly="True" SortExpression="Thursday" ItemStyle-HorizontalAlign="Right" />
                <asp:BoundField DataField="Friday" HeaderText="Fri" ReadOnly="True" SortExpression="Friday" ItemStyle-HorizontalAlign="Right" />
                <asp:BoundField DataField="Saturday" HeaderText="Sat" ReadOnly="True" SortExpression="Saturday" ItemStyle-HorizontalAlign="Right" />
                <asp:BoundField DataField="Sunday" HeaderText="Sun" ReadOnly="True" SortExpression="Sunday" ItemStyle-HorizontalAlign="Right" />
                <asp:TemplateField HeaderText="Total" ItemStyle-HorizontalAlign="Right">
                <ItemTemplate>
                     <asp:LinkButton ID="taskLinkButton" Text='<%# Eval("Total") %>' Enabled='<%# Eval("StoryTitle").ToString() != "" %>' runat="server" OnClick="taskLinkButton_Click" />
                </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
            <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
            <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
            <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
            <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#FFF1D4" />
            <SortedAscendingHeaderStyle BackColor="#B95C30" />
            <SortedDescendingCellStyle BackColor="#F1E5CE" />
            <SortedDescendingHeaderStyle BackColor="#93451F" />
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>" 
            SelectCommand="
SELECT RowType AS RowType
    ,Person AS Person
    ,Project AS Project
    ,ProjectType AS ProjectType
    ,Theme AS Theme
    ,StoryNumber AS StoryNumber
    ,StoryTitle AS StoryTitle
    ,Effort AS Effort
    ,Task AS Task
    ,OriginalEstimateHours AS OriginalEstimateHours
    ,MondayHours AS Monday
    ,TuesdayHours AS Tuesday
    ,WednesdayHours AS Wednesday
    ,ThursdayHours AS Thursday
    ,FridayHours AS Friday
    ,SaturdayHours AS Saturday
    ,SundayHours AS Sunday
    ,TotalHours AS Total
    ,DifferentUsers AS DifferentUsers
    ,DoubleBookedFlag AS DoubleBookedFlag
    ,PointPerson AS PointPerson
FROM (
    -- DATE DISPLAY
    SELECT '1' AS RowType
        ,'' AS Person
        ,'' AS Project
        ,'Category' AS ProjectType
        ,'' AS Theme
        ,'Ticket #' AS StoryNumber
        ,'' AS StoryTitle
        ,'' AS Effort
        ,'' AS Task
        ,'' AS OriginalEstimateHours
        ,'' AS Category
        ,'' AS IncidentNumber
        ,'' AS ApplicationName
        ,(
            CASE 
                WHEN DATEDIFF(d, @startDateParam, @endDateParam) >= 7
                    THEN ''
                WHEN DATEDIFF(d, @startDateParam, @endDateParam) <= 5
                    THEN ''
                WHEN DATEPART(dw, DATEADD(DAY, 0, @startDateParam)) = 2
                    THEN CONVERT(VARCHAR(5), DATEADD(DAY, 0, @startDateParam), 101)
                ELSE ''
                END
            ) AS MondayHours
        ,(
            CASE 
                WHEN DATEDIFF(d, @startDateParam, @endDateParam) >= 7
                    THEN ''
                WHEN DATEDIFF(d, @startDateParam, @endDateParam) <= 5
                    THEN ''
                WHEN DATEPART(dw, DATEADD(DAY, 1, @startDateParam)) = 3
                    THEN CONVERT(VARCHAR(5), DATEADD(DAY, 1, @startDateParam), 101)
                ELSE ''
                END
            ) AS TuesdayHours
        ,(
            CASE 
                WHEN DATEDIFF(d, @startDateParam, @endDateParam) >= 7
                    THEN ''
                WHEN DATEDIFF(d, @startDateParam, @endDateParam) <= 5
                    THEN ''
                WHEN DATEPART(dw, DATEADD(DAY, 2, @startDateParam)) = 4
                    THEN CONVERT(VARCHAR(5), DATEADD(DAY, 2, @startDateParam), 101)
                ELSE ''
                END
            ) AS WednesdayHours
        ,(
            CASE 
                WHEN DATEDIFF(d, @startDateParam, @endDateParam) >= 7
                    THEN ''
                WHEN DATEDIFF(d, @startDateParam, @endDateParam) <= 5
                    THEN ''
                WHEN DATEPART(dw, DATEADD(DAY, 3, @startDateParam)) = 5
                    THEN CONVERT(VARCHAR(5), DATEADD(DAY, 3, @startDateParam), 101)
                ELSE ''
                END
            ) AS ThursdayHours
        ,(
            CASE 
                WHEN DATEDIFF(d, @startDateParam, @endDateParam) >= 7
                    THEN ''
                WHEN DATEDIFF(d, @startDateParam, @endDateParam) <= 5
                    THEN ''
                WHEN DATEPART(dw, DATEADD(DAY, 4, @startDateParam)) = 6
                    THEN CONVERT(VARCHAR(5), DATEADD(DAY, 4, @startDateParam), 101)
                ELSE ''
                END
            ) AS FridayHours
        ,(
            CASE 
                WHEN DATEDIFF(d, @startDateParam, @endDateParam) >= 7
                    THEN ''
                WHEN DATEDIFF(d, @startDateParam, @endDateParam) <= 5
                    THEN ''
                WHEN DATEPART(dw, DATEADD(DAY, 5, @startDateParam)) = 7
                    THEN CONVERT(VARCHAR(5), DATEADD(DAY, 5, @startDateParam), 101)
                ELSE ''
                END
            ) AS SaturdayHours
        ,(
            CASE 
                WHEN DATEDIFF(d, @startDateParam, @endDateParam) >= 7
                    THEN ''
                WHEN DATEDIFF(d, @startDateParam, @endDateParam) <= 5
                    THEN ''
                WHEN DATEPART(dw, DATEADD(DAY, 6, @startDateParam)) = 1
                    THEN CONVERT(VARCHAR(5), DATEADD(DAY, 6, @startDateParam), 101)
                ELSE ''
                END
            ) AS SundayHours
        ,'' AS TotalHours
        ,'' AS DifferentUsers
        ,'' AS DoubleBookedFlag
        ,'' AS PointPerson
    --

    UNION ALL

    --
    -- GRAND TOTALS
    --
    SELECT '2' AS RowType
        ,'All Personnel' AS Person
        ,'' AS Project
        ,'' AS ProjectType
        ,'' AS Theme
        ,'' AS StoryNumber
        ,'' AS StoryTitle
        ,'' AS Effort
        ,'Total:' AS Task
        ,'' AS OriginalEstimateHours
        ,'' AS Category
        ,'' AS IncidentNumber
        ,'' AS ApplicationName
        ,CAST(SUM(CASE 
                    WHEN DATEPART(dw, DTH.ActivityDate) = 2
                        THEN DTH.[Hours]
                    ELSE 0
                    END) AS VARCHAR(20)) AS Monday
        ,CAST(SUM(CASE 
                    WHEN DATEPART(dw, DTH.ActivityDate) = 3
                        THEN DTH.[Hours]
                    ELSE 0
                    END) AS VARCHAR(20)) AS Tuesday
        ,CAST(SUM(CASE 
                    WHEN DATEPART(dw, DTH.ActivityDate) = 4
                        THEN DTH.[Hours]
                    ELSE 0
                    END) AS VARCHAR(20)) AS Wednesday
        ,CAST(SUM(CASE 
                    WHEN DATEPART(dw, DTH.ActivityDate) = 5
                        THEN DTH.[Hours]
                    ELSE 0
                    END) AS VARCHAR(20)) AS Thursday
        ,CAST(SUM(CASE 
                    WHEN DATEPART(dw, DTH.ActivityDate) = 6
                        THEN DTH.[Hours]
                    ELSE 0
                    END) AS VARCHAR(20)) AS Friday
        ,CAST(SUM(CASE 
                    WHEN DATEPART(dw, DTH.ActivityDate) = 7
                        THEN DTH.[Hours]
                    ELSE 0
                    END) AS VARCHAR(20)) AS Saturday
        ,CAST(SUM(CASE 
                    WHEN DATEPART(dw, DTH.ActivityDate) = 1
                        THEN DTH.[Hours]
                    ELSE 0
                    END) AS VARCHAR(20)) AS Sunday
        ,CAST(SUM(DTH.[Hours]) AS VARCHAR(20)) AS Total
        ,'' AS DifferentUsers
        ,'' AS DoubleBookedFlag
        ,'' AS PointPerson
    FROM DailyTaskHours DTH
    LEFT JOIN NonScrumStory NSS ON DTH.NonScrumStoryId = NSS.PK_NonScrumStory
    LEFT JOIN Task TSK ON DTH.TaskId = TSK.PK_Task
    LEFT JOIN Story STY ON TSK.StoryId = STY.PK_Story
    LEFT JOIN Product PDT ON STY.ProductId = PDT.PK_Product
    LEFT JOIN [User] USR ON DTH.EnteredBy = USR.DisplayName
    WHERE DTH.EnteredBy LIKE @userParam
        AND ActivityDate >= @startDateParam
        AND ActivityDate <= @endDateParam
        AND 1 = CASE ISNUMERIC(@productId)
            WHEN 0
                THEN CASE 
                        WHEN DTH.TaskId IS NULL
                            OR PDT.PK_Product LIKE @productId
                            THEN 1
                        END
            WHEN 1
                THEN CASE 
                        WHEN DTH.TaskId IS NOT NULL
                            AND PDT.PK_Product = @productId
                            THEN 1
                        END
            END
        AND (
            (
                @orgTeamPK = '%'
                AND (
                    USR.[OrganizationalTeamId] LIKE @orgTeamPK
                    OR USR.[OrganizationalTeamId] IS NULL
                    )
                )
            OR (
                @orgTeamPK <> '%'
                AND (USR.[OrganizationalTeamId] LIKE @orgTeamPK)
                )
            )
        AND (
            (
                STY.Number LIKE @search
                OR STY.Number IS NULL
                )
            OR (
                STY.Title LIKE @search
                OR STY.Title IS NULL
                )
            OR (
                TSK.NAME LIKE @search
                OR TSK.NAME IS NULL
                )
            )
        AND (
            (
                @theme = '%'
                AND (
                    dbo.primaryTheme(STY.[Number]) LIKE @theme
                    OR dbo.primaryTheme(STY.[Number]) IS NULL
                    )
                )
            OR (
                @theme != '%'
                AND dbo.primaryTheme(STY.[Number]) = @theme
                )
            )

    UNION ALL

    --
    -- Details by PERSON, PROJECT, SPRINT, STORY, TASK
    --
    SELECT '3' AS RowType
        ,DTH.EnteredBy AS Person
        ,COALESCE(PDT.[Name], APP.AppName) AS Project
        ,(
            CASE 
                WHEN (
                        STY.KanBanProductId IS NOT NULL
                        AND STY.SprintId IS NULL
                        )
                    THEN 'Kanban'
                WHEN (
                        STY.KanBanProductId IS NULL
                        AND STY.SprintId IS NOT NULL
                        )
                    THEN 'Sprint'
                ELSE SCY.Catagory
                END
            ) AS ProjectType
        ,dbo.primaryTheme(STY.[Number]) AS Theme
        ,COALESCE(STY.[Number], NSS.IncidentNumber) AS StoryNumber
        ,COALESCE(STY.Title, NSS.[Description]) AS StoryTitle
        ,CONVERT(VARCHAR(20), STY.Effort) AS Effort
        ,COALESCE(TSK.[Name], (
                SELECT STUFF((
                            SELECT ' | ' + Notes 
                            FROM DailyTaskHours
                            WHERE Notes IS NOT NULL
                                AND Notes <> ''
                                AND NonScrumStoryId = DTH.NonScrumStoryId
                            FOR XML PATH('')
                            ), 1, 1, '')
                )) AS Task
        ,CONVERT(VARCHAR(20), TSK.OriginalEstimateHours) AS OriginalEstimateHours
        ,SCY.Catagory AS Category
        ,NSS.IncidentNumber AS IncidentNumber
        ,APP.AppName AS ApplicationName
        ,CAST(SUM(CASE 
                    WHEN DATEPART(dw, DTH.ActivityDate) = 2
                        THEN DTH.[Hours]
                    ELSE 0
                    END) AS VARCHAR(20)) AS MondayHours
        ,CAST(SUM(CASE 
                    WHEN DATEPART(dw, DTH.ActivityDate) = 3
                        THEN DTH.[Hours]
                    ELSE 0
                    END) AS VARCHAR(20)) AS TuesdayHours
        ,CAST(SUM(CASE 
                    WHEN DATEPART(dw, DTH.ActivityDate) = 4
                        THEN DTH.[Hours]
                    ELSE 0
                    END) AS VARCHAR(20)) AS WednesdayHours
        ,CAST(SUM(CASE 
                    WHEN DATEPART(dw, DTH.ActivityDate) = 5
                        THEN DTH.[Hours]
                    ELSE 0
                    END) AS VARCHAR(20)) AS ThursdayHours
        ,CAST(SUM(CASE 
                    WHEN DATEPART(dw, DTH.ActivityDate) = 6
                        THEN DTH.[Hours]
                    ELSE 0
                    END) AS VARCHAR(20)) AS FridayHours
        ,CAST(SUM(CASE 
                    WHEN DATEPART(dw, DTH.ActivityDate) = 7
                        THEN DTH.[Hours]
                    ELSE 0
                    END) AS VARCHAR(20)) AS SaturdayHours
        ,CAST(SUM(CASE 
                    WHEN DATEPART(dw, DTH.ActivityDate) = 1
                        THEN DTH.[Hours]
                    ELSE 0
                    END) AS VARCHAR(20)) AS SundayHours
        ,CAST(SUM(DTH.[Hours]) AS VARCHAR(20)) AS TotalHours
        ,CAST(SUM(CASE 
                    WHEN DTH.Hours > 0
                        THEN DTH.[UserDifference]
                    END) AS VARCHAR(20)) AS DifferentUsers
        ,CAST(SUM(CASE 
                    WHEN DTH.Hours > 0
                        THEN DTH.DoubleBookedFlag
                    END) AS VARCHAR(20)) AS DoubleBookedFlag
        ,DTH.PointPerson AS PointPerson
    FROM DailyTaskHours DTH
    LEFT JOIN Task TSK ON DTH.TaskId = TSK.PK_Task
    LEFT JOIN Story STY ON TSK.StoryId = STY.PK_Story
    LEFT JOIN NonScrumStory NSS ON DTH.NonScrumStoryId = NSS.PK_NonScrumStory
    LEFT JOIN SupportCatagory SCY ON NSS.CatagoryId = SCY.PK_SupportCatagory
    LEFT JOIN [Application] APP ON NSS.ApplicationId = APP.PK_Application
    LEFT JOIN Sprint SPT ON STY.SprintId = SPT.PK_Sprint
    LEFT JOIN Product PDT ON STY.ProductId = PDT.PK_Product
    LEFT JOIN [User] USR ON DTH.EnteredBy = USR.DisplayName
    WHERE DTH.EnteredBy LIKE @userParam
        AND ActivityDate >= @startDateParam
        AND ActivityDate <= @endDateParam
        AND 1 = CASE ISNUMERIC(@productId)
            WHEN 0
                THEN CASE 
                        WHEN DTH.TaskId IS NULL
                            OR PDT.PK_Product LIKE @productId
                            THEN 1
                        END
            WHEN 1
                THEN CASE 
                        WHEN DTH.TaskId IS NOT NULL
                            AND PDT.PK_Product = @productId
                            THEN 1
                        END
            END
        AND (
            (
                @orgTeamPK = '%'
                AND (
                    USR.[OrganizationalTeamId] LIKE @orgTeamPK
                    OR USR.[OrganizationalTeamId] IS NULL
                    )
                )
            OR (
                @orgTeamPK <> '%'
                AND (USR.[OrganizationalTeamId] LIKE @orgTeamPK)
                )
            )
        AND (
            (
                STY.Number LIKE @search
                OR STY.Number IS NULL
                )
            OR (
                STY.Title LIKE @search
                OR STY.Title IS NULL
                )
            OR (
                TSK.NAME LIKE @search
                OR TSK.NAME IS NULL
                )
            )
        AND (
            (
                @theme = '%'
                AND (
                    dbo.primaryTheme(STY.[Number]) LIKE @theme
                    OR dbo.primaryTheme(STY.[Number]) IS NULL
                    )
                )
            OR (
                @theme != '%'
                AND dbo.primaryTheme(STY.[Number]) = @theme
                )
            )
    GROUP BY DTH.EnteredBy
        ,PDT.[Name]
        ,SPT.[Name]
        ,SPT.[Description]
        ,STY.[Number]
        ,STY.Title
        ,TSK.[Name]
        ,SCY.Catagory
        ,NSS.IncidentNumber
        ,APP.AppName
        ,STY.KanBanProductId
        ,STY.SprintId
        ,NSS.[Description]
        ,TSK.OriginalEstimateHours
        ,STY.Effort
        ,DTH.PointPerson
        ,DTH.NonScrumStoryId
    HAVING SUM(DTH.[Hours]) > 0
    --

    UNION ALL

    --
    -- Sub-TOTAL by PERSON
    --
    SELECT '4' AS RowType
        ,DTH.EnteredBy AS Person
        ,'' AS Project
        ,'' AS ProjectType
        ,'' AS Theme
        ,'' AS StoryNumber
        ,'' AS StoryTitle
        ,'' AS Effort
        ,'Subtotal:' AS Task
        ,'' AS OriginalEstimateHours
        ,'' AS Category
        ,'' AS IncidentNumber
        ,'' AS ApplicationName
        ,CAST(SUM(CASE 
                    WHEN DATEPART(dw, DTH.ActivityDate) = 2
                        THEN DTH.[Hours]
                    ELSE 0
                    END) AS VARCHAR(20)) AS MondayHours
        ,CAST(SUM(CASE 
                    WHEN DATEPART(dw, DTH.ActivityDate) = 3
                        THEN DTH.[Hours]
                    ELSE 0
                    END) AS VARCHAR(20)) AS TuesdayHours
        ,CAST(SUM(CASE 
                    WHEN DATEPART(dw, DTH.ActivityDate) = 4
                        THEN DTH.[Hours]
                    ELSE 0
                    END) AS VARCHAR(20)) AS WednesdayHours
        ,CAST(SUM(CASE 
                    WHEN DATEPART(dw, DTH.ActivityDate) = 5
                        THEN DTH.[Hours]
                    ELSE 0
                    END) AS VARCHAR(20)) AS ThursdayHours
        ,CAST(SUM(CASE 
                    WHEN DATEPART(dw, DTH.ActivityDate) = 6
                        THEN DTH.[Hours]
                    ELSE 0
                    END) AS VARCHAR(20)) AS FridayHours
        ,CAST(SUM(CASE 
                    WHEN DATEPART(dw, DTH.ActivityDate) = 7
                        THEN DTH.[Hours]
                    ELSE 0
                    END) AS VARCHAR(20)) AS SaturdayHours
        ,CAST(SUM(CASE 
                    WHEN DATEPART(dw, DTH.ActivityDate) = 1
                        THEN DTH.[Hours]
                    ELSE 0
                    END) AS VARCHAR(20)) AS SundayHours
        ,CAST(SUM(DTH.[Hours]) AS VARCHAR(20)) AS TotalHours
        ,'' AS DifferentUsers
        ,'' AS DoubleBookedFlag
        ,'' AS PointPerson
    FROM DailyTaskHours DTH
    LEFT JOIN Task TSK ON DTH.TaskId = TSK.PK_Task
    LEFT JOIN Story STY ON TSK.StoryId = STY.PK_Story
    LEFT JOIN Product PDT ON STY.ProductId = PDT.PK_Product
    LEFT JOIN [User] USR ON DTH.EnteredBy = USR.DisplayName
    WHERE DTH.EnteredBy LIKE @userParam
        AND ActivityDate >= @startDateParam
        AND ActivityDate <= @endDateParam
        AND 1 = CASE ISNUMERIC(@productId)
            WHEN 0
                THEN CASE 
                        WHEN DTH.TaskId IS NULL
                            OR PDT.PK_Product LIKE @productId
                            THEN 1
                        END
            WHEN 1
                THEN CASE 
                        WHEN DTH.TaskId IS NOT NULL
                            AND PDT.PK_Product = @productId
                            THEN 1
                        END
            END
        AND (
            (
                @orgTeamPK = '%'
                AND (
                    USR.[OrganizationalTeamId] LIKE @orgTeamPK
                    OR USR.[OrganizationalTeamId] IS NULL
                    )
                )
            OR (
                @orgTeamPK <> '%'
                AND (USR.[OrganizationalTeamId] LIKE @orgTeamPK)
                )
            )
        AND (
            (
                STY.Number LIKE @search
                OR STY.Number IS NULL
                )
            OR (
                STY.Title LIKE @search
                OR STY.Title IS NULL
                )
            OR (
                TSK.NAME LIKE @search
                OR TSK.NAME IS NULL
                )
            )
        AND (
            (
                @theme = '%'
                AND (
                    dbo.primaryTheme(STY.[Number]) LIKE @theme
                    OR dbo.primaryTheme(STY.[Number]) IS NULL
                    )
                )
            OR (
                @theme != '%'
                AND dbo.primaryTheme(STY.[Number]) = @theme
                )
            )
    GROUP BY DTH.EnteredBy
    HAVING SUM(DTH.[Hours]) > 0
    ) AS My_View
ORDER BY Person
    ,RowType
    ,Project
    ,ProjectType
    ,StoryNumber
    ,StoryTitle
    ,Task
            ">
            <SelectParameters>
                <asp:QueryStringParameter Name="userParam" Type="String" DefaultValue="%" />
                <asp:QueryStringParameter Name="startDateParam" Type="String" />
                <asp:QueryStringParameter Name="endDateParam" Type="String" />
                <asp:QueryStringParameter Name="orgTeamPK" Type="String" DefaultValue="%" />
                <asp:QueryStringParameter Name="productId" Type="String" DefaultValue="%" />
                <asp:QueryStringParameter Name="search" Type="String" DefaultValue="%" />
                <asp:QueryStringParameter Name="theme" Type="String" />
            </SelectParameters>
        </asp:SqlDataSource>

5 个答案:

答案 0 :(得分:4)

您的数据库引擎没有这样做,ASP.NET正在这样做。这是一项有助于防止XSS的安全功能。

您可以在绑定字段上设置 HtmlEncode 属性,例如此MSDN链接中的示例:

<asp:boundfield datafield="City"
            htmlencode="false"
            headertext="City"/>

答案 1 :(得分:2)

在您的应用程序中,您可能希望对字符串执行HttpUtility.HtmlDecode

来自MSDN:

  

将已经过HTML编码的HTTP传输字符串转换为已解码的字符串。

http://msdn.microsoft.com/en-us/library/system.web.httputility.htmldecode(v=vs.110).aspx

实际上,可能首选将<br />附加到应用程序本身,而不是查询中。在查询本身以这种方式按摩数据并不常见。通常,为应用程序提取的数据不应附加任何表示层或视图类型的内容,除非它确切地存在于数据库中。为您的应用程序保存该工作。例如,如果这是一个MVC应用程序,您可以考虑在控制器中执行此类工作。

更多...

现在,我从您刚刚发布的代码中看到了问题的完整背景,我将在下面为今后的读者添加相同的评论。自从我进入ASP.NET WebForms之后已经有一段时间了,所以我可能会有点生疏!我的评论......

  

有更好的方法可以解决这个问题,但我担心它可能超出了这个问题的范围,因为它也会涉及你如何创建数据源。如果您有兴趣,可以快速搜索RepeaterGridView的{​​{1}}和两个相关数据表的数据源。这是一个旧的例子(.NET 2.0),但它可能仍然有效,或者至少应该有助于引导您找到更好的例子。

现在换另一个答案? ...

我可以考虑针对您的问题的另一个选项。在查询中,您可以在SQL中追加一个简单的新行(<br />或char(10)),而不是附加\n。然后,不要在asp:BoundColumn中使用GridView该字段,而是使用asp:TemplateField并在模板内部,将\n替换为<br />。就像我之前说的那样,自从我完成WebForms以来已经有一段时间了,但我相信它看起来像是:

<%#Eval("Task").Replace("\n", "<br />")%>

我想您可以使用HtmlDecode执行类似操作,但我不喜欢在这些查询结果中看到HTML标记。

答案 2 :(得分:1)

SELECT REPLACE((SELECT '<br/>' + Notes 
               FROM DailyTaskHours
               WHERE Notes IS NOT NULL
                 AND Notes <> ''
                 AND NonScrumStoryId = DTH.NonScrumStoryId
            FOR XML PATH('')) ,'&lt;br/&gt;','<br/>')

答案 3 :(得分:0)

亲爱的使用以下

SELECT STUFF((
            SELECT '<br/>' + Notes 
            FROM DailyTaskHours
            WHERE Notes IS NOT NULL
                AND Notes <> ''
                AND NonScrumStoryId = DTH.NonScrumStoryId
            FOR XML PATH(''),TYPE).value('.','NVARCHAR(MAX)')
            ), 1, 1, '')
            )

  SELECT STUFF((
            SELECT '<br/>' + Notes 
            FROM DailyTaskHours
            WHERE Notes IS NOT NULL
                AND Notes <> ''
                AND NonScrumStoryId = DTH.NonScrumStoryId
            FOR XML PATH(''),TYPE).value('(./text())[1]','NVARCHAR(MAX)')
            ), 1, 1, '')
            )

答案 4 :(得分:0)

测试数据

DECLARE @DailyTaskHours TABLE (NonScrumStoryId INT, Notes NVARCHAR(100))
INSERT INTO @DailyTaskHours VALUES
(1, 'Note one for value one'),(1, 'Note two for value one'),(1, 'Note three for value one'),
(2, 'Note one for value Two'),(2, 'Note Two for value Two'),(2, 'Note Three for value two')

<强>查询

SELECT STUFF((SELECT '<br/>' + Notes 
                FROM @DailyTaskHours
                WHERE Notes IS NOT NULL
                AND Notes <> ''
                AND NonScrumStoryId = DTH.NonScrumStoryId
                FOR XML PATH(''), TYPE)
               .value('.','NVARCHAR(MAX)'), 1, 5, '') AS NotesList
FROM @DailyTaskHours DTH
GROUP BY NonScrumStoryId

Resutl Set

╔════════════════════════════════════════════════════════════════════════════════╗
║                                   NotesList                                    ║
╠════════════════════════════════════════════════════════════════════════════════╣
║ Note one for value one<br/>Note two for value one<br/>Note three for value one ║
║ Note one for value Two<br/>Note Two for value Two<br/>Note Three for value two ║
╚════════════════════════════════════════════════════════════════════════════════╝

使用CROSS APPLY

SELECT DISTINCT STUFF(REPLACE(C.NotesList,'Break_Tag', '<br/>') , 1, 5, '') AS NotesList
FROM @DailyTaskHours DTH CROSS APPLY 
                         (SELECT 'Break_Tag' + Notes  [text()]
                          FROM @DailyTaskHours
                          WHERE Notes IS NOT NULL
                          AND Notes <> ''
                          AND NonScrumStoryId = DTH.NonScrumStoryId
                          FOR XML PATH('')
                          )C(NotesList)