0

I have the following situation:

Page1.aspx is based on a master page and has a hidden field called "hdFlotaID" in it. On the same page I have a button for which I set PostBackUrl="Page2.aspx". When I click the buton, the Page2 is loaded but I can't get the hidden field. I tried both (as I saw on msdn or other posts):

this.PreviousPage.Controls[0].FindControl("hdFlotaID")

and

this.PreviousPage.FindControl("hdFlotaID")

but they return null.

This.PreviousPage returns a value, but Controls[0] of that value seems to return the masterpage and I want the content page.

I also checked and the hidden field has runat server value and ClientID mode is set to static (I even checked the generated HTML and the ID is correct)

Can you please help me! Thank you

SOLUTION: Ok,so based on your help I got it to work like this

this.PreviousPage.Controls[0].FindControl("CPH").FindControl("hdFlotaID")

where CPH is the ID of the ContentPlaceHolder from the master page.

Also the ideea with the public property is very good allthough in my case adding the PreviousPageType directive was giving me some errors about namespaces. If I removed the directive and cast it in codebehind it works fine.

Thank you all very much

4

2 回答 2

1

FindControl 只搜索一个级别,即顶级容器,并且内容页面的所有控件并不直接在主控件集合中,而是在主内容控件集合中。

为达到这个

1)你需要编写一个递归版本的FindControl。试试这样(没有测试过):

((Page1)this.PreviousPage).FindControl("hdFlotaID")

或者

2) 将前一页类型转换为它所在的页面类型。然后您可以访问控件。

于 2012-05-16T09:08:05.113 回答
0

在 page1.aspx 中设置一个属性,使用 .aspx 返回隐藏字段的值this.Page.MasterPage.FindControl("hdFlotaID")。在 Page2.aspx 中,"PreviousPageType"在 ASPX 文件中添加标签。这样,您可以以类型安全的方式访问 previos 页面属性,例如this.PreviousPage.hdFlotaID_property

于 2012-05-16T09:07:01.833 回答