how to add a code-behind page to a view or partial view

前端 未结 4 1407
面向向阳花
面向向阳花 2020-12-05 05:19

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

相关标签:
4条回答
  • 2020-12-05 05:56

    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)

    0 讨论(0)
  • 2020-12-05 06:02

    How to add a Code-behind page to a Partial View

    Seems this wasn't particularly tricky, and is quite do-able. This answer worked for a Partial ViewUserControl but the same should apply for a Normal MVC ViewPage as well

    1. Add a new Class file with the convention of <view filename & extention>.cs (i.e. view.ascx.cs)

    2. Add using System.Web.Mvc; to the class

    3. Change the class to Inherit from ViewUserControl<>.
      i.e. public class Foo:ViewUserControl

    4. Add the following to the View's header:

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

    5. Copy the files out of the solution and drag back in to re-associate the two together. This may not be necessary in VS 2010+ and MVC 2+.

    For this to work with a normal MVC View, you just need to inherit the class from "ViewPage"

    0 讨论(0)
  • 2020-12-05 06:02

    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;
        }
    
    0 讨论(0)
  • 2020-12-05 06:20

    To add a codebehind file to your aspx page, while still allowing it to be the target of an MVC view, do the following.

    For a view page named Index.aspx...

    Replace the following code....

    <%@ Page Inherits="System.Web.Mvc.ViewPage" %>
    

    with

    <%@ Page CodeFile="Index.aspx.vb" Inherits="Home_Index" %>
    

    Then create a file called Index.aspx.cs (or .vb).

    partial class Home_Index : System.Web.Mvc.ViewPage
    {...}
    

    or VB

    Partial Class Home_Index
        Inherits System.Web.Mvc.ViewPage
        ...
    End Class
    

    That's it. The only thing special is using the correct Mvc.ViewPage base class.

    0 讨论(0)
提交回复
热议问题