Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#如何在listview中设置标签文本?_C#_Asp.net_Listview_Label - Fatal编程技术网

C#如何在listview中设置标签文本?

C#如何在listview中设置标签文本?,c#,asp.net,listview,label,C#,Asp.net,Listview,Label,在我的codebehind中,我希望设置标签的文本。以下是aspx代码: <asp:ListView ID="lstRegistrations" runat="server"> <LayoutTemplate> <table cellspacing="0" cellpadding="0" border="0"> <tr> <th width="80" align=

在我的codebehind中,我希望设置标签的文本。以下是aspx代码:

<asp:ListView ID="lstRegistrations" runat="server">
    <LayoutTemplate>
        <table cellspacing="0" cellpadding="0" border="0">
            <tr>
                <th width="80" align="left">
                    <asp:Label ID="lblDate" runat="server" Text="<%= GetTranslatedText(7726) %>" />
                </th>
                <th width="150" align="left">
                    <asp:Label ID="lblAuthor" runat="server" Text="<%= GetTranslatedText(7728) %>"  />
                </th>
                <th width="290" align="left">
                    <asp:Label ID="lblRegistration" runat="server" Text="<%= GetTranslatedText(6671) %>"  />
                </th>
                <th width="60" align="left">
                    <asp:Label ID="lblVersion" runat="server" Text="<%= GetTranslatedText(13) %>"  />
                </th>
            </tr>
            <tr>
                <td colspan="4" style="height: 3px;"></td>
            </tr>
            <tr runat="server" id="itemPlaceholder"></tr>
        </table>
    </LayoutTemplate>

    <ItemTemplate>
        <tr style="background-color:#FFFFD0;">
            <td style="padding-left: 3px">
                <%# ((DateTime)Eval("Date")).ToString("d-M-yyyy") %>
            </td>
            <td>
                <%# GetStaffNameById((int)Eval("StaffID")) %>
            </td>
            <td>
               <%# Server.HtmlEncode(Eval("Text").ToString())%> 
            </td>
            <td>
                <%# Eval("Version") %>
            </td>
        </tr>
    </ItemTemplate>
    <AlternatingItemTemplate>
        <tr style="background-color: #C89292">
            <td style="padding-left: 3px">
                <%# ((DateTime)Eval("Date")).ToString("d-M-yyyy") %>
            </td>
            <td> 
                <%# GetStaffNameById((int)Eval("StaffID")) %>
            </td>
            <td>
               <%# Server.HtmlEncode( Eval("Text").ToString() )%> 
            </td>
            <td>
                <%# Eval("Version") %>
            </td>
        </tr>
    </AlternatingItemTemplate>
</asp:ListView>

在顶部的layoutTemplate中,我有4个标签,我想更改其中的文本属性。我尝试使用lstRegistrations.FindControl()方法访问标签,但该方法找不到标签。我还尝试了Page.FindControl()方法,但是这个方法或者找不到标签。然后我想,我创建了一个方法并在我的aspx页面中调用它(参见我的代码)。我没有收到任何错误,但我没有看到任何文本


我做错了什么?

如何指定标签的值?什么时候加载?当用户选择某个操作时

您可以实现ItemDataBound事件,并为每一行访问标签以设置其文本

protected void ListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    if (e.Item.ItemType == ListViewItemType.DataItem)
    {
        Label someLabel = (Label)e.Item.FindControl("MyLabel");
        someLabel.Text = "Hurray!";
    }
}

您的
FindControl()
将永远无法工作,因为每行都有一组标签。FindControl应该从哪一行获取标签?您需要先到达行,然后获取所需的标签。

如何指定标签的值?什么时候加载?当用户选择某个操作时

您可以实现ItemDataBound事件,并为每一行访问标签以设置其文本

protected void ListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    if (e.Item.ItemType == ListViewItemType.DataItem)
    {
        Label someLabel = (Label)e.Item.FindControl("MyLabel");
        someLabel.Text = "Hurray!";
    }
}
您的
FindControl()
将永远无法工作,因为每行都有一组标签。FindControl应该从哪一行获取标签?您需要先到达行,然后获取所需的标签。

您可以:

  • 尝试获取对控件本身的引用,而不是依赖ListView:
  • 将一个虚拟对象绑定到ListView,然后以您最初想要的方式使用FindControl:以及(查看底部附近的两个链接)
  • 问题是,在ListView上调用DataBind()之前,LayoutTemplate中的项不可用。因此,FindControl在此之前返回null。

    您可以:

  • 尝试获取对控件本身的引用,而不是依赖ListView:
  • 将一个虚拟对象绑定到ListView,然后以您最初想要的方式使用FindControl:以及(查看底部附近的两个链接)

  • 问题是,在ListView上调用DataBind()之前,LayoutTemplate中的项不可用。因此,FindControl在此之前返回null。

    我希望在加载页面时设置标签。但我每行只有一组标签。布局模板描述了布局,对吗?所以这不是重复的还是我错了?LayoutTemplate每行重复一次,您在其中添加一个占位符,ItemTemplate将放置在占位符中。尽管如此,在ItemDataBound中,如果您需要的话,您应该能够访问标签以从代码隐藏中设置一些值。我找不到我的4个标签。我认为这是因为标签所在的行没有数据绑定。我在这一排不用。我不确定该行(放置标签的位置)是否被视为DataItem,因为该行位于“itemplaceholder”行之外。但我不确定。你是对的,它不起作用。正如您对Ahmad所回答的,最好的方法是在ListView中执行FindControl(“lblName”)。好的:)Thnx尽管如此:)我希望在加载页面时设置标签。但我每行只有一组标签。布局模板描述了布局,对吗?所以这不是重复的还是我错了?LayoutTemplate每行重复一次,您在其中添加一个占位符,ItemTemplate将放置在占位符中。尽管如此,在ItemDataBound中,如果您需要的话,您应该能够访问标签以从代码隐藏中设置一些值。我找不到我的4个标签。我认为这是因为标签所在的行没有数据绑定。我在这一排不用。我不确定该行(放置标签的位置)是否被视为DataItem,因为该行位于“itemplaceholder”行之外。但我不确定。你是对的,它不起作用。正如您对Ahmad所回答的,最好的方法是在ListView中执行FindControl(“lblName”)。好的:)Thnx尽管如此:)我使用了第一种方式。我认为这是最整洁的方法。ThnxI使用了第一种方式。我认为这是最整洁的方法。Thnx