从aspx.cs文件中的ascx文件调用方法 [英] Calling a method from ascx file in aspx.cs file
问题描述
我想从Default.aspx.cs文件中的ShoppingCartControl.ascx.cs文件(用户控件)中调用一个名为AddProductToCart()的方法.在Default.aspx.cs中,我有以下代码:
using System; using System.Web.UI; public partial class _Default : Page { protected void ProductControl_AddToCartClick(object sender, EventArgs e) { ProductControl product = (ProductControl)sender; }}
其中(Productcontrol)是另一个.ascx.cs文件中的类.
此方法存在于ShoppingCartControl.ascx.cs文件中的ShoppingCartControl类中.
如何在aspx.cs文件中调用此方法?请建议
回复将非常可观!!!!!!!!!!!!!!!!!!
您可以通过以下两种方式之一进行操作:
0)创建一个BasePage类,其中包含多个页面将需要访问的通用方法.
1)创建一个静态类,其中包含所有代码都可以在站点上访问的方法.如果这些方法需要访问特定于页面的变量,则必须将这些变量设置为会话变量,或者将它们作为参数传递给静态方法.
ShoppingCartControl shoppingCartControl = new ShoppingCartControl(); shoppingCartControl.AddProductToCart();
或使用对象的任何扩展实例(如果可用).该方法应根据需要是公共方法或内部方法.
I want to call a method named AddProductToCart() from ShoppingCartControl.ascx.cs file(user control) in Default.aspx.cs file..In Default.aspx.cs,I have the code:
using System; using System.Web.UI; public partial class _Default : Page { protected void ProductControl_AddToCartClick(object sender, EventArgs e) { ProductControl product = (ProductControl)sender; }}
where (Productcontrol) is a class in another .ascx.cs file.
This method is present in the ShoppingCartControl class which is in ShoppingCartControl.ascx.cs file.
How to call this method in aspx.cs file ? plz suggest
Reply will be highly appreciable !!!!!
You can do it one of two ways:
0) Create a BasePage class that holds common methods that more than one page will need to access
1) Create a static class that holds methods that all code can access on the site. If these methods require access to page-specific variables, you''ll have to set those variables as session variables or pass them as parameters to the static methods.
ShoppingCartControl shoppingCartControl = new ShoppingCartControl(); shoppingCartControl.AddProductToCart();
Or use any exixting instance of the object if available and applicable. The method should be a public or internal method based on the needs.
这篇关于从aspx.cs文件中的ascx文件调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!