0

我有一个列表视图,它显示来自和 sql 服务器视图的记录。数据按 customerid 过滤,并在运行视图时具有 2 条唯一记录。然而,列表视图将只显示第一条记录,并显示两次。所以列表视图中显示了两条记录,但它没有显示视图生成的两条记录,它只是在视图中显示了第一条记录两次。我希望这很清楚。

到底是怎么回事?我一直在寻找几个小时没有运气。

<asp:ListView ID="lstBeforeAfter"  runat="server" DataKeyNames="CustomerID" InsertItemPosition="None">
    <LayoutTemplate>
        <div class="divreg">
            <table ID="itemPlaceholderContainer" runat="server" cellpadding="2" class="tablebasic" style="vertical-align:top">
                <tr ID="Tr1" runat="server" align="center">
                    <td style="background-color:#7e0d7d; color:White; font-size:1.2em; font-weight:bold; padding-top:4px; padding-bottom:4px">
                        Services
                    </td>
                </tr>
                <tr ID="itemPlaceholder" runat="server">
                </tr>
            </table>
        </div>
        </td>
    </LayoutTemplate>     

     <ItemTemplate>
         <tr style="">

             <td align="center">
                <div class="divwrap">
                <div class="left3ColHeader">
                   <b>Date</b>
               </div>
                <div class="mid3ColHeader">
                  <b>Stylist</b>
                </div>
               <div class="right3ColHeader">
                  <b>Services</b>
                </div> 
                    <div class="formclear">
                    </div>                     
                <div class="left3ColBorder">
                   <%# Eval("SchedStartDateTime", "{0:d}")%>
               </div>
                <div class="mid3ColBorder">
                  <%#Eval("FullName")%>
                </div>
               <div class="right3ColBorder">
                  <%#Eval("ServiceText")%>
                </div> 
                    <div class="formclear">
                    </div>

               <div class="left2ColHeader">
                    <b>Before Picture</b>
               </div>
                <div class="right2ColHeader">
                   <b>After Picture</b>
                </div>
                <div class="formclear">
                    </div>



               <div class="left2ColPicBorder">

               <asp:Image ID="imgLeft" ImageUrl='<%# Eval("BeforePicPath") %>' runat="server" />
               </div>
                <div class="right2ColPicBorder">
                  <asp:Image  runat="server" ID="imgRight" ImageUrl='<%# Eval("AfterPicPath") %>' />
                </div>


                <div class="formclear">
                    </div>      

                <div class="left2ColBorder">
                  <asp:Label ID="lblBefore"  Width="95%" runat="server" Text='<%# Eval("BeforePicName") %>'></asp:Label>
               </div>
                <div class="right2ColBorder">
                    <asp:Label ID="lblAfter" Width="95%" runat="server" Text='<%# Eval("AfterPicName") %>'></asp:Label>
                </div>
                <div class="formclear">
                    </div> 
                <div class="divreg">
                    <hr />
                </div>
                    <div class="formclear">
                    </div>       
              </div>
              </td>

         </tr>
     </ItemTemplate>
     <AlternatingItemTemplate>
        <tr style="background-color:#ffffd9">

             <td align="center">
                <div class="divwrap">
                <div class="left3ColHeader">
                   <b>Date</b>
               </div>
                <div class="mid3ColHeader">
                  <b>Stylist</b>
                </div>
               <div class="right3ColHeader">
                  <b>Services</b>
                </div> 
                    <div class="formclear">
                    </div>                     
                <div class="left3ColBorder">
                   <%# Eval("SchedStartDateTime", "{0:d}")%>
               </div>
                <div class="mid3ColBorder">
                  <%#Eval("FullName")%>
                </div>
                <div class="right3ColBorder">
                <%# Eval("ServiceText").ToString%>
                </div>

                    <div class="formclear">
                    </div>

               <div class="left2ColHeader">
                    <b>Before Picture</b>
               </div>
                <div class="right2ColHeader">
                   <b>After Picture</b>
                </div>
                <div class="formclear">
                    </div>

               <div class="left2ColPicBorder">

               <asp:Image ID="imgLeft" ImageUrl='<%# Eval("BeforePicPath") %>' runat="server" />
               </div>
                <div class="right2ColPicBorder">
                  <asp:Image  runat="server" ID="imgRight" ImageUrl='<%# Eval("AfterPicPath") %>' />
                </div>


                <div class="formclear">
                    </div>      

                <div class="left2ColBorder">
                  <asp:Label ID="lblBefore"  Width="95%" runat="server" Text='<%# Eval("BeforePicName") %>'></asp:Label>
               </div>
                <div class="right2ColBorder">
                    <asp:Label ID="lblAfter" Width="95%" runat="server" Text='<%# Eval("AfterPicName") %>'></asp:Label>
                </div>
                <div class="formclear">
                    </div> 
                <div class="divreg">
                    <hr />
                </div>
                    <div class="formclear">   </div>       
              </div>
              </td>             
         </tr>         
     </AlternatingItemTemplate>
</asp:ListView>

 Protected Sub LoadPortfolio(ByVal iCustID As Integer)
    Dim dc As New SalonDataClassesDataContext


    Dim q = From p In dc.vClientStyles Where p.CustomerID = iCustID Order By p.SchedStartDateTime Descending Select p
    If q.Count > 0 Then
        Me.lstBeforeAfter.DataSource = q
        Me.lstBeforeAfter.DataBind()

    End If


End Sub

SELECT ISNULL(dbo.Services.ServiceID, 0) AS ServiceID, dbo.Schedules.SchedStartDateTime, dbo.Schedules.ServiceText, dbo.Schedules.CustomerID, 
                  ISNULL(dbo.Services.Completed, 0) AS Completed, dbo.Stylists.FullName, dbo.Stylists.StylistID, ISNULL(dbo.Services.BeforePicName, 'No Title') AS BeforePicName, 
                  ISNULL(dbo.Services.BeforePicPath, '../images2020/resume-photo.jpg') AS BeforePicPath, ISNULL(dbo.Services.AfterPicName, 'No Title') AS AfterPicName, 
                  ISNULL(dbo.Services.AfterPicPath, '../images2020/resume-photo.jpg') AS AfterPicPath
FROM  dbo.Schedules INNER JOIN dbo.Stylists ON dbo.Schedules.UserID = dbo.Stylists.UserID LEFT OUTER JOIN  dbo.Services ON dbo.Schedules.ServiceID = dbo.Services.ServiceID
ORDER BY dbo.Schedules.SchedStartDateTime DESC

更改为联合查询,但 linq 不喜欢它。

SELECT ISNULL(dbo.Schedules.ServiceID, 0) AS ServiceID,     dbo.Schedules.SchedStartDateTime, dbo.Schedules.ServiceText, dbo.Schedules.CustomerID, 
ISNULL(dbo.Schedules.Completed, 0) AS Completed, dbo.Stylists.FullName,      dbo.Stylists.StylistID, 'No Title' AS BeforePicName, '../images2020/resume-photo.jpg' AS     BeforePicPath, 'No Title' AS AfterPicName, '../images2020/resume-photo.jpg' AS AfterPicPath
FROM dbo.Schedules INNER JOIN  dbo.Stylists ON dbo.Schedules.UserID = dbo.Stylists.UserID
Where dbo.Schedules.CustomerID = 27
UNION 
SELECT ISNULL(dbo.Services.ServiceID, 0) AS ServiceID,            dbo.Schedules.SchedStartDateTime, dbo.Schedules.ServiceText, dbo.Schedules.CustomerID, 
ISNULL(dbo.Services.Completed, 0) AS Completed, dbo.Stylists.FullName,     dbo.Stylists.StylistID, ISNULL(dbo.Services.BeforePicName, 'No Title') AS BeforePicName, 
ISNULL(dbo.Services.BeforePicPath, '../images2020/resume-photo.jpg')  AS BeforePicPath, ISNULL(dbo.Services.AfterPicName, 'No Title') AS AfterPicName,      ISNULL(dbo.Services.AfterPicPath, '../images2020/resume-photo.jpg') AS AfterPicPath
FROM dbo.Schedules INNER JOIN dbo.Stylists ON dbo.Schedules.UserID = dbo.Stylists.UserID INNER JOIN dbo.Services ON dbo.Schedules.ServiceID = dbo.Services.ServiceID
Where dbo.Schedules.CustomerID = 27 ORDER BY dbo.Schedules.SchedStartDateTime DESC

我现在得到错误...查询结果不能被多次枚举。

我究竟做错了什么?谢谢

4

1 回答 1

0

编辑 3

DataKeyNames 中返回的数据必须是唯一的。您拥有相同的 CustomerID 这一事实意味着您需要另一列来创建唯一键。从理论上讲,您可以在 DataKeyNames 中包含 CustomerID 和 ServiceID,但是您有一些空 ServiceID 记录的事实使其变得困难。

为什么不向每个名为“MyUniqueID”的选择添加一个新列并将其值设置为 NewId()。然后使用 MyUniqueID 作为 DataKeyNames。

 SELECT NewId() as MyUniqueID from blah

NewId() 函数将确保您为结果集中的每一行获取一个全局唯一标识符,但不要将 UniqueID 用于任何后续操作,因为数据不会持久保存在数据库中,因此每次刷新 ListView 时都会丢失。

很难看出为什么在示例中使用联合,主要是因为很难格式化 stackoverflow 代码块。但是很明显,您不止一次拥有相同的客户 ID,因此它不能单独用作唯一的 DataKeyName。

或者可能CustomerID + UserIDSchedules唯一的,应该在 DataKeyNames 中组合?

在 DataKeyNames 中指定多个列,如下所示:-

DataKeysNames="CustomerID,UserID,ServiceID"

如果这不起作用,您应该解释您希望ListView单曲返回给您的内容以及每条记录将具有CustomerID哪些独特属性ListView

拿出工会,你的第一个例子似乎效果最好,除了唯一性

于 2012-09-07T18:51:13.607 回答