如何在ListView中使用模板(ASP.NET,C#)

3
我有许多页面显示相同的布局和结果(例如搜索结果、热门浏览、最近添加等)。如果我想更改任何字段,我必须在所有页面上进行更改。是否可以在listview中使用模板或用户控件,在一个地方更改并如何传递id参数?
附加信息: 现在我有类似这样的东西:
<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource" DataKeyNames="id">
    <ItemTemplate>
                 Id:
         <asp:Label ID="IDLabel" runat="server" 
         Text='<%# Eval("id") %>' />
         Description:
         <asp:Label ID="DescriptionLabel" runat="server" 
              Text='<%# Eval("Description") %>' />
   </ItemTemplate>
</asp:ListView>

What i woild like to have is:

<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource" DataKeyNames="id">
    <ItemTemplate>
         <usrCtrl:info ID="info1" runat="server" /> 
   </ItemTemplate>
</asp:ListView>

以及一个模板文件:

Id:
<asp:Label ID="IDLabel" runat="server" 
Text='<%# Eval("id") %>' />
Description:
<asp:Label ID="DescriptionLabel" runat="server" 
    Text='<%# Eval("Description") %>' />

感谢,Jim Oak。
2个回答

2
尝试这个:
列表视图:
<asp:ListView ID="ListView2" runat="server" 
    onitemdatabound="ListView2_ItemDataBound">
  <ItemTemplate>
     <usrCtrl:info Description_Lbl='<%# Bind("id") %>' 
                 ID_Lbl='<%# Bind("Description") %>' ID="info1"  runat="server" />
 </ItemTemplate>
</asp:ListView>

用户控件代码:

info.ascx:

<%@ Control Language="C#" AutoEventWireup="true" 
                    CodeBehind="info.ascx.cs" Inherits="info" %>
<asp:Label ID="IDLabel" runat="server"  />
Description:
<asp:Label ID="DescriptionLabel" runat="server"  />

info.ascx.cs:

public partial class info: System.Web.UI.UserControl
{
    public string ID_Lbl {get;set;}
    public string Description_Lbl { get; set; }
    protected void Page_Load(object sender, EventArgs e)
    {
        IDLabel.Text = ID_Lbl;
        DescriptionLabel.Text = Description_Lbl;
    }
}

谢谢您的快速回复。这看起来是我的解决方案,唯一的问题是我无法获得结果。有一个“描述:”但数据字段为空。 - Oak
某些原因导致该值未出现。 - Pamingkas Sevada

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接