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>