0

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.

2

1 Answer 1

1

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 ?

Sign up to request clarification or add additional context in comments.

4 Comments

From the page method I want to enable an add button and disable an edit button based on the user control based on what variable I pass. Our system uses user controls which is why I also using them. How would I create child controls?
If you are using ajax then your user control is already rendered and this approach will not work. You should get the result indicating if the add button is enabled or not via the ajax call but then use client script to enable / disable the button itself. Or use an update panel.
I will try the update panel and see how it works. Thanks for the suggestion.
I ended up using an update panel and partial postback to modify the properties of buttons used on a modalpopupextender user control.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.