[英]How to reuse this C# code?
I can´t find a way to reuse the code below in all my webpages.我找不到在所有网页中重用以下代码的方法。
How can I do it?我该怎么做?
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Page.Request.UrlReferrer == null)
{
Response.Redirect("test.aspx");
}
}
}
I´d like to use something like this:我想使用这样的东西:
protected void Page_Load(object sender, EventArgs e)
{
CheckURL();
}
Please, give me a example: :) I´m new using the C#!请给我一个例子:)我是使用C#的新手!
Just create a base page which all other pages inherit from.只需创建一个所有其他页面都继承自的基本页面。 Put your code in the base page.
将您的代码放在基本页面中。
so public class CurrentPage: BasePage
(inherits)所以
public class CurrentPage: BasePage
(继承)
then然后
public abstract class BasePage: System.Web.Ui.Page
You could create a new class that inherits from System.Web.UI.Page
and add this piece of logic there and then use the new class for all your pages.您可以创建一个新的 class 继承自
System.Web.UI.Page
并在其中添加这段逻辑,然后对所有页面使用新的 class。
EDIT:编辑:
something like this像这样的东西
public class MyPage : System.Web.UI.Page
{
public MyPage()
{
Load += MyPage_Load;
}
void MyPage_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Page.Request.UrlReferrer == null)
{
Response.Redirect("test.aspx");
}
}
}
}
and add this in web.config并将其添加到 web.config
<system.web>
<!-- ... -->
<pages pageBaseType="MyNamespace.MyPage" />
<!-- ... -->
</system.web>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.