【发布时间】:2015-10-23 21:54:34
【问题描述】:
我有2个aspx页面,aspx页面,比如说page1.aspx包含一个列表视图,代码如下
<asp:ListView ID="ListView1" runat="server"
GroupItemCount="3" DataSourceID="SqlDataSource1">
<LayoutTemplate>
<table style="table-layout:fixed;width:100%">
<tr id="groupPlaceholder" runat="server"></tr>
</table>
</LayoutTemplate>
<GroupTemplate>
<tr>
<td id="itemPlaceholder" runat="server"></td>
</tr>
</GroupTemplate>
<ItemTemplate>
<td align="center">
<asp:Image ID="productImage" ImageUrl='<%# Eval("ImageUrl") %>' runat="server"/>
<br />
<asp:LinkButton ID="ProductTitleLinkButton"
runat="server" Text='<%# Eval("ProductTitle") %>'
OnClick="ProductTitleLinkButton_Click"
PostBackUrl="~/ItemDetails.aspx">
</asp:LinkButton>
<br />Rs.
<asp:Label ID="PriceLabel" runat="server" Text='<%# Eval("Price") %>'></asp:Label>
<br />
</td>
</ItemTemplate>
<GroupSeparatorTemplate>
<tr runat="server">
<td colspan="3"><hr /></td>
</tr>
</GroupSeparatorTemplate>
</asp:ListView>
这里我尝试从另一个aspx页面访问ListView1 listview控件的DataSourceId属性、productImage图像控件的ImageUrl属性和ProductTitleLinkButton链接按钮控件的Text属性。
第二个aspx页面的代码,比如page2.aspx如下
protected void Page_Load(object sender, EventArgs e)
{
//Checking if itemsView page exists
if (Page.PreviousPage != null)
{
//Getting the list view in previous page
ListView listView_PreviousPage = (ListView)PreviousPage.FindControl("ListView1");
//Getting the data source of list view in the previous page
string dataSource = listView_PreviousPage.DataSourceID;
//Getting the SQL data source used by the list view in the previous page
SqlDataSource sqlDataSource_PreviousPage = (SqlDataSource)PreviousPage.FindControl(dataSource);
//Getting the SelectCommand property (to get the query) of the SQL Data source
string selectCommand = sqlDataSource_PreviousPage.SelectCommand;
//Getting the image of the product selected in itemsView page
Image productImage_PreviousPage = (Image)PreviousPage.FindControl("productImage");
//Getting the image url of the image
string imageUrl_PreviousPage = productImage_PreviousPage.ImageUrl;
}
}
我正在使用FindControl()查找上一页的控件。但我收到了System.NullReferenceException: Object reference not set to an instance of an object. 请帮帮我。我想要上一页中控件属性的值。
【问题讨论】:
-
在使用PreviousPage时,通常会通过公共属性公开控件值。
-
它不起作用,我已尝试公开控制值