asp.net access a control from static function

2019-01-26 01:30发布

问题:

I have a webform and in that webform, I am trying to access a Panel object from a static method, but couldn't access it How can I access a Panel object from static method. Why am I trying to access the object from static? Because I am using JQUERY which only accepts static methods.

I tried to store Panel in session in Page_Load() and retrieve it from static method but it didn't work.

回答1:

You are probably using web method in the aspx page to call it from jQuery ajax(). You could not access the controls in web method rather pass the information to your web method by ajax call and return the information from web method to jQuery ajax callback and perform action on panel in javascript jQuery call back function.



回答2:

public static void DoSomething()
{
    if (HttpContext.Current != null)
    {
        Page page = (Page)HttpContext.Current.Handler;
        TextBox TextBox1 = (TextBox)page.FindControl("TextBox1");

        TextBox TextBox2 = (TextBox)page.FindControl("TextBox2");
    }
}