错误:在使用 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">
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的 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 doingFindControl
函数无法找到目标控件,因为它没有直接包含在ListView
中。The
FindControl
function could not find the target control because it is not directly contained in theListView
.