views:

2662

answers:

4

I notice with the latest version of ASP.NET MVC that a View no longer defaults to having code-behind classes.

How do I go about adding a code-behind class now to a View or Partial View??

+7  A: 
Harry
Thanks for that. I've found a quicker way to associate the two files together is to right-click them, choose Exclude From Project, then with 'Show All Files' selected right-click them again and re-include them.
Andrew
+1  A: 

I'm not sure why you are creating a code behind file, but if you really really do, then I would consider using the standard webforms approach instead.

I would also look into the basics of MVC to understand why page behinds are not needed.

Another explanation

How to use ASP:Chart without a code-behind (Option B)

Dan Atkinson
It's quite simple really. I need to use code behind to populate the Chart series i'm using. It wouldn't let me use a normal MVC approach. This is a REAL world compromise, and it works.
Harry
In that case, here is how you can use Chart without a code-behind:http://code-inside.de/blog-in/2008/11/27/howto-use-the-new-aspnet-chart-controls-with-aspnet-mvc/
Dan Atkinson
So are you telling me you would prefer to have very messy hard to manage spaghetti code in your html? I think this is very valid use of code behind. It is not controller logic, it isn't presentation code. it is presentation preparation.
Harry
You don't *have* to put all of that code in the view. It's merely put there to show the user that they don't need to use code-behinds to achieve the same thing.
Dan Atkinson
+1  A: 

Ok, I have verified the solution, here is something that you need to note:

CodeBehind="View.ascx.cs" Inherits="Project.Views.Shared.View"

In your case, you need to change "Project.Views.Shared.View" based on your namespace and classname, and in order to access the control in the code-behind, you have to manually add declaration in code-behind. In my case, I need to initialize the gigaSoft proEssential control:

public class gigaTest2 : ViewUserControl
{
    protected global::Gigasoft.ProEssentials.PegoWeb PegoWeb1;
    protected void Page_Load(object sender, EventArgs e)
    {
        // Set Titles 
        PegoWeb1.PeString.MainTitle = "Hello ASP.NET";
        PegoWeb1.PeString.SubTitle = "";

        // One simple way of passing data, data binding also possible. //' 
        PegoWeb1.PeData.Subsets = 1;
        PegoWeb1.PeData.Points = 6;
        PegoWeb1.PeData.Y[0, 0] = 10;
        PegoWeb1.PeData.Y[0, 1] = 30;
        PegoWeb1.PeData.Y[0, 2] = 20;
        PegoWeb1.PeData.Y[0, 3] = 40;
        PegoWeb1.PeData.Y[0, 4] = 30;
        PegoWeb1.PeData.Y[0, 5] = 50;

        // Set style of chart and a few other properties //' 
        PegoWeb1.PePlot.Method = Gigasoft.ProEssentials.Enums.GraphPlottingMethod.Bar;
        PegoWeb1.PePlot.Option.GradientBars = 8;
        PegoWeb1.PeFont.FontSize = Gigasoft.ProEssentials.Enums.FontSize.Large;
    }
+1  A: 

you guys are so full of crap. One of the few good things in asp.net was code behind. Yeah sure you can mix it in with the html.... I say BS! use a code behind! Yes this should only contain code to RENDER the view but still its much much neater than mixing with HTML. Don't be a purest and Use what works!

jacko