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 se开发者_StackOverflownder, 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#!
Just create a base page which all other pages inherit from. Put your code in the base page.
so public class CurrentPage : BasePage
(inherits)
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.
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
<system.web>
<!-- ... -->
<pages pageBaseType="MyNamespace.MyPage" />
<!-- ... -->
</system.web>
精彩评论