Error: Object reference not set to an instance of a object, while getting a label value using findcontrol which in 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="viewProfi开发者_StackOverflow社区leLinkButton" 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.....
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
The FindControl
function could not find the target control because it is not directly contained in the ListView
.
精彩评论