如何使用asp.net将一个Web表单的函数或方法调用到其他Web表单 [英] How to call a function or method of one web form to other web form using asp.net

查看:93
本文介绍了如何使用asp.net将一个Web表单的函数或方法调用到其他Web表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





是否可以继承该方法?如果是,我可以知道如何使用asp.net将一个Web表单的函数或方法调用到其他Web表单以避免代码长度。

解决方案

我会建议你将公共代码移到这些表单之外并从两个表单中使用它。



如果你需要从页面本身调用方法,你将不得不使用 XMLHTTPRequest 来调用带有一些特定查询字符串的页面,并将结果作为响应返回。这样做会涉及很多客户端代码。



从设计和可用性的角度来看,更好的选择是将公共代码移到两个表单之外。



PS永远不要使用继承代码resue。 继承只应在实体之间存在 is-a 关系时使用。 (继承的所有其他用途都是错误的设计,很快就会出现很多问题)


你唯一想要做的就是这个方法是静态的 - 如果它不是那么它需要另一个表单的实例来作为这个引用。由于你不太可能在网络环境中拥有该实例,静态是唯一真正的方法。



在这种情况下,为什么不从两种形式中删除方法,并将它添加到两个引用的DLL?优点是它将公共代码与任何特定形式相关联,因此如果您决定不再需要FormB,则可以在不破坏FormA和FormE的情况下将其删除。


< pre lang =F#>

你最好在App_code文件夹中创建一个类,而不是创建calss实例,而不是从两个web调用类的方法...为你做的





例子



把这个类放在App_code文件夹中



  public   class  Common 
{
public void MyFunction()
{

}
}





在你的webform中使用这个



 web 来自  1  
public class webform1
{
public void myfunctioncall()
{
new Common()。MyFunction
}
}

web 来自 2
公开 webform2
{
public void myfunctioncall()
{
new Common()。MyFunction
}
}


Hi,

Is it possible to inherit the method? If yes,may I know how to call a function or method of one web form to other web form using asp.net to avoid code length.

解决方案

I would recommend that you move the common code outside of these forms and use it from both the forms.

still if you need to call the method from page itself, you will have to use XMLHTTPRequest to call the page with some specific query string and get the result back as response. doing this would involve a lot of client side code.

The better option from design and usability standpoint would be to move the common code outside both the forms.

P.S. never use inheritance for code resue. inheritance should only be used when you have an is-a relationship between entities. (all other uses of inheritance is wrong by design ans sooner ot later surface a lot of problems)


The only time you would want to do that is if the method is static - if it wasn't then it would require an instance of the other form to work from as the this reference. Since you are unlikely to have that instance in a web situation, static is the only real way to go.

In that case, why not remove the method from both forms, and add it to a DLL which both reference? The advantage is that it separates common code from being tied to any particular form, so if you decide FormB is no longer needed, you can delete it without breaking FormA and FormE later on.


Its better you create one class in App_code folder and than create instance of calss and than than call method of the class from both web from...that will do for you


Example

put this class in App_code folder

public class Common
{
 public void MyFunction()
 {

  }
}



use this in you webform

web from 1 
public class webform1
{
  public void myfunctioncall()
  {
    new Common().MyFunction
  }
}

web from 2
public class webform2
{
  public void myfunctioncall()
  {
    new Common().MyFunction
  }
}


这篇关于如何使用asp.net将一个Web表单的函数或方法调用到其他Web表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆