我想在页眉中显示一些细节,如名称和许可证号码,这是唯一且不会在特定的Listview中重复的。有没有办法显示这个?
您可以为要在布局中显示的数据引入页面属性,从ItemDataBound事件处理程序中获取第一行数据项,并在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();
}
}
<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>