I want to reuse the same method for several pages, for example for a logout button or a redirect.
Right now i'm writing the same method in every codebehind page.
How can i do this? In PHP i just include a file with common functions and i have access to it, but what is the approach in ASP.NET?
<asp:Button ID="logoutBtn" Text="Logout" runat="server"
CssClass="btn btn-default" OnClick="logoutBtn_Click" />
I want to access the method "logoutBtn_Click" from all pages in my project.
Create button click event handler method replica in app_code class file like
namespace DotNetMirror
{
public class CommonClass
{
public void CommonlogoutBtn_Click(object sender, EventArgs e)
{
//common logic code
}
}
}
Designer file:
<asp:Button ID="logoutBtn" Text="Logout" runat="server"
CssClass="btn btn-default" OnClick="logoutBtn_Click" />
CodeBehind:
protected void logoutBtn_Click(object sender, EventArgs e)
{
DotNetMirror.CommonClass obj = new DotNetMirror.CommonClass();
obj.CommonlogoutBtn_Click(sender, e);
}
You can have any number of button click events in pages but call the common method.
You can use app_code folder. Create a .cs file there.
Write you method there and call that method form you all of the pages.
public class Class1
{
public void abc()
{
}
}
and use it like this
Class1 c = new Class1();
c.abc();
Edit 1
Or you can use re-useable controls like user-controls or custom-controls and place it anywhere in you project.
Some basic examples
http://www.codeproject.com/Articles/1739/User-controls-in-ASP-NET
http://www.codeproject.com/Articles/87474/ASP-NET-Custom-Control
The way you include files in PHP, there are similar ways in ASP.NET(C#) to do that.
You can make a class and then you can use that class anywhere in the project, on any page you want by creating an instance of that class.
For example:
Public Class logout
{
public void logoutclick()
{
//your code goes here
}
}
and now in the other page just create an instance of the class and use the function inside that class
logout log = new logout();
log.logoutclick();
Related
I have an application in which when user login page load event check either there is a session for this user or not if there is no session user redirect to login.aspx file. But my web application has a lot of pages so instead of writing session code in every page load event I want to call it in page load event so I want to ask can I add this code in web.config file if yes then please help me how can I do this?
Thanks
if (Session["LoginUserName"] != null)
{
string str;
str = Session["LoginUserName"].ToString();
Label1.Text = str;
}
else
{
Server.Transfer("Login_Form.aspx");
}
make you all your Pages code inherit from a custom Class. Instead of the original class System.Web.UI.Page
Do it for all ASPX pages
public partial class MyPage : PageCommonBase
{
....
}
Make your custom class PageCommonBase and check for SessionUser OnPreinit Event
public class PageCommonBase : System.Web.UI.Page
{
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
if (
HttpContext.Current.Session.IsNewSession ||
HttpContext.Current.Session["LoginUserName"] == null
)
{
......
}
else
.....
}
}
How can I pass parameter beetwen pages? I've tried to add parameters to page uri but it didn't work because I can't use onNavigatedTo event on user control.
Please help
You must use OnFragmentNavigation.
public void OnFragmentNavigation(FragmentNavigationEventArgs e)
{
DoYourStuff(e.Fragment)
}
e.Fragement contains everything past the # in the URI. In example, using
NavigationCommands.GoToPage.Execute("/Pages/CustomerPage.xaml#CustomerID=12345", this);
e.Fragment will be "CustomerID=12345"
It looks like you are coming from a client browser showing web pages world. With WPF you own the app! you can simply set the value on the new page before or after navigating, pass it in with a constructor or access it from a location accessible from both pages. It sounds like the parameter is an argument to the page so I would pass it in with a constructor in this case:
public class APage : Page
{
private object myVar; // use whatever Type you want
public APage
{
InitializeComponent();
}
public APage(object arg) : this()
{
this.myVar = args;
}
}
we have a rather large Silverlight application and we need to add some extra functionality to it.
The App consists of an Frame-Element and a TreeView w/ HyperlinkButtons for the navigation. Every content which will be loaded into the main Frame is a Page.
Now, I need to hook into every Page's unload event. Currently we use something like this:
/* PageX.xaml */
<navigation:Page
x:Class="Foo.Views.PageX"
<!-- namespacing -->
Title="Test Page X"
Unloaded="Page_Unloaded">
...
</navigation:Page>
Code-behind:
/* PageX.xaml.cs */
/* usings */
namespace Foo.Views
{
public partial class PageX : Page
{
public PageX() {
InitializeComponent();
}
private void Page_Unloaded(object sender, RoutedEventArgs e) {
/* CODE */
}
}
}
This approach need to be implemented on each and every Page, as the code within the unloaded method stays exactly the same... As I mentioned earlier, we have a couple of Pages and it would be much more useful to create a custom Page-class where this Page_Unloaded() is implemented directly, so that we don't need to alter every Page.
Can you please tell me how to create such a custom Page-class?
Thanks in advance for any help!!
Kind regards!
You could create a base Page class that all your pages inherit from which registers the event in the constructor...
public class BasePage : Page
{
public BasePage()
{
Unloaded += Page_Unloaded;
}
void Page_Unloaded(object sender, RoutedEventArgs e)
{
}
}
Then all your pages could inherit from that...
public partial class Page1 : BasePage
{
public Page1()
{
InitializeComponent();
}
}
...and in the xaml of each page...
<base:BasePage x:Class="WPFApp.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:base="clr-namespace:WPFApp">
<Grid>
</Grid>
</base:BasePage>
I was trying to add some non-production test code by creating a 3rd partial file in addition to MyPage.aspx and MyPage.aspx.cs and MyPage.aspx.designer.cs. I called my third file MyPage.aspx.TEST.cs
In the partial file MyPage.aspx.TEST.cs, I wrote the following:
protected override void OnInit(EventArgs e)
{
Page.LoadComplete += RunTest;
base.OnInit(e);
}
//Assert.
public void RunTest(object sender, EventArgs e)
{
//Clever assertions
}
The code compiles and I then decompile the code and there it is, I can see the OnInit override and the RunTest method.
But when I execute the page, the event doesn't register, nor run, nor can I set a breakpoint.
I move that code out of the MyPage.aspx.TEST.cs partial class into the MyPage.aspx.cs partial file and the event registers and is executed. Stranger, when I decompile the assembly, and do a diff, the class appears to decompile to the same code.
Possible clues that may be unrelated:
The page uses autoeventwireup="true" (I still get the same behavior if I try to register my event my Page_LoadComplete)
The application is a web application (i.e.uses a proj file)
The partial file does compile (and if I introduce errors into the partial file, it will prevent compilation, so I know for sure that the partial file does get compiled)
I get the same result using different events (PreRender, etc)
This is strange, I just made the same experiment and all the events are being fired, I have the same conditions, web application, autoeventwireup = true
Are you inheriting from another base page?
This is my partial class:
public partial class _Default
{
protected override void OnInit(EventArgs e)
{
this.LoadComplete += RunTest;
this.Load += new EventHandler(_Default_Load);
base.OnInit(e);
}
void _Default_Load(object sender, EventArgs e)
{
//throw new NotImplementedException();
}
void RunTest(object sender, EventArgs e)
{
//throw new NotImplementedException();
}
protected override void OnPreRender(EventArgs e)
{
this.Response.Write("omfgggg");
this.lblMyMessageTest.Text = "omfg2";
base.OnPreRender(e);
}
}
All the events works, if I uncomment the //throw new NotImplementedException(); I get the exception as expected.
Try the following:
Ensure the name of your partial page classes are the same
Try to change Page.LoadComplete += RunTest; to this.LoadComplete += RunTest;
Ensure you are not terminating the response of the page when an exception occurs
If you have a custom HTTP module, try to disable it, it might be interfering with the events somehow
I have something like this in Site.Master in an asp.net mvc3 (not razor) project:
<telerik:RadRibbonBar ID="RadRibbonBar1" runat="server">
The code behind is defined as such:
<%# Master Language="C#" Inherits="myproject.Site_Master" CodeFile="Site.Master.cs" %>
So Site.Master.cs is:
using System.Web.Mvc;
using System;
namespace myproject
{
public partial class Site_Master : ViewMasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
}
How can I access radribbonbar1 from Page_loaded (which does get called once the page loads).
I was under the impression by giving it an ID it would be autogenerated to be accessible via C# code but that isn't happening (there is no variable radribbonbar1 available to Site_master).
Any ideas for how to make this work?
Server controls will not work properly because they require event handling, which you do not have in ASP.NET MVC. See the jQuery ribbon plugin
Didn't test this with your example but something like this might work
public static Control FindControlRecursive(Control root, string id)
{
return root.ID == id ? root : (from Control c in root.Controls select FindControlRecursive(c, id)).FirstOrDefault(t => t != null);
}
And, in your page_load
RadRibbonBar radRibbonBar1 = (RadRibbonBar)FindControlRecursive(Page, "RadRibbonBar1");