asp.net(c#),请问如何定义一个公共的函数然后在各个页面调用?这样省去了一些工作量
3个回答
展开全部
写一个类,把要重复应用的函数封装好就ok了,你可以把它编译成dll文件
例如:
using System;
using System.Text;
using System.Reflection;
/// <summary>
/// Summary description for Class1
/// </summary>
///
namespace cc
{
public class CUseData
{
private string _ConnectString;
public CUseData()
{
}
public string ConnectString
{
get
{
return _ConnectString;
}
set
{
_ConnectString = value;
}
}
public int ConnectStringLength()
{
return _ConnectString.Length;
}
}
}
在应用的文件中:
<%@ Import Namespace="cc" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
CUseData cu = new CUseData();
cu.ConnectString = "a";
int i = cu.ConnectStringLength();
Label1.Text = i.ToString();
}
</script>
例如:
using System;
using System.Text;
using System.Reflection;
/// <summary>
/// Summary description for Class1
/// </summary>
///
namespace cc
{
public class CUseData
{
private string _ConnectString;
public CUseData()
{
}
public string ConnectString
{
get
{
return _ConnectString;
}
set
{
_ConnectString = value;
}
}
public int ConnectStringLength()
{
return _ConnectString.Length;
}
}
}
在应用的文件中:
<%@ Import Namespace="cc" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
CUseData cu = new CUseData();
cu.ConnectString = "a";
int i = cu.ConnectStringLength();
Label1.Text = i.ToString();
}
</script>
展开全部
做一个公共的类:
public YourNameSpace{
class Function{
void static YourFunction1(...);
bool static YourFunction2(...);
}
}
调用的时候:
using YourNameSpace;
Function.YourFunction1(...);
bool bRet = Function.YourFunction2(...);
public YourNameSpace{
class Function{
void static YourFunction1(...);
bool static YourFunction2(...);
}
}
调用的时候:
using YourNameSpace;
Function.YourFunction1(...);
bool bRet = Function.YourFunction2(...);
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
通过 interface, class 或 struct 来作
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询