首页 \ 问答 \ 从ascx动态添加aspx页面中的静态方法(Dynamically add static method in aspx page from ascx)

从ascx动态添加aspx页面中的静态方法(Dynamically add static method in aspx page from ascx)

我正在搜索谷歌是否有任何方法在运行时在我的页面中添加任何方法。 我从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页面中添加上述方法。 请帮助我实现它。 谢谢


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


原文:https://stackoverflow.com/questions/11185876
更新时间:2023-12-09 21:12

最满意答案

答案是您的应用程序(在REST场景中)根本不会跟踪发生的情况。 所有状态由客户端管理,状态转换通过URI导航实现。 REST的“状态转移”部分是指客户端导航到新的状态的新URI。

根据HTTP规范和REST方法,使用GET访问的URI实际上是只读操作。 这意味着如果客户端“备份”到之前的某个URI,那么发生的“最糟糕的”是另一个GET和更多的数据被加载。

假设客户端执行此操作(使用高度简化的伪HTTP)...

获得//site.com/product/123

这将检索关于产品ID 123的信息(或可能是页面),其可能包括对URI的引用,该URI可用于将该项目发布到用户的购物车中。 所以用户决定购买该物品。 再一次,它过于简单化了,但是:

POST //site.com/shoppingcart/ {productid = 123}

从中得到的回报可能是购物车的表示,也可能是对添加项目的引用(可以在DELETE的shoppingcart URI上使用,以便再次移除该项目)或其他各种事物(如更深入的XML描述购物车内容与其他URI指向购物车项目并返回到原始产品)。 全取决于你。

但“状态”是由客户正在做的任何事情来定义的。 它根本不在服务器上进行跟踪,尽管您肯定会在您的数据库中保留一段时间内他的购物车内容的副本。 (两年后,我曾回到网站,我的购物车项目仍然存在......)但是,他需要跟踪该ID。 对于您的服务器应用程序,这只是另一个记录,可能是某种期限。

这样你就不需要cookies了,javascript完全依赖于客户端的实现。 在没有脚本的情况下构建一个不错的REST客户端很困难 - 你可能用XSLT构建一些东西并且只从服务器返回XML,但这可能比任何人都需要的更痛苦。

起点是要真正了解REST,然后设计系统,然后构建它。 它绝对不适合像大多数其他系统那样(正确或错误)在运行中构建它。

这是一篇优秀的文章,它让你在REST中看起来相当“纯粹”,而不会太抽象,也不会让代码陷入困境:

http://www.infoq.com/articles/subbu-allamaraju-rest


The answer is that your application (in a REST scenario) simply doesn't keep track of what happens. All state is managed by the client, and state transitions are effected through URI navigation. The "State Transfer" part of REST refers to client navigation to new URIs which are new states.

A URI accessed with GET is effectively a read-only operation as per both the HTTP spec and the REST methodology. That means if the client "backs up" to some previous URI, "the worst" that happens is another GET is made and more data is loaded.

Let's say the client does this (using highly simplified pseudo-HTTP)...

GET //site.com/product/123

This retrieves information (or maybe a page) about product ID 123, which presumably includes a reference to a URI which can be used to POST that item into the user's shopping cart. So the user decides to buy the item. Again, it's oversimplified but:

POST //site.com/shoppingcart/ {productid = 123}

The return from this might be a representation of the shopping cart, or a reference to the added item (which could be used on the shoppingcart URI with DELETE to remove the item again), or a variety of other things (such as deeper XML describing the cart contents with other URIs pointing to the cart items and back to the original products). It's all up to you.

But the "state" is defined by whatever the client is doing. It isn't tracked on the server at all, although you will certainly keep a copy of his shopping cart contents in your database for some period of time. (I once returned to a website two years later and my shopping cart items were still there...) But it's up to him to keep track of the ID. To your server app it's just another record, presumably with some kind of expiration.

In this way you don't need cookies, and javascript is entirely dependent on the client implementation. It's difficult to build a decent REST client without script -- you could probably build something with XSLT and only return XML from the server, but that's probably more pain than anyone needs.

The starting point is to really understand REST, then design the system, then build it. It definitely doesn't lend itself to building it on the fly like most other systems do (right or wrong).

This is an excellent article that gives you a fairly "pure" look at REST without getting too abstract and without bogging you down with code:

http://www.infoq.com/articles/subbu-allamaraju-rest

相关问答

更多
  • 调用CountDownTimer.cancel(): http://developer.android.com/reference/android/os/CountDownTimer.html#cancel() 在创建期间存储对活动中当前CountDownTimer实例的引用,以便在Activity中访问它。 Call CountDownTimer.cancel(): http://developer.android.com/reference/android/os/CountDownTimer.html# ...
  • 使用“splashscreens”,您可以通过在AndroidManifest.xml文件的相关< activity >条目中将android:noHistory属性设置为“ true ”将其从“back-stack”中删除。 例如: With "splashscreens" you can delete them from the "back-stack ...
  • 您正在调用ReaderActivity ,从那时起您正在调用intent to View pdf的intent to View 。 现在当你从pdf阅读回来 ..你回到读者活动 ,你没有任何布局设置所以你看到黑屏.. 第一件事您应该直接从主要活动中调用视图意图 。 但无论如何你创建了EXTRA活动来执行此操作。因此,您必须尽快删除该ReaderActivity,以便您可以通过两种方式实现这一点。 1) Intent it = new Intent(this, ReaderActivity.class); ...
  • 播放所选/当前歌曲 func play(at index: Int) { audioPlayer.removeAllItems() playerItem = audioItems[index] playerItem?.seek(to: kCMTimeZero) audioPlayer.insert(playerItem!, after: nil) audioPlayer.play() isPlaying = true self.playAllSong ...
  • 作为保罗答案的修正案:你当然只需要跟踪这样的状态,如果你丢失用户输入,例如因为你添加了返回之后不再存在的输入元素。 如果您只使用HTML中“静态”输入元素中的用户数据重建DOM状态,那么您“只需”另外触发您的函数“onload”(或准备好DOM)。 比如说,根据复选框显示/隐藏div:
    ...
    然后将事件处理程序( sh ...
  • 结束使用此。 覆盖后退按钮单击事件。