首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从其他aspx页访问aspx页中的控件属性

从其他aspx页访问aspx页中的控件属性
EN

Stack Overflow用户
提问于 2015-10-24 05:54:35
回答 2查看 2.9K关注 0票数 0

我有两个aspx页面,aspx页面,假设page1.aspx包含一个列表视图,代码如下

代码语言:javascript
运行
复制
<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页面的代码如下所示

代码语言:javascript
运行
复制
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.请帮帮我。我想要上一页中控件的属性值。

EN

回答 2

Stack Overflow用户

发布于 2015-10-24 10:42:00

我假设你是从一个页面点击到下一个页面。我建议处理下一次单击按钮,并将控件值复制到session(),或者在下一页的请求中传递它们。

票数 0
EN

Stack Overflow用户

发布于 2015-10-24 13:35:37

您的页面在容器(母版页)中吗?

如果是,则FindControl方法在当前命名容器中查找控件。如果要查找的控件位于另一个控件内(通常在模板内),则必须首先获取对容器的引用,然后搜索容器以查找要获取的控件。

因此,如果page1.aspx包含在母版页中,则在page2.aspx中获取对该母版页的引用,如下所示:

代码语言:javascript
运行
复制
var previousPageMaster = PreviousPage.Master;

然后获取对包含列表视图的contentplaceholder的引用:

代码语言:javascript
运行
复制
var maincontentplaceholder = previousPageMaster.FindControl("maincontent");

然后,您应该能够在ItemDetails页面中获得对ListView的引用:

代码语言:javascript
运行
复制
var listView_PreviousPage = (ListView)maincontentplaceholder.FindControl("ListView1");

有关更多信息,请查看链接,https://msdn.microsoft.com/en-us/library/ms178139.aspx

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33311954

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档