如何从另一个类调用 Default.aspx.cs 中定义的方法
我对 ASP.NET 相当陌生,我正在尝试创建网站。我在 AppCode/GSA 中有一个类,我需要在其中访问 default.aspx.cs 中声明的事件处理程序。有人可以帮忙吗? 我尝试搜索它,但我得到的只是创建该类的实例后的标准方法调用,例如 classname.methodname() 。请帮我一下。
这就是我想要做的以及我需要调用 default.aspx.cs 中的方法的原因
我正在调用 GSA 函数来制作 html 字符串,我用它来在占位符中显示搜索结果。它工作得很好。现在我在显示的每个搜索前面添加按钮。当用户单击此按钮时,该按钮应替换为文本字段,并且它将包含该搜索结果的小 url。
现在我得到了显示每个结果的按钮,我必须为该按钮分配事件处理程序。我尝试将该事件处理程序放入 GSA.cs 文件本身,但该函数不会被调用,因为默认情况下未定义它,这就是为什么我决定将该事件处理程序放入 defalut.aspx.cs
I am fairly new to asp.net and I am trying to create website. I have a class in AppCode/GSA in which I need to access event handler declared in default.aspx.cs. Can anyone help with it?
I tried searching about it but all I got is standard method calls like classname.methodname() after creating an instance of that class. Please help me out here.
here is what I want to do and the reason why I need to call the method in default.aspx.cs
I am calling GSA functions to make html string which I am using to display search results in place holder. It works perfectly fine. Now I am adding button in front of every search displayed. When user clicks this button that button should be replaced by textfield and it will contain tiny url for that search result.
Now I am getting button with every result displayed, I have to assign event handler to that button. I tried putting that event handler in GSA.cs file itself but that function does not get called as it is not defined in default that why i decided to put that event handler in defalut.aspx.cs
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一般来说,事件处理程序不应该在类之间共享。如果您需要在两个地方使用相同的功能,则将该功能提取到其自己的方法中,并在事件处理程序中调用该方法:
应该变成:
“我在哪里放置
DoComplexFunctionality()?”是一个只有知道其具体用途的人才能有意义地回答的问题。您可以使其成为静态类的成员,这将使任何可以看到该类的代码都可以访问它。或者,如果它负责改变一组特定的值,您可以将其作为封装这些值的类的实例成员,并将该类的实例传递给需要调用该方法的任何人。
无论如何,尝试直接调用事件处理程序都不是正确的方法。
Generally speaking, event handlers aren't the sort of thing that should be shared between classes. If you need to use the same functionality in two places, then pull that functionality out into its own method and invoke that method in your event handlers:
Should become:
The question of "where do I put
DoComplexFunctionality()
?" is one that can only be meaningfully answered by someone who knows the specifics of what it does. You could make it a member of a static class, which would make it accessible to any code that can see that class. Or, if it's responsible for mutating a specific set of values, you could make it an instance member of a class that encapsulates those values, and pass an instance of that class to whoever needs to invoke the method.In any case, trying to directly call an event handler is not the way to go.
您真的不应该从其他代码中调用事件处理程序。
我确实没有足够的信息可以继续,但我可以设想两种场景,它们可能会让您想到应该这样做:公共页面场景和共享业务逻辑场景。
如果您希望在多个页面中使用某些代码块,那么我强烈建议您创建一个从“Page”类派生的父类,并将所有公共代码放在其中。这与主从关系不同;它只是一种实现所有页面都将使用的代码的方法。例如,我使用父类来管理强类型会话类。
如果您希望共享或实现业务逻辑,并且恰好您已经将其放入 Default.aspx.cs 类中,那么您需要重新考虑您的体系结构。即使您没有使用 MVC 方法,我也强烈建议您使用三层方法处理除最琐碎的站点之外的所有站点(一旦完成,额外的工作就微不足道了)。在这种情况下,您将创建实现业务逻辑的类,并且仅将代码隐藏代码用于 UI 工作。因此,典型的代码隐藏方法将从 UI 中提取一些内容,创建业务逻辑类并将其传入,然后处理结果(反之亦然)。在这种情况下,您将能够从任何地方调用实现您想要共享的代码的业务逻辑类 - 包括您的测试类。
You really should simply never be calling an event handler from other code.
I don't really have enough information to go on but I can envision two scenarios that might be giving you the idea that you should do this: a common page scenario and a shared business logic scenario.
If you are hoping to use some block of code in multiple pages, then I would strongly recommend that you create a parent class derived from the "Page" class and place all common code in there. This is NOT the same as a master-slave relationship; it is simply a way to implement code that all of your pages will use. I use my parent class to manage a strongly-typed session class, for example.
If you are looking to share or implement business logic and it just happens that you already put it in the Default.aspx.cs class, then you need to rethink your architecture. Even if you are not using an MVC approach, I would strongly recommend that you approach all but the most trivial of sites with a three-tier approach (once you've done it, additional work is trivial). In this case, you'll create classes that implement your business logic and only use your code-behind code for UI work. So your typical code-behind method will pull some stuff from the UI, create a business logic class and pass it in, and then process the results (or vice versa). In this case, you'll be able to call the business logic class that implements the code you want to share from anywhere - including your testing classes.
将您的 Default.aspx.cs 与所有其他类一起拖到 AppCode 文件夹中,并更改 Default.aspx 中的
CodeFile
路径 到CodeFile="~/App_Code/Default.aspx.cs"
您现在应该能够像访问任何其他类一样访问它。
Drag your Default.aspx.cs to the AppCode folder with all your other classes and change the
CodeFile
path in your Default.aspx toCodeFile="~/App_Code/Default.aspx.cs"
You should now be able to access it like any other class.
这是你想要的吗?
Default.aspx.cs
在 App_Code/Class1.cs 中
更新(OP 请求的代码):
默认.aspx.cs:
Is this what you want?
Default.aspx.cs
In App_Code/Class1.cs
Updated (code requested by the OP):
Default.aspx.cs: