← Tutti gli articoli

FIndControl within Page ( within a MasterPage ) from a UserControl Code Behind

11 febbraio 2011  · Asp.Net · Article  ·  454 visite

I am trying to find a control which is within my aspx page within a MasterPage.

I have a master page with the following ContentPlaceHolder:
 
  1. < asp:ContentPlaceHolder   ID = "cphMiddle"   runat = "server" >   
  2. </ asp:ContentPlaceHolder >   

In the content page I use the following web user control:
  1. < %@ Register  Src = "WebControls/ucFinanziamentoAttachments.ascx"   TagName = "ucFinanziamentoAttachments"   
  2.      TagPrefix = "uc2"  % >       
and the following content
  1. <asp:Content ID="Content3" ContentPlaceHolderID="cphMiddle" runat="Server">     
  2.      
  3.     <asp:HiddenField ID="hf_ID_SpidFinanziamentoStato" runat="server" />     
  4.         
  5. <uc2:ucFinanziamentoAttachments ID="FinanziamentoAttachments" runat="server" />  
  6. </asp:Content>    

Now I need to read the hf_ID_SpidFinanziamento in the web user control.
 
To do it we use the following code in the WebUserControl's PageLoad:
  1. if(!IsPostBack){   
  2.   
  3.              hf2_ID_SpidFinanziamento.Value  =  Request .QueryString["ID_SpidFinanziamento"];   
  4.             int  ID_SpidFinanziamento  =  Convert .ToInt32(hf2_ID_SpidFinanziamento.Value);   
  5.             RefreshFinanziamentoAttachments(ID_SpidFinanziamento);   
  6.   
  7.             //su page hf_ID_SpidFinanziamentoStato   
  8.             string cn;   
  9.             foreach (Control c in this.Page.Master.FindControl("cphMiddle").Controls)   
  10.             {   
  11.                  c cn  = c.ClientID;   
  12.                  cn  =  "" ;   
  13.             }   
  14.   
  15.   
  16.             HiddenField  hf  = (HiddenField)this.Page.Master.FindControl("cphMiddle").FindControl("hf_ID_SpidFinanziamentoStato");   
  17.              hf hf_ID_SpidFinanziamentoStato2.Value  = hf.Value;   
  18.   
  19.         }