2012-06-25 43 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; 
    } 
    }