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");
Related
We are currently working with an older project (ASP.NET Web Forms Website) and trying to see if we can set up dependency injection for it.
Need to emphasize: this is NOT a Web Application project... it's the older type, the Website.
It is currently targeting .NET 4.7.2:
<httpRuntime targetFramework="4.7.2" />
So far, we've included the NuGet package:
<package id="Microsoft.AspNet.WebFormsDependencyInjection.Unity" version="1.0.0" targetFramework="net472" />
Defined some dummy interface and implementations:
public interface IDependencyTest
{
string GetName();
}
public class DependencyTest : IDependencyTest
{
public string GetName()
{
return "Mwuhahaha!!!";
}
}
And wired the DI container in the Application_Start event handler in global.asax:
void Application_Start(object sender, EventArgs e)
{
var container = this.AddUnity();
container.RegisterType<IDependencyTest, DependencyTest>();
}
Required namespaces were imported:
<%# Import Namespace="Microsoft.AspNet.WebFormsDependencyInjection.Unity" %>
<%# Import Namespace="Unity" %>
Created a test page Teste.aspx:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Teste.aspx.cs" Inherits="Teste" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblDisplay" runat="server" Text="No luck..."></asp:Label>
</div>
</form>
</body>
</html>
With the following code behind:
public partial class Teste : System.Web.UI.Page
{
private IDependencyTest _dependencyTest;
public Teste(IDependencyTest dependencyTest)
{
_dependencyTest = dependencyTest;
}
protected void Page_Load(object sender, EventArgs e)
{
lblDisplay.Text = _dependencyTest.GetName();
}
}
All this setup will fail with the following exception:
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\7a04dd72\81815e95\App_Web_teste.aspx.cdcab7d2.rtms4_ja.0.cs(187): error CS7036: There is no argument given that corresponds to the required formal parameter 'dependencyTest' of 'Teste.Teste(IDependencyTest)'
However, property injection does work:
using Unity.Attributes;
public partial class Teste : System.Web.UI.Page
{
[Dependency]
public IDependencyTest DependencyTest { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
lblDisplay.Text = DependencyTest.GetName();
}
}
To be honest, I'd really like to use the constructor injection...
Why isn't it working with our current setup?
Is it because it's a ASP.NET Website and NOT an ASP.NET WebApplication?
Can anything be done to get it working here, also?
Why isn't it working with our current setup?
The default configuration of Asp.Net WebForms the System.Web.UI.Page requires a parameter less constructor to instantiate your page and start the Page Life Cycle.
Thats the reason you get this error when try to use Constructor Dependency Injection.
error CS7036: There is no argument given that corresponds to the required formal parameter 'dependencyTest' of 'Teste.Teste(IDependencyTest)'
Can anything be done to get it working here, also?
Dependency injection (DI) with Asp.Net WebForms wasn't very commom, on the gold times of WebForms, so is very difficult to find something in the official documentation.
After some searches on StackOverflow and Google I found some useful information that can help you to solve your problem.
DI wans't very well supported in Asp.Net WebForms until version 4.7.2
So if you are using an older version than 4.7.2, and can't update to a newer version, you will need to use PageHandlerFactory. As I never tried this I prefer to give you the links to the two references I found.
Why does everyone say dependency injection in ASP.NET webforms is hard when PageHandlerFactory and IHttpHandlerFactory exist?
Dependency Injection in ASP.NET Web Forms
If you a using a version newer then 4.7.2, or if you can change to this version. You can use WebForms with a better support for DI.
Step 1 – Implement IServiceProvider. You can implement your own DI logic in it or plug in another DI framework, e.g. Unity, Ninject. The following example demonstrates injecting an ILog object through the constructor.
public class SimpleActivator : IServiceProvider
{
public object GetService(Type serviceType)
{
var ctors = serviceType.GetConstructors();
ConstructorInfo targetCtor = null;
foreach (var c in ctors)
{
var parameters = c.GetParameters();
if (parameters.Count() == 1 && parameters[0].ParameterType == typeof(ILog))
{
targetCtor = c;
break;
}
}
if(targetCtor != null)
{
return targetCtor.Invoke(new object[] { new DebuggingLoger() });
}
else
{
return Activator.CreateInstance(
serviceType,
BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.CreateInstance,
null,
null,
null);
}
}
}
Step 2 – Set WebObjectActivator in Global.asax.
public class Global : System.Web.HttpApplication
{
public override void Init()
{
HttpRuntime.WebObjectActivator = new SimpleActivator();
base.Init();
}
}
Step 3 – Use Dependency Injection in your Webform page.
public partial class WebForm2 : System.Web.UI.Page
{
private ILog _log;
public WebForm2(ILog l)
{
_log = l;
}
}
You can see this examples, and more inflortion about it in this link: https://devblogs.microsoft.com/dotnet/announcing-the-net-framework-4-7-2/
In my opinion if you can move to a newer version of .NetFramework you should try to implement the IServiceProvider. If you can't then you should analyse if the implementation of PageHandlerFactory will save you more time in the future than the time you will expend to implement it. If it will not save you any time in the future you can keep with [Dependency] property injection.
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
.....
}
}
I have register a page using the code
<%# Register TagPrefix="uc1" TagName="side" Src="side.ascx" %>
And called this by using the code
<uc1:side ID="Side1" runat="server"></uc1:side>
And i tried to acces the ID="Side1" in behind by using
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if (Session["UName"] != null || Session["UName"] != " ")
{
Side1.Visible = true;
}
else
Side1.Visible = true;
}
but gives the error
The name 'Side1' does not exist in the current context .
How will i resolve it by accessing the 'id' using 'protected System.Web.UI'.
Check the access modifier on your WebUserControl partial class definition (ie YourControl.ascx.cs file), it should be something like this:
public partial class SideControl : System.Web.UI.UserControl
{
// your code...
}
You can find more information about access modifiers here.
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();
I have the following class...
public class MyCustomLogger
{
public static int DEFAULT_INFO = 0;
public static int DEFAULT_LOGIN = 1;
//etc
public static void Log( ... )
{
doStuff(...);
}
public static void Log( ... , ... )
{
doStuff(...);
}
private static void doStuff(...)
{
//doLots of Stuff
}
So when I call MyCustomLogger.Log(...); from another Custom Class... it works fine. No compile errors...nothing. It just works
When I call it from SuchAndSuch.master code behind... it works fine. No Compile errors...nothing. It just works.
When I call it from SuchAndSuch.ascx code behind... it works fine. No Compile errors...nothing. It just works.
However... when I call it from SuchAndSuch.aspx code behind... it doesn't work. I'm getting 'MyCustomLogger' does not contain a definition for 'Log'
I'm getting this from any aspx page I add it to.
EDIT
Here's a snippet from the aspx pages i'm trying to add it to
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Logout : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//doStuff
}
protected void Button1_Click(object sender, EventArgs e)
{
MyCustomLogger.Log(MyCustomLogger.DEFAULT_INFO);
Has anyone else experienced this?
Here are a few ideas on what might be causing this:
If this is a web site project, which I'm guessing because your extract does not include a namespace, do you have another class in your app_code folder with the MyCustomLogger name?
If this page is in the Master Page, is the Master Page exposing something named MyCustomLogger as a public property and conflicting with the app_code definition?
If this is a web application project, the problem could be because the Namespace declaration is missing on the page.
Is there another type in the page that's MyCustomLogger that there is a conflict (can't resolve the correct type? Can you post any code to help us see the issue?