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

发布于 2024-11-16 13:46:09 字数 4625 浏览 2 评论 0原文

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

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>

请大家帮帮我,谢谢......

Object reference not set to instance of an object, this error I'm getting when I click the linkbutton. The code bind file contain the below code for click event.

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

My aspx page is like this:

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">
                  <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>

Please help me out, Thank you.....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鹤舞 2024-11-23 13:46:09

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

ListView1.Items[0].FindControl("profileTitleLabel"); 这样的事情是你应该做的事情

Your html has gone funny in the question so I cannot see where ListView1 is. I think it is safe to assume though that is is a ListView. In which case you cannot find your Label that way since it is in a ListViewItem rather than the ListView itself.

Something like ListView1.Items[0].FindControl("profileTitleLabel"); is the thing you should be doing

源来凯始玺欢你 2024-11-23 13:46:09

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

The FindControl function could not find the target control because it is not directly contained in the ListView.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文