C# – To get controls inside static function

asp.netc

I am calling function in codebehind from javascript using webservice.

function  GetAdmissionType()
        {
            InitComponents();
            var type="";
            type=document.getElementById(dlAdmissionType.id).value;
             document.getElementById(hdnAdmissionType.id).value=document.getElementById(dlAdmissionType.id).value;

             else if(type=="2")
             {
                   InitComponents();
                   ViewResettingPanel()
                   makeFavorite(1);

             }
    }



       function makeFavorite(id) {
    PageMethods.SaveInfo(id, CallSuccess, CallFailed);
}

// This will be Called on success
function CallSuccess(res, id) {
alert(destCtrl);
}

// This will be Called on failure
function CallFailed(res) {
alert(res.get_message());
}

Following is my code in codebehind

[System.Web.Services.WebMethod]
    public static void SaveInfo(String Id)
    {

       //to get textbox in form
    }

Problem is iam not getting controls in aspx page in SaveInfo.Can anybody help to access controls in form inside saveinfo?

Best Answer

Static page methods cannot get the page's control tree. (They don't receive ViewState)

You will need to use an UpdatePanel.

You can make an asp:Button inside a <div style="display:none> with a regular Click event, make an UpdatePanel triggered by the button, and use Javascript to simulate a click of the button.

Alternatively, you could send the values of the controls that you need as parameters to your page method in Javascript. This will be more efficient than using an UpdatePanel.

Related Topic