最近很多小伙伴都在问c#–使用与pagemethod和webmethod相同的datatable的区别和webpage和webform这两个问题,那么本篇文章就来给大家详细解答一下,同时本文还将给你拓
最近很多小伙伴都在问c# – 使用与pagemethod和webmethod相同的datatable的区别和webpage和webform这两个问题,那么本篇文章就来给大家详细解答一下,同时本文还将给你拓展@staticmethod 和 @classmethod 之间的区别、@staticmethod和@classmethod的作用与区别、asp.net – WebMethod未被Visual Studio 2013中的PageMethod调用(触发)、classmethod和instancemethod的名称相同吗?等相关知识,下面开始了哦!
本文目录一览:- c# – 使用与pagemethod和webmethod相同的datatable的区别(webpage和webform)
- @staticmethod 和 @classmethod 之间的区别
- @staticmethod和@classmethod的作用与区别
- asp.net – WebMethod未被Visual Studio 2013中的PageMethod调用(触发)
- classmethod和instancemethod的名称相同吗?
c# – 使用与pagemethod和webmethod相同的datatable的区别(webpage和webform)
Gridviews绑定在pageload事件上,并且使用jquery-ajax方法(使用amcharts以及highcharts)通过调用WebMethod来显示图表.
最初我实现了这样一种方式,即在执行存储过程gridview绑定后,在webmethod内部也执行相同的sp来绘制图表.同样的sp对该页面执行两次(一个用于网格,另一个用于图表).10 sps需要执行网格和图表10.
所以为了提高页面性能,我创建了这样的静态datatable
static DataTable Report1;
并像这样绑定gridview.
private void gvbindReport1() { try { Report1 = new DataTable();//refreshed datatable DataSet ReportDS1 = objmvbl.GetReportGraph(ClientID,date_From,date_To); if (ReportDS1.Tables.Count > 0) { Report1 = ReportDS1.Tables[0];//bindinding data to static datatable } GdReport.DataSource = Report1; GdReport.DataBind(); } catch (Exception ex) { Log.Errlog("Error Occured in gvbindReport1 : " + ex.Message.ToString()); } }
并且在webmethod内部,我已经使用相同的datatable来绘制图表
喜欢这个
[System.Web.Services.WebMethod] public static string GetDataReport1() { System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer(); List<Dictionary<string,object>> rows = new List<Dictionary<string,object>>(); Dictionary<string,object> row; try { //processing for the data inside static datatable if (Report1.Rows.Count > 0) { foreach (DaTarow dr in Report1.Rows) { row = new Dictionary<string,object>(); foreach (DataColumn col in Report1.Columns) { row.Add(col.ColumnName,dr[col]); } rows.Add(row); } } } catch (Exception ex) { Log.Errlog("Error Occured in GetDataReport WebMethod of Report Page : " + ex.Message.ToString()); } return serializer.Serialize(rows); }
与此我可以显示网格和图表.
现在请说说,这是处理webmethods的正确方法吗?我已经读过该webmethod与该页面和所有的没有任何关系.请告诉我这个方法的回退.
如果这是错误的方法,请提供一些改进页面性能的想法?
解决方法
users will get the same result (last updated values).
您可以在concurrency测试中实现.
请检查以下情况:
考虑dtbl是在主页上初始化的静态dataTable,并在索引页面上创建另一个“datatable”实例实例(两者都在页面加载中,如下所示).
家
public static DataTable dtbl; protected void Page_Load(object sender,EventArgs e) { if (!Page.IsPostBack) { dtbl = new DataTable(); dtbl.Columns.Add("id"); dtbl.Columns.Add("name"); for (int i = 0; i < 10; i++) { DaTarow dr = dtbl.NewRow(); dr["id"] = i.ToString(); dr["name"] = i + 1; dtbl.Rows.Add(dr); } } }
索引页
protected void Page_Load(object sender,EventArgs e) { if (!Page.IsPostBack) { home.dtbl = new DataTable(); } }
现在在每个页面中放置一个断点并运行应用程序,
>在单独的选项卡中打开这两个页面.
>刷新主页并检查列是否显示
>现在转到下一个选项卡(索引)并刷新它(为dt创建一个新的实例).它将影响datatable,现在你也可以在家里得到新的datatable.
所以如果这两个进程/页面同时被执行,那么这两个页面将获得最新的值.这就是为什么我会在并发测试中认识到这一点.
You can make use of a session in this case. Consider the following code:
家
protected void Page_Load(object sender,EventArgs e) { if (!Page.IsPostBack) { dtbl = new DataTable(); dtbl.Columns.Add("id"); dtbl.Columns.Add("name"); for (int i = 0; i < 10; i++) { DaTarow dr = dtbl.NewRow(); dr["id"] = i.ToString(); dr["name"] = i + 1; dtbl.Rows.Add(dr); } if (((DataTable)Session["MyDatatable"]).Columns.Count < 0) { Session["MyDatatable"] = dtbl; } else { dtbl = (DataTable)Session["MyDatatable"]; } } }
@staticmethod 和 @classmethod 之间的区别
@staticmethod用 装饰的函数和用 装饰的函数有什么区别@classmethod?
@staticmethod和@classmethod的作用与区别
一般来说,要使用某个类的方法,需要先实例化一个对象再调用方法。
而使用@staticmethod或@classmethod,就可以不需要实例化,直接类名.方法名()来调用。
这有利于组织代码,把某些应该属于某个类的函数给放到那个类里去,同时有利于命名空间的整洁。
既然@staticmethod和@classmethod都可以直接类名.方法名()来调用,那他们有什么区别呢
从它们的使用上来看,- @staticmethod不需要表示自身对象的self和自身类的cls参数,就跟使用函数一样。
- @classmethod也不需要self参数,但第一个参数需要是表示自身类的cls参数。
如果在@staticmethod中要调用到这个类的一些属性方法,只能直接类名.属性名或类名.方法名。
而@classmethod因为持有cls参数,可以来调用类的属性,类的方法,实例化对象等,避免硬编码。
下面上代码。
- class A(object):
- bar = 1
- def foo(self):
- print(''foo'')
- @staticmethod
- def static_foo():
- print(''static_foo'')
- print(A.bar)
- @classmethod
- def class_foo(cls):
- print(''class_foo'')
- print(cls.bar)
- cls().foo()
- A.static_foo()
- A.class_foo()
static_foo
1
class_foo
1
foo
asp.net – WebMethod未被Visual Studio 2013中的PageMethod调用(触发)
WebMethod的调用不是在Visual Studio 2013(ASP.NET WebForms Application)中创建的项目上完成的.如果我创建一个项目,例如,在Visual Studio 2008中并迁移到VS 2013正常工作.只有在Visual Studio 2013中创建新项目时才会出现此问题.控制台中没有错误消息. WebMethod没有被调用.什么都没发生.我经常搜索但却一无所获.
ASPX代码:
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="TestePageMethods._Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" EnablePageMethods="true" runat="server" /> <script type="text/javascript"> function testeClick() { PageMethods.SayHello("Name"); } </script> <input type="button" value="Say Hello" onclick="testeClick();" /> </form> </body> </html>
ASPX.VB代码:
Partial Public Class _Default Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object,ByVal e As System.EventArgs) Handles Me.Load End Sub <System.Web.Services.WebMethod(True)> _ Public Shared Function SayHello(ByVal name As String) As String Return "Hello " & name End Function End Class
有没有人试过这个或知道解决方案?我不知道该怎么做 …
编辑:
伙计们,我现在又发现了一条信息:
仅在VS2013中使用:
-新项目.
-Web – ASP.NET Web应用程序.
– 选择模板“空”.
– 插入页面“Default.aspx”,WebMethod正常工作……
现在,如果您创建一个新项目并选择模板“WebForms”,则不起作用…
可以交叉引用吗?还是一些不同的设置?
解决方法
“System.Web.Optimization”和“Microsoft.AspNet.Web.Optimization.WebForms”
还需要删除web.config,如下所示:
<namespaces> <add namespace="System.Web.Optimization" /> </namespaces> <add assembly="Microsoft.AspNet.Web.Optimization.WebForms" namespace="Microsoft.AspNet.Web.Optimization.WebForms" tagPrefix="webopt" /> <dependentAssembly> <assemblyIdentity name="WebGrease" culture="neutral" publicKeyToken="31bf3856ad364e35" /> <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" /> </dependentAssembly>
一切都好吧!感谢所有帮助我解决问题的人!
总结
以上是小编为你收集整理的asp.net – WebMethod未被Visual Studio 2013中的PageMethod调用(触发)全部内容。
如果觉得小编网站内容还不错,欢迎将小编网站推荐给好友。
classmethod和instancemethod的名称相同吗?
我想做这样的事情:
class X: @classmethod def id(cls): return cls.__name__ def id(self): return self.__class__.__name__
现在调用id()
类或它的实例:
>>> X.id()''X''>>> X().id()''X''
显然,此确切的代码不起作用,但是是否有类似的方法可以使它起作用?还是任何其他解决方法都可以在没有太多“怪异”内容的情况下获得这种行为?
答案1
小编典典类和实例方法位于同一个命名空间中,并且您不能像这样重用名称。id
在这种情况下,最后的定义将获胜。
类方法将继续在实例上运行,但是, 无需 创建单独的实例方法。只需使用:
class X: @classmethod def id(cls): return cls.__name__
因为方法继续绑定到该类:
>>> class X:... @classmethod... def id(cls):... return cls.__name__... >>> X.id()''X''>>> X().id()''X''
明确记录了以下内容:
可以在类(如
C.f()
)或实例(如C().f()
)上调用它。该实例除其类外均被忽略。
如果确实需要区分绑定到类和实例
如果您需要一种方法,使其根据使用的位置而有所不同;在类上访问时绑定到一个类,在实例上访问时绑定到该实例,您需要创建一个自定义 描述符对象 。
该描述符的API是Python的如何导致功能被绑定为方法,并结合classmethod
对象的类;
请参阅描述符说明。
您可以通过创建具有__get__
方法的对象来为方法提供自己的描述符。这是一个简单的方法,可根据上下文切换方法所绑定的对象,如果to的第一个参数__get__
是None
,则描述符将绑定到一个类,否则它将绑定到一个实例:
class class_or_instancemethod(classmethod): def __get__(self, instance, type_): descr_get = super().__get__ if instance is None else self.__func__.__get__ return descr_get(instance, type_)
这将重用classmethod
并且仅重新定义其处理绑定的方式,将的原始实现委派给instance isNone
,__get__
否则将其委派给标准函数实现。
请注意,在方法本身中,您可能随后必须测试其绑定的对象。isinstance(firstargument, type)
这是一个很好的测试:
>>> class X:... @class_or_instancemethod... def foo(self_or_cls):... if isinstance(self_or_cls, type):... return f"bound to the class, {self_or_cls}"... else:... return f"bound to the instance, {self_or_cls"...>>> X.foo()"bound to the class, <class ''__main__.X''>">>> X().foo()''bound to the instance, <__main__.X object at 0x10ac7d580>''
一种替代实现可以使用 两个 函数,一个函数绑定到类时,另一个函数绑定到实例时:
class hybridmethod: def __init__(self, fclass, finstance=None, doc=None): self.fclass = fclass self.finstance = finstance self.__doc__ = doc or fclass.__doc__ # support use on abstract base classes self.__isabstractmethod__ = bool( getattr(fclass, ''__isabstractmethod__'', False) ) def classmethod(self, fclass): return type(self)(fclass, self.finstance, None) def instancemethod(self, finstance): return type(self)(self.fclass, finstance, self.__doc__) def __get__(self, instance, cls): if instance is None or self.finstance is None: # either bound to the class, or no instance method available return self.fclass.__get__(cls, None) return self.finstance.__get__(instance, cls)
这是带有可选实例方法的类方法。像使用property
对象一样使用它;用以下方法装饰实例方法@<name>.instancemethod
:
>>> class X:... @hybridmethod... def bar(cls):... return f"bound to the class, {cls}"... @bar.instancemethod... def bar(self):... return f"bound to the instance, {self}"... >>> X.bar()"bound to the class, <class ''__main__.X''>">>> X().bar()''bound to the instance, <__main__.X object at 0x10a010f70>''
就个人而言,我的建议是谨慎使用此方法。基于上下文更改行为的完全相同的方法可能会令人困惑。但是,有一些用例,例如SQLAlchemy在SQL对象和SQL值之间的区分,其中模型中的列对象会切换这样的行为。请参阅其_混合属性_文档。其实现遵循与我hybridmethod
上面的类完全相同的模式。
今天关于c# – 使用与pagemethod和webmethod相同的datatable的区别和webpage和webform的介绍到此结束,谢谢您的阅读,有关@staticmethod 和 @classmethod 之间的区别、@staticmethod和@classmethod的作用与区别、asp.net – WebMethod未被Visual Studio 2013中的PageMethod调用(触发)、classmethod和instancemethod的名称相同吗?等更多相关知识的信息可以在本站进行查询。
本文标签: