Asp.Net从不同的* .aspx.cs页面调用函数 [英] Asp.Net call function from a different *.aspx.cs page
问题描述
如果我在一页内有一个按钮,例如main.aspx。点击后,我希望main.aspx.cs中的clicked方法可以从不同的* .aspx.cs页面调用一个函数,但也可以重定向到那个* .aspx页面......这是可能的吗?
你想要做的更具体的例子会很有用。否则,您将得到各种答案,其中许多答案将不符合要求。
您应该将通用代码放在App_Code文件夹中。你也不应该在表单代码隐藏内部有任何业务逻辑。
事实上,你需要一个页面来调用另一个页面中的方法,这表明你没有这样做。页面用于显示和解释操作,但不应包含任何业务逻辑。
例如。购物车的一个蹩脚的例子。
ProductView.aspx - 调用Cart.AddToCart(Product p)
CartView.aspx - 显示购物车中的所有物品,用户
可以更新或移除物品。
- 调用Cart.RemoveItem(int cartIndex)
- 调用Cart.UpdateItem(int cartIndex,int newItemCount)
Cart本身不属于CartView.aspx或ProductView.aspx。它属于〜/ App_Code / Cart.cs
所以你的解决方案可能看起来像
/(web文件夹的根目录)
产品/
ProductView.aspx
ProductView.aspx.cs
Cart /
CartView.aspx
CartView.aspx.cs
App_Code /
Cart.cs
Product.cs
另外,要添加App_Code文件夹(如果该文件夹尚不存在),请右键单击Web项目并选择添加ASP。 NET文件夹
并选择 App_Code
If I have a button inside one page for example "main.aspx". And upon being clicked I want the clicked method in "main.aspx.cs" to call a function from a different *.aspx.cs page but also redirect to that *.aspx page...is that possible?
A more concrete example of what you're trying to do would be useful. Otherwise you'll get all sorts of answers, many of which will be off the mark.
You should put common code in the App_Code folder. You should also not have any business logic inside a forms code-behind.
The fact that you need one page to call a method in another page indicates that you haven't done this. Pages are for displaying and interpreting actions, but they should not hold any of the business logic.
e.g. a lame example of a shopping cart.
ProductView.aspx - calls Cart.AddToCart(Product p) CartView.aspx - displays all items in the cart, and the user can update or remove items. - calls Cart.RemoveItem(int cartIndex) - calls Cart.UpdateItem(int cartIndex, int newItemCount)
Cart itself doesn't belong in either CartView.aspx or ProductView.aspx. It instead belongs in ~/App_Code/Cart.cs
So your solution could look something like
/ (root of your web folder) Product/ ProductView.aspx ProductView.aspx.cs Cart/ CartView.aspx CartView.aspx.cs App_Code/ Cart.cs Product.cs
Also, to add the App_Code folder if it's not already there, right-click the web project and select Add ASP.NET folder
and choose App_Code
这篇关于Asp.Net从不同的* .aspx.cs页面调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!