locked
Can I use more of one C# BehindCode? RRS feed

  • Question

  • User196874992 posted

    I'm programing in ASP .NET with c# behindcode and now i have a lot of code lines, I want to know if i can use more of one codebehind and interconnect for exaample:

    A.aspx.cs

     protected void ddlExample_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (ddlExample.SelectedValue == "OPTION A")
                {
                    functionExample();
                }
    }

    B.aspx.cs

    privte void functionExample(){
    //DO SOMETHING
    }

    Tuesday, May 4, 2021 4:37 PM

Answers

All replies

  • User1535942433 posted

     Hi EvansGxz,

    As far as I think,there's no way to call event in the another page unless you redirect to the other page and the page load.And then call the event in the page load.

    Just like this:

    A.aspx.cs:

    protected void ddlExample_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (ddlExample.SelectedValue == "OPTION A")
                {
                    Response.Redirect("B.aspx");
                }
            }

    B.aspx.cs:

    protected void Page_Load(object sender, EventArgs e)
    {
     functionExample();
    }
    protected void functionExample(){
    //DO SOMETHING
    }

    Best regards,

    Yijing Sun

    Wednesday, May 5, 2021 2:15 AM
  • User-821857111 posted

    Code behind files contain partial classes, so you can divide the code in a single file among multiple files if you wish. 

    public partial class Default : Page
    {
        ...
    }



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 5, 2021 5:40 AM
  • User753101303 posted

    Hi,

    When you want to use the same code from multiple places you usually create a separate class that you can then use where you want pretty much like any class provvided by .NET such as System.IO.File etc...

    See perhaps https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/classes 

    You alos have other feartures that could help dependng on the exact use case.

    Wednesday, May 5, 2021 8:10 AM
  • User196874992 posted

    Code behind files contain partial classes, so you can divide the code in a single file among multiple files if you wish. 

    public partial class Default : Page
    {
        ...
    }

    How can I do that? I using visual studio but when I do Right-Click on the page.aspx I can't add a new aspx.cs file

    Friday, May 7, 2021 12:57 PM
  • User475983607 posted

    Add a new class not an ASPX page.  I think you'll be interested in learning the "partial" keyword.

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/partial-classes-and-methods

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 7, 2021 2:07 PM