错误:对象引用未设置为对象的实例,而在listview中使用findcontrol获取标签值

时间:2011-06-22 15:42:47

标签: asp.net listview label findcontrol

对象引用未设置为对象的实例,当我单击链接按钮时,我得到此错误。代码绑定文件包含以下用于单击事件的代码。

protected void viewProfileLinkButton_Click(object sender, EventArgs e)  
        {  
            String emailID = ((Label)ListView1.FindControl("profileTitleLabel")).Text;  
            Response.Redirect("profilePage.aspx?e=" + emailID);  
        }  

我的aspx页面是这样的:

default1.aspx: 

<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1" 
                    onselectedindexchanged="ListView1_SelectedIndexChanged">
             <LayoutTemplate>
                <div ID="itemPlaceholderContainer" runat="server" style="">
                    <span runat="server" id="itemPlaceholder" />
                </div>
                <div style=" text-align:right; margin-right:100px; width:750px;">
                    <asp:DataPager ID="DataPager1" runat="server" PageSize="2">
                        <Fields>
                            <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" 
                                ShowNextPageButton="False" ShowPreviousPageButton="False" />
                            <asp:NumericPagerField />
                            <asp:NextPreviousPagerField ButtonType="Button" ShowLastPageButton="True" 
                                ShowNextPageButton="False" ShowPreviousPageButton="False" />
                        </Fields>
                    </asp:DataPager>
                </div>
            </LayoutTemplate>
            <AlternatingItemTemplate>
            <hr  style=" float:left; width:552px; margin-left:17px;"/>
            <div class="center_title_bar" style="text-align:left;">
                    <asp:Label ID="nameLabel" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"first_name") %>'></asp:Label>
        </div>
        <div class="profile_box_big">
            <div class="top_profile_box_big"></div>
            <div class="center_profile_box_big">          
                 <div class="profile_img_big">
                 &nbsp;<asp:Image ID="profileImage" runat="server" Height="160px" Width="150px" />
                 </div>
                     <div class="details_big_box">
                         <div class="specifications">
                            <asp:Label ID="profileTitleLabel" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"email_id") %>' 
                                 ForeColor="#388977"></asp:Label><br />

                         <asp:LinkButton ID="viewProfileLinkButton" runat="server">View Profile</asp:LinkButton>
                     </div>                        
            </div>
            <div class="bottom_profile_box_big"></div>
            </div>                        
        </div>
            </AlternatingItemTemplate>
            <EditItemTemplate/>
            <EmptyDataTemplate>

            </EmptyDataTemplate>
            <InsertItemTemplate/>
            <ItemTemplate>
            <br />
                <div class="center_title_bar" style="text-align:left;">
                    <asp:Label ID="nameLabel" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"first_name") %>'></asp:Label>
        </div>
        <div class="profile_box_big">
            <div class="top_profile_box_big"></div>
            <div class="center_profile_box_big">          

                     <div class="details_big_box">
                         <div class="specifications">
                            <asp:Label ID="profileTitleLabel" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"email_id") %>' 
                                 ForeColor="#388977"></asp:Label><br />

                         </div>
                         <asp:LinkButton ID="viewProfileLinkButton" runat="server" OnClick="viewProfileLinkButton_Click">View Profile</asp:LinkButton>
                     </div>                        
            </div>
            <div class="bottom_profile_box_big"></div>
            </div>                        
        </div>
            </ItemTemplate>
            <SelectedItemTemplate/>
        </asp:ListView>

请帮帮我,谢谢.....

2 个答案:

答案 0 :(得分:1)

你的html在问题中变得很有趣,所以我看不到ListView1在哪里。我认为虽然这是一个ListView是安全的。在这种情况下,您无法找到您的Label,因为它位于ListViewItem而不是ListView本身。

ListView1.Items[0].FindControl("profileTitleLabel");之类的东西是你应该做的事情

答案 1 :(得分:0)

FindControl函数无法找到目标控件,因为它未直接包含在ListView中。