Domanda

i was searching google for that is there any way to add any method in my page at run time. i got a link from stackoverflow for this....that is expando object.

i am not familiar with expando object. here is little snippet of code i got and like

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();
}
}
}

according to my situation i need to add a routine below like in many aspx page.

[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;
   }

so i need to know that is there any way to add some technique in my ascx page which will add the above method in all the aspx pages which host that particular ascx. please help me to achieve it. thanks

È stato utile?

Soluzione

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;
     }
  }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top