我正在搜索谷歌,因为有任何方法可以在运行时在我的页面中添加任何方法。我从stackoverflow获得了一个链接....这是expando对象。
我不熟悉expando对象。这里是我得到的一小段代码
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();
}
}
}
根据我的情况,我需要在下面添加例程 在许多aspx页面中。
[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页面中添加一些技术,这将在托管该特定ascx的所有aspx页面中添加上述方法。请帮助我实现它。感谢
答案 0 :(得分: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;
}
}