【问题标题】:Listview notshowing up on the code behind fileListview 未显示在文件后面的代码中
【发布时间】:2012-06-11 18:59:49
【问题描述】:

我创建了一个列表视图,并且需要在第一个列表视图中创建另一个列表视图。当我转到文件后面的代码时,我没有看到 listview2 控件,它是第二个列表视图的名称

我做错了什么?

以下是我的标记代码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Attestation2.aspx.cs" Inherits="Attestation2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body style="z-index: 1; left: 21px; top: 0px; position: absolute; height: 313px;
    width: 983px">
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="Entitlementdlist" runat="server" AutoPostBack="true" Visible="true"
            ToolTip="All the Requests in this dropdown are Entitlement Requests only">
        </asp:DropDownList>

        <asp:ListView ID="listview1" runat="server" ItemPlaceholderID="itemplaceholder" >
            <LayoutTemplate>         
            <asp:PlaceHolder ID="itemplaceholder" runat="server" />  
            </LayoutTemplate> 

            <ItemTemplate>
                <table style="border:1px solid black;">
                    <tr>
                        <td>Report Name</td>
                        <td><asp:TextBox ID="ReportNameLbl" runat="server" Text='<%#Eval("REPORT_DESCRIPTION.rptdesctext") %>' Width="500"></asp:TextBox> </td>
                        <td>
                        <asp:CheckBoxList ID="CheckBoxList1" runat="server" RepeatDirection="Horizontal">
                             <asp:ListItem Text="Yes" Value="Yes"></asp:ListItem>
                                <asp:ListItem Text="No" Value="No"></asp:ListItem>
                            </asp:CheckBoxList>
                        </td>
                    </tr>
                    <tr>
                        <td>Content Description</td>
                        <td>
                        <asp:Label ID="ContentDescLbl" Text='<%#Eval("REPORT_REQUEST.StopLossMax") %>' runat="server" ></asp:Label>
                        </td>
                        <td>
                        <asp:CheckBoxList ID="CheckBoxList2" runat="server" RepeatDirection="Horizontal">
                             <asp:ListItem Text="Yes" Value="Yes"></asp:ListItem>
                                <asp:ListItem Text="No" Value="No"></asp:ListItem>
                            </asp:CheckBoxList>
                        </td>




                    </tr>
                    <tr>

                    <td>
                    Frequency
                    </td>
                    <td>
                     <asp:Label ID="frequencyLbl" Text='<%#Eval("REPORT_REQUEST.reportfrequency") %>' runat="server" ></asp:Label>
                    </td>
                    <td>
                    <asp:CheckBoxList ID="CheckBoxList3" runat="server" RepeatDirection="Horizontal">
                             <asp:ListItem Text="Yes" Value="Yes"></asp:ListItem>
                                <asp:ListItem Text="No" Value="No"></asp:ListItem>
                            </asp:CheckBoxList>
                    </td>
                    </tr>
                    <tr>


                    <td>
             <asp:ListView ID="listview2" runat="server" ItemPlaceholderID="itemplaceholder1" >
            <LayoutTemplate>         
            <asp:PlaceHolder ID="itemplaceholder1" runat="server" />  
            </LayoutTemplate> 

            <ItemTemplate>

             <table style="border:1px solid black;">
                    <tr>

                        <td>Recepients</td>
                        <td>
                         <asp:Label ID="frequencyLbl" Text='<%#Eval("REPORTAlaska.reportfrequency") %>' runat="server" ></asp:Label>
                        </td>
                    </tr>
                    </table>
            </ItemTemplate>
            </asp:ListView>
                    </td>

                    </tr>
                </table>


            </ItemTemplate>
        </asp:ListView>
    </div>
    </form>
</body>
</html>

【问题讨论】:

    标签: asp.net


    【解决方案1】:

    由于是嵌套在模板中的控件,所以需要使用“FindControl”方法。

    编辑:

    如果您使用函数,请查看 MSDN 中的描述,这将节省您在此处等待答案的时间 :) 简而言之,FindControl 不是递归函数,并且如果控件不直接在集合中所选元素的控件,它将不起作用。您正在尝试访问另一个 ListView 中的 ListView,因此您不能使用 Page.FindControl,但您可以执行以下操作:

    myListView.FindControl(...)
    

    希望你现在就明白这个想法并且它对你有用。

    【讨论】:

    • 我使用了查找控件方法,现在得到错误:对象引用未设置为对象的实例。下面是我的代码 ListView listview_nested = Page.FindControl("listview2") as ListView; listview_nested.DataSource = RECEPIENTDATA; listview_nested.DataBind();
    【解决方案2】:

    这是正常行为。您的嵌套 ListView 是在外部 ListView 的模板中定义的。 您必须使用特定行的 FindControl() 方法访问嵌套列表视图。

    例如代码检查这个:Find Control

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多