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);

private void LoadNextYears(int currentYear)
        bool yearsExist = false;
            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;
            BtnCopy.Enabled = false;

<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
<table width="100%">
        <td>Year: </td>
        <td><asp:DropDownList ID="DdlYear" runat="server"></asp:DropDownList></td>
        <td colspan="2">
            <asp:Button ID="BtnCopy" runat="server" OnClick="BtnCopy_Click" Text="<%$Resources:SPNLSponsoring, btnCopyToAnotherYear%>"/>
  • 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 – Luis Valencia Dec 5 '12 at 12:45
  • if it helps, the .cs class inherits from : LayoutsPageBase – Luis Valencia 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? – Luis Valencia Dec 5 '12 at 12:53

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


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.


Try using the BtnCopy instance.

  • 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 – Luis Valencia Dec 5 '12 at 13:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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