简体   繁体   English

“ System.NullReferenceException-对象引用未设置为对象的实例。” Web方法,用户控件

[英]“System.NullReferenceException— Object reference not set to an instance of an object.” Webmethod, User control

I have a ModalPopupExtender(mpe) which will show when a asp:button is clicked. 我有一个ModalPopupExtender(mpe),它将在单击asp:button时显示。 The button calls a javascript function which then calls a webmethod. 该按钮调用一个javascript函数,然后调用一个web方法。 The webmethod is in the code behind of the page. Web方法在页面后面的代码中。

The webmethod passes a string variable to another method in the code behind. Web方法将字符串变量传递给后面代码中的另一个方法。 which determines what the mpe does. 这决定了mpe的作用。 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. 这种逻辑的思想是,当您单击按钮时,我想启用mpe的某些部分并禁用其他部分。 This is because the mpe has several functionalities. 这是因为mpe具有多个功能。

Aspx Button and UserControl Aspx按钮和UserControl

 <uc2:IMG ID="IMG1" runat="server" />
 <asp:Button ID="btnAddNewImg" runat="server" Text="Add New Image" onclientclick="ShowImgPopupScreen()"  />

Javascript function Javascript功能

  <script type="text/javascript">
     function ShowImgPopupScreen() {
         // call server side method
         PageMethods.AddNewImg();
     }   
</script>

ASPX.CS code ASPX.CS代码

[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 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; 代码进入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: Microsoft JScript运行时错误: Sys.Net.WebServiceFailedException: The server method 'AddNewImg'失败,出现以下错误:

 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 ? 您实际上想从页面方法中使用用户控件做什么?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.NullReferenceException:“对象引用未设置为 object 的实例。” 问题 - System.NullReferenceException: „Object reference not set to an instance of an object.” problem 会话{“对象引用未设置为对象的实例。”} System.Exception {System.NullReferenceException} - session {“Object reference not set to an instance of an object.”} System.Exception {System.NullReferenceException} System.NullReferenceException:对象引用未设置为对象的实例。 抛出错误 - System.NullReferenceException: Object reference not set to an instance of an object. throwing error System.NullReferenceException:未将对象引用设置为对象的实例。 在测试自动化执行期间 - System.NullReferenceException: Object reference not set to an instance of an object. During Test automation execution System.NullReferenceException未将对象引用设置为对象的实例。 关于模型实例化 - 有时候 - System.NullReferenceException Object reference not set to an instance of an object. on Model instantiation - SOMETIMES MVC:通过SelectListItem,我得到“ System.NullReferenceException:对象引用未设置为对象的实例。”错误 - MVC: By SelectListItem I get “System.NullReferenceException: Object reference not set to an instance of an object.” error 获取 System.NullReferenceException:“对象引用未设置为 object 的实例。” 调用服务时 - getting System.NullReferenceException: 'Object reference not set to an instance of an object.' when calling a service 编辑 System.NullReferenceException 时使用 DataTable 的 Datagrid 绑定:“未将对象引用设置为对象的实例。” - Datagrid binding using DataTable on edit System.NullReferenceException: 'Object reference not set to an instance of an object.' C# Npgsql 连接检查器异常抛出 System.NullReferenceException '对象引用未设置为 object 的实例。 - C# Npgsql connection checker Exception Throw System.NullReferenceException 'Object reference not set to an instance of an object.' System.NullReferenceException-对象引用未设置为对象的实例。 C#中的原因 - System.NullReferenceException - Object reference not set to an instance of an object. Causes in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM