【问题标题】:Access a control's property in an aspx page from another aspx page从另一个 aspx 页面访问一个 aspx 页面中的控件属性
【发布时间】: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时,通常会通过公共属性公开控件值。
  • 它不起作用,我已尝试公开控制值

标签: c# asp.net


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    您的页面是否在容器内(母版页)?

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

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

    var previousPageMaster = PreviousPage.Master;
    

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

    var maincontentplaceholder = previousPageMaster.FindControl("maincontent");
    

    然后你应该可以在 ItemDetails 页面中获得对 ListView 的引用:

    var listView_PreviousPage = (ListView)maincontentplaceholder.FindControl("ListView1");
    

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

    【讨论】:

      猜你喜欢
      • 2014-09-04
      • 2014-03-07
      • 2021-10-05
      • 1970-01-01
      • 2011-01-26
      • 2010-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多