4

我想在标题中显示一些详细信息,例如 Name, Lic.# ,这是唯一的,并且对于特定的 Listview 不重复。反正有没有显示这个?

4

1 回答 1

5

您可以为要在布局中显示的数据引入页面属性,从 ItemDataBound 事件处理程序中的第一行 dataItem 获取它们,并在 ListView 的 DataBound 事件处理程序中绑定布局控件:

<asp:ListView ID="ListView1" runat="server" DataKeyNames="job_id" DataSourceID="SqlDataSource1">
          <LayoutTemplate>
               <table>
                    <thead>
                         <tr runat="server" id="headerRow">
                              <th>
                                   <%# FirstHeaderText %></th>
                              <th>
                                   <%# SecondHeaderText  %>
                              </th>
                         </tr>
                    </thead>
                    <tbody>
                         <tr runat="server" id="itemPlaceholder" />
                    </tbody>
               </table>
          </LayoutTemplate>
          <ItemTemplate>
               <tr>
                    <td>
                         <%# Eval("job_id")%></td>
                    <td>
                         <%# Eval("job_desc")%></td>
               </tr>
          </ItemTemplate>
     </asp:ListView>
     <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:PUBSConnectionString %>"
          SelectCommand="SELECT [job_id], [job_desc] FROM [jobs]"></asp:SqlDataSource>

服务器代码:

protected string FirstHeaderText
{
    get { return ViewState["FirstHeaderText"] as string; }
    set { ViewState["FirstHeaderText"] = value; }
}
protected string SecondHeaderText
{
    get { return ViewState["SecondHeaderText"] as string; }
    set { ViewState["SecondHeaderText"] = value; }
}

protected void Page_Init(object sender, EventArgs e)
{
    ListView1.ItemDataBound += new EventHandler<ListViewItemEventArgs>(ListView1_ItemDataBound);
    ListView1.DataBound += new EventHandler(ListView1_DataBound);
}

void ListView1_DataBound(object sender, EventArgs e)
{
    ((HtmlTableRow)ListView1.FindControl("headerRow")).DataBind();
}

void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    if (string.IsNullOrEmpty(FirstHeaderText) && e.Item.ItemType == ListViewItemType.DataItem)
    {
        var dataRow = e.Item.DataItem as DataRowView;
        FirstHeaderText = dataRow["job_id"].ToString();
        SecondHeaderText = dataRow["job_desc"].ToString();
    }
}

另一个可用的决定是将标题部分放入 itemTemplate 但仅在第一条记录中显示:

<asp:ListView ID="ListView1" runat="server" DataKeyNames="job_id" DataSourceID="SqlDataSource1">
     <LayoutTemplate>
          <table>
               <asp:PlaceHolder runat="server" ID="itemPlaceholder" />
          </table>
     </LayoutTemplate>
     <ItemTemplate>
          <tr runat="server" visible='<%# (int)DataBinder.Eval(Container, "DataItemIndex") == 0 %>'>
               <th>
                    <%# Eval("job_id")%></th>
               <th>
                    <%# Eval("job_desc")%></th>
          </tr>
          <tr>
               <td>
                    <%# Eval("job_id")%></td>
               <td>
                    <%# Eval("job_desc")%></td>
          </tr>
     </ItemTemplate>
</asp:ListView>
于 2012-09-17T14:14:10.040 回答