There is an aspx page, html code is generated by clicking on the button with the help of LiteralControl. A certain number of buttons (input) is created depending on the number of records in the database. Clicking on a certain input calls the Test server method using PageMethods. Here is the method:

[WebMethod] [System.Web.Script.Services.ScriptMethod]

public static void Test(string ic1) { new main().Label1.Text = "123"; //main - это имя класса страницы } 

It is executed, but it throws an exception "The link to the object does not indicate an object instance". These components are placed in UpdatePanel so that the page is not updated ... The difficulty is that you cannot use response.redirect with a get request, since I need the page not to reload ... So I have to use PageMethods. What is the problem? Why is Label1 unavailable or from a static method in this case it needs to be called differently (the main class is non-static)? The question is how to refer to a non-static property of the same class from a static method or is it impossible ?? Thank you in advance!

    1 answer 1

    In static methods, there is no access to the class instance members and you will not be able to implement your plans in this way. You somehow made the label static on the page (it seems to be NULL at the time of the call), and even if you manage to add it, the browser page doesn’t know anything about it and nothing will happen.

    Working with web methods is usually approximately the following: you call the web method from javascript, in it you get the result from the server and after that with the help of the same javascript update the necessary elements of the DOM of the page model.

    But since you use the update panel, there is another simpler option: put all the content planned for the update into the panel, add the button to the same button and create a server handler for the button, in which you will have access to the page elements in the usual mode and the page will be to update as you planned without additional javascript, web methods and reloads - this is, in general, the main feature of the update panel. The button does not have to be placed in the panel, but in order for everything to still work, you must add a trigger to the panel about the event of the "click" button.

    UP: Firstly, it is necessary to dynamically add controls to the page in the Init event BEFORE loading the viewpoint. Secondly, if your first button is server-side and has a server handler, it is also placed on the update panel - when handling an Ajax request from it, the Load event is triggered, but the server button handler is already AFTER load and therefore it is too late to create other buttons. This can be done, but you need to think about some parameters of the event, etc., so that you can already define on Init that you need to generate more server buttons and then everything will work. But, as I wrote above several times, to implement all this in the client code, the update panel is not needed. You make a request to the web method (for example, it returns the number of buttons that you need to create) and in javascript create the client buttons! That is, you have two ways of development of events - select one and use.

    • You did not quite understand me ... the fact is that initially I do not have a button that calls this static method! It is generated when you click on another button, and when you click on it with js, the server static method Test is called ... so initially I don’t have a server handler for the button .. - lider112
    • Why can't you generate a handler for it when generating a new button? - wind
    • You have chosen an implementation in which you do all the manipulations with the page on the client in javascript. Since this is the case, then you need to continue to do so further, namely, as I wrote above on working with web methods: Ie, from the server, only receive some information and modify the DOM by a script. In this case, you and the update panel is not needed. - wind
    • The fact is that I do not generate server buttons, but client buttons .. therefore, I have to screw the server method to the button with js and ajax, but I cannot create dynamic server buttons, since they must be created in Page_Load for event handlers to work, and for me Page_Load does not occur, because I use ajax to prevent the page from being completely updated ... can you tell me how to generate a server component from the code and bind a handler to it, but without Page_Load ? - lider112