2011-06-22 22 views
0

未將對象引用設置爲對象的實例,當單擊linkbutton時出現此錯誤。代碼綁定文件包含單擊事件的以下代碼。錯誤:對象引用未設置爲對象的實例,同時使用listview中的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> 

請幫我,謝謝.....

回答

0

FindControl功能無法找到目標控制,因爲它是不直接包含在ListView中。

1

您的html在問題中變得有趣,所以我看不到ListView1在哪裏。我認爲儘管這是一個ListView是可以安全的。在這種情況下,你不能以這種方式找到你的標籤,因爲它在ListViewItem而不是ListView本身。

ListView1.Items[0].FindControl("profileTitleLabel");事情是你應該做

+0

非常感謝你的東西,它的工作完全沒有問題..... –

+0

這是偉大聽到......所以平時的提問者將給予好評和最佳答案的答案 – BritishDeveloper

相關問題