Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

This line returns null

Button BtnCopy = Page.FindControl("BtnCopy") as Button;

but the control is there in the markup.

<asp:Button ID="BtnCopy" runat="server" 

the entire method is called in the application page load. Its a modal dialog from acustom action

protected void Page_Load(object sender, EventArgs e)
        {
            Logger.LogDebug("CopyRequestToAnotherYear", "Page_Load(object sender, EventArgs e)", "BEGIN");
            string messageSource = Request.Url.ToString();
            string listId = Request.QueryString["ListID"];
            int id = Convert.ToInt32(Request.QueryString["ID"]);
            string state = Request.GetQueryStringValue(SponsoringCommon.Constants.QUERYSTRINGPARAMETER_STATE_NAME);
            string statusMessage = Request.GetQueryStringValue(SponsoringCommon.Constants.QUERYSTRINGPARAMETER_MESSAGE_NAME);
            LoadNextYears(int.Parse(SPContext.Current.Web.Name));
        }


private void LoadNextYears(int currentYear)
    {
        DdlYear.Items.Clear();
        bool yearsExist = false;
        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            SPWeb currentWeb = SPContext.Current.Web;                    
            using (SPSite site = new SPSite(currentWeb.Url))
            {
                foreach (SPWeb web in site.AllWebs)
                {
                    if (web.WebTemplate == "NLSPONSORINGYEAR" &&  int.Parse(web.Title) > currentYear)
                    {
                        DdlYear.Items.Add(new ListItem(web.Name, web.Name));
                        yearsExist = true;
                    }
                }
            }                
        });

        Button BtnCopy = Page.FindControl("BtnCopy") as Button;
        if (yearsExist)
            BtnCopy.Enabled = true;
        else
            BtnCopy.Enabled = false;
    }




<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
<table width="100%">
    <tr>
        <td>Year: </td>
        <td><asp:DropDownList ID="DdlYear" runat="server"></asp:DropDownList></td>
    </tr>
    <tr>
        <td colspan="2">
            <asp:Button ID="BtnCopy" runat="server" OnClick="BtnCopy_Click" Text="<%$Resources:SPNLSponsoring, btnCopyToAnotherYear%>"/>
        </td>
    </tr>
</table>
</asp:Content>
share|improve this question
    
Exactly in which event you are doing Page.FindControl, may be the control is not rendered yet, are you using some other event than CreateChildControl –  Diptarag Dec 5 '12 at 12:43
    
in the page load, pls see the updated question with more info –  L.V. Sharepoint Architect Dec 5 '12 at 12:45
    
if it helps, the .cs class inherits from : LayoutsPageBase –  L.V. Sharepoint Architect Dec 5 '12 at 12:46
    
FindControl only works for current container, so make sure Button is directly under page, can you post the front end also? –  Diptarag Dec 5 '12 at 12:50
    
its inside a content placeholder, I pasted the frontend, not all, but does it help? –  L.V. Sharepoint Architect Dec 5 '12 at 12:53

2 Answers 2

up vote 2 down vote accepted

Try this -

ContentPlaceHolder content = Page.Master.FindControl("PlaceHolderMain") as ContentPlaceHolder;
Button BtnCopy = content.FindControl("BtnCopy") as Button;

Have not tested the code, but this is the only solution that came into my mind

share|improve this answer

My question is why you can grab the DropDownList without FindControl, but not the button. They are at the same level and both asp controls.

   `DdlYear.Items.Clear();`

Try using the BtnCopy instance.

share|improve this answer
    
the page is in the sharepoint solution, but the code behind is in a different class library., I am not actually grabing the dropdownlist, its an instance of the same class, I guess if I do findcontrol it wont work either –  L.V. Sharepoint Architect Dec 5 '12 at 13:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.