I have a ModalPopupExtender(mpe) which will show when a asp:button is clicked. The button calls a javascript function which then calls a webmethod. The webmethod is in the code behind of the page.
The webmethod passes a string variable to another method in the code behind. which determines what the mpe does. The idea behind this logic is that when you click the button I want to enable certain parts of the mpe and disable other parts. This is because the mpe has several functionalities.
Aspx Button and UserControl
<uc2:IMG ID="IMG1" runat="server" />
<asp:Button ID="btnAddNewImg" runat="server" Text="Add New Image" onclientclick="ShowImgPopupScreen()" />
Javascript function
<script type="text/javascript">
function ShowImgPopupScreen() {
// call server side method
PageMethods.AddNewImg();
}
</script>
ASPX.CS code
[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public static void AddNewImg()
{
string option = "add";
image_loader_cms cms = new image_loader_cms();
cms.SetButtons(option);
}
protected void SetButtons(string add)
{
if (add == "add")
{
IMG1.Add = true;
}
else
{
IMG1.Edit = true;
}
}
ASCX.CS
public bool Add
{
get
{
return btnAdd.Enabled;
}
set
{
btnAdd.Enabled = value;
}
}
public bool Edit
{
get
{
return btnUpdate.Enabled;
}
set
{
btnUpdate.Enabled = value;
}
}
The code steps into IMG1.Add = true; when using breakpoints but after that line of code I get this error message.
Microsoft JScript runtime error: Sys.Net.WebServiceFailedException: The server method 'AddNewImg' failed with the following error:
System.NullReferenceException-- Object reference not set to an instance of an object.
This problem has me scratching my head.
You are creating a new usercontrol, however it wont create its child controls or be in anyway usable until it is added to the control tree, hence your image and buttons are never created.
Since you are in static page method, i dont think this is going to work.
What are you actually trying to do with the user control from the page moethod ?