2015-02-09 67 views
3

如何獲取Page.UICulture中的staticWebMethod函數。下面給我一個錯誤:如何在靜態WebMethod中獲取Page.UICulture?

Page.UICulture = System.Threading.Thread.CurrentThread.CurrentCulture; 

的錯誤,我得到:

An object reference is required for the non static field,method or property

回答

3

簡單,你不能。

該屬性Page不適用於static方法,這是有道理的,因爲在該靜態WebMethod的上下文中,您沒有頁面。

您可以將相關屬性保存在Session變量中,並從您的WebMethod中的會話信息中獲取。請注意,您必須設置[WebMethod(EnableSession=true)]才能從WebMethod獲取會話信息。

+0

2 Page'UICulture = System.Threading.Thread.CurrentThread.CurrentCulture;'導致編譯錯誤或運行時異常? – 2015-02-09 11:42:51

+0

@JenishRabadiya:通常編譯時。它在代碼中,而不是在標記中。這將使編譯時間被檢查。 – 2015-02-09 11:44:06

-2

請嘗試下面的代碼。我不嘗試,但它可能對你有幫助。

public static Page obj; 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     obj = this.Page; 
    } 


    public static void function1() 
    { 
     obj.UICulture = System.Threading.Thread.CurrentThread.CurrentCulture.ToString(); 
    } 
+0

可能工作,但它是超級討厭的。 – DavidG 2015-02-09 11:27:06

+1

ASP.NET中的'static'變量非常危險,因爲它們在所有會話中共享,所以你真的不想使用'static'。 – 2015-02-09 11:28:08

+0

@DavidG:...而且非常危險。 – 2015-02-09 11:28:32

相關問題