2012-06-25 38 views
1

我正在谷歌搜索,有沒有什麼辦法可以在運行時在我的頁面中添加任何方法。我從stackoverflow獲得了一個鏈接,這是expando對象。從ascx動態添加靜態方法在aspx頁

我不熟悉expando對象。這裏是根據我的情況,我需要在許多aspx頁面添加例行下面像 的代碼小片段我有和喜歡

namespace DynamicDemo 
{ 
class ExpandoFun 
{ 
public static void Main() 
{ 
    Console.WriteLine("Fun with Expandos..."); 
    dynamic student = new ExpandoObject(); 
    student.FirstName = "John"; 
    student.LastName = "Doe"; 

student.Introduction=new Action(()=> 
Console.WriteLine("Hello my name is {0} {1}",student.FirstName,student.LastName); 
); 

); 
    Console.WriteLine(student.FirstName); 
    student.Introduction(); 
} 
} 
} 

[WebMethod] 
    public static string LoadData(string CountryCode,int PageIndex) 
    { 
     string strData = ""; 
     if (CountryCode != "") 
     { 
      strData = Left1.HavingCountry(CountryCode, PageIndex); 
     } 
     else 
     { 
      strData = Left1.WithoutCountry(PageIndex); 
     } 
     return strData; 
    } 

,所以我需要知道,有沒有什麼辦法來加入我的ascx頁面的一些技術將在所有的aspx頁面的主機特定的ascx添加上述方法。請幫我實現它。謝謝

回答

0
I don't think it is possible. 
Can you do this way 

1. create a `class` which extents `System.Web.UI.Page`. 
2. write you `WebMethod` in that class. 
3. Create your aspx pages by extending this class 

public partial class _Default : TestUserControl 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
    } 
} 

public class TestUserControl : System.Web.UI.Page 
{ 
    [System.Web.Services.WebMethod] 
    public static string LoadData(string CountryCode, int PageIndex) 
    { 
     string strData = ""; 
     if (CountryCode != "") 
     { 
      strData = Left1.HavingCountry(CountryCode, PageIndex); 
     } 
     else 
     { 
      strData = Left1.WithoutCountry(PageIndex); 
     } 
     return strData; 
    } 
    }