简体   繁体   English

如何在ASP.NET的静态方法中访问ASP控件

[英]How to access asp control inside static method in asp .net

my Method declared like this : 我的方法这样声明:

private static void GetHospitaDetails() 
{
    txtAccreditation.Text = "Test"; // Here am not able to assign value to textbox field.
}
public static void Savedata()
{
    if (HttpContext.Current != null)
    {
        Page page = (Page)HttpContext.Current.Handler;
        TextBox TextBox1 = (TextBox)page.FindControl("TextBox1");

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

Above method is for finding the control values. 上面的方法是寻找控制值。 The whole point of [WebMethod]s is that they don't run the ASP.Net page lifecycle. [WebMethod]的全部要点是它们不运行ASP.Net页面生命周期。 This way, they're fast and parallelizable. 这样,它们既快速又可并行化。 Your controls don't exist. 您的控件不存在。

Instead, you should use Javascript (better) or an UpdatePanel (worse). 相反,您应该使用Javascript(更好)或UpdatePanel(更差)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM