2015-10-06 35 views
-2

內部靜態方法很顯着得到隱藏字段值內控制值:如何找到靜態方法

private static void GetDetails() 
{ 
    Page page = (Page)HttpContext.Current.Handler; 
    HiddenField hdnUserID = (HiddenField)page.FindControl("hdnUserID"); 
    txtAccreditation.Text = hdnUserID.Value ; // here hdnUserID.Value is null why ?? 
} 
+0

只是想什麼:http://stackoverflow.com/questions/241339/when-to-use-static-classes-in-c-尖銳; – Schuere

回答

0

嘗試這樣

Page page = (Page)HttpContext.Current.Handler; 
    TextBox TextBox1 = (TextBox)page.FindControl("TextBox1"); 

    TextBox TextBox2 = (TextBox)page.FindControl("TextBox2"); 
+1

我已經嘗試過,但它的空值爲 – Muthu

+0

yes其空值:( –

0

或許你需要更改您的代碼是這樣的:爲遵循

更改代碼

private static void GetDetails(HiddenField field) 
{  
    txtAccreditation.Text = field.Value ; 
} 

,並調用它

Page page = (Page)HttpContext.Current.Handler; 
HiddenField hdnUserID = (HiddenField)page.FindControl("hdnUserID"); 
YourClassName.GetDetails(hdnUserId); 

我不明白的是爲什麼你會稱它爲通過靜態操作。

我會假設你多創造一些類似下面的代碼:

private static string GetDetails(HiddenField field) 
{  
    return field.Value ; 
} 

這將導致下面的代碼:此代碼將不會出現在靜態函數內部。

Page page = (Page)HttpContext.Current.Handler; 
HiddenField hdnUserID = (HiddenField)page.FindControl("hdnUserID"); 
txtAccreditation.Text = YourClassName.GetDetails(hdnUserId); 

這意味着你可以實際使用的代碼在其他地方