listview只显示两次记录

时间:2012-09-07 18:02:59

标签: asp.net listview

我有一个listview,显示来自和sql server视图的记录。 数据由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

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

我做错了什么? 感谢

1 个答案:

答案 0 :(得分:0)

编辑3

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

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

 SELECT NewId() as MyUniqueID from blah

NewId()函数将确保为结果集中的每一行获取全局unqiue标识符,但不对任何后续操作使用UniqueID,因为数据不会保留在数据库中,因此每次ListView都会丢失得到了更新。

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

或者CustomerID + UserID中的Schedules是唯一的,应该合并在DataKeyNames中吗?

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

DataKeysNames="CustomerID,UserID,ServiceID"

如果这不起作用,您应该向单ListView解释您希望返回CustomerID的内容以及ListView中每条记录的唯一属性

取出联盟,你的第一个例子看起来效果最好,除了唯一性