Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在ASP.NET C中实例化分部类:页面类型#_C#_Partial Classes - Fatal编程技术网

C# 在ASP.NET C中实例化分部类:页面类型#

C# 在ASP.NET C中实例化分部类:页面类型#,c#,partial-classes,C#,Partial Classes,下面是我试图实现的一个示例,它涉及实例化部分类,以便从单个.aspx页面使用多个类(及其方法) login.aspx: <%@ Page Language="C#" Codefile="Web_Code/LogonService.cs" Inherits="Client.LogonService" %> <!-- html here, also will be calling methods here via a form --> using System; using

下面是我试图实现的一个示例,它涉及实例化部分类,以便从单个.aspx页面使用多个类(及其方法)

login.aspx:

<%@ Page Language="C#" Codefile="Web_Code/LogonService.cs" Inherits="Client.LogonService" %>
<!-- html here, also will be calling methods here via a form -->
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Client {

    public partial class SessionHandler : Page {

        //Constructor method here

        public string setSessionUser(string username) {
            return "this works, this is just a test";
        }

    }   
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Client {

    public partial class LoginService : Page {

        public void checkCredentials(object sender, EventArgs e) {

            //Check credentials here

            //If credentials are good, add the username to session

            /* PROBLEM HERE: VS CAN'T FIND TYPE NOR INSTANTIATE*/
            SessionHandler ses = new SessionHandler();
            ses.setSessionUser(username.Value);  

        }
    }
}   

Web\u代码/LoginService.cs:

<%@ Page Language="C#" Codefile="Web_Code/LogonService.cs" Inherits="Client.LogonService" %>
<!-- html here, also will be calling methods here via a form -->
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Client {

    public partial class SessionHandler : Page {

        //Constructor method here

        public string setSessionUser(string username) {
            return "this works, this is just a test";
        }

    }   
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Client {

    public partial class LoginService : Page {

        public void checkCredentials(object sender, EventArgs e) {

            //Check credentials here

            //If credentials are good, add the username to session

            /* PROBLEM HERE: VS CAN'T FIND TYPE NOR INSTANTIATE*/
            SessionHandler ses = new SessionHandler();
            ses.setSessionUser(username.Value);  

        }
    }
}   
该问题在Web_Code/LogonService.cs中进行了注释-它无法实例化SessionHandler。-为什么呢

我正在从PHP切换到C#,在PHP的世界里,我会简单地写上“require”(“Web_Code/SessionHandler.PHP”)”,并就此结束,但C#方式似乎更为复杂


我感谢任何意见

您试图以PHP的方式使用.NET,最终这会给您带来问题。在ASP.NET中,您不会(也可能不应该)从
页面
类继承每个类。这通常只用于正在实现的特定网页的代码隐藏。在WebForms中,您还可以使用
控件
,如
标签
中继器
,而不是调用页面方法。此外,您已经可以访问页面中的
会话
对象和
用户
对象,通常不需要添加自己的会话处理


鉴于您刚刚起步,您可能希望了解ASP.NETMVC。这是一种更好的web编程模式。

您试图以PHP的方式使用.NET,最终这会给您带来问题。在ASP.NET中,您不会(也可能不应该)从
页面
类继承每个类。这通常只用于正在实现的特定网页的代码隐藏。在WebForms中,您还可以使用
控件
,如
标签
中继器
,而不是调用页面方法。此外,您已经可以访问页面中的
会话
对象和
用户
对象,通常不需要添加自己的会话处理


鉴于您刚刚起步,您可能希望了解ASP.NETMVC。对于网络编程来说,这是一个更好的范例。

我遗漏了一些东西。一个问题。你在这里问什么?你好,我编辑了OP,以引用问题在我代码中的位置。如果您查看Web_Code/LogonService.cs,我试图在其中实例化SessionHandler,它不会,我的问题是如何修复它。谢谢。我错过了一些东西。一个问题。你在这里问什么?你好,我编辑了OP,以引用问题在我代码中的位置。如果您查看Web_Code/LogonService.cs,我试图在其中实例化SessionHandler,它不会,我的问题是如何修复它。谢谢,谢谢你的回答。你会建议我只在每页使用一个分部类吗?因此,如果我有一个“user dashboard.aspx”页面,那么代码文件将是一个称为“UserActions”的部分类,它将处理仪表板页面上的所有内容,如果我在仪表板页面上有一个注销按钮,我可以将其作为“logoff.aspx”的链接,该链接将代码文件作为一个称为“SessionHandler”的部分类破坏会话?这是一种更好的思考方式吗?是和否,通常每个页面都有一个部分类支持。该类包含页面的“代码隐藏”——通常处理任何业务逻辑、数据访问等。ASPX文件中的任何代码通常仅用于呈现HTML。因此,您的
logoff.aspx
将有一个从
页面继承的部分
logoff
类。在
Page\u Load
方法中,然后使用
Session
属性和
FormsAuthentication
静态类实际注销并放弃会话。@Matthew2468-说真的,跳过WebForms,改用MVC。MVC真的是ASP.NET的未来,如果你刚开始使用MVC,你就不必忘记在WebForms中养成的任何坏习惯。感谢你提供的所有信息,我现在对如何构建我的项目有了更好的想法。另外,我将研究MVC。谢谢你的建议,谢谢你的回答。你会建议我只在每页使用一个分部类吗?因此,如果我有一个“user dashboard.aspx”页面,那么代码文件将是一个称为“UserActions”的部分类,它将处理仪表板页面上的所有内容,如果我在仪表板页面上有一个注销按钮,我可以将其作为“logoff.aspx”的链接,该链接将代码文件作为一个称为“SessionHandler”的部分类破坏会话?这是一种更好的思考方式吗?是和否,通常每个页面都有一个部分类支持。该类包含页面的“代码隐藏”——通常处理任何业务逻辑、数据访问等。ASPX文件中的任何代码通常仅用于呈现HTML。因此,您的
logoff.aspx
将有一个从
页面继承的部分
logoff
类。在
Page\u Load
方法中,然后使用
Session
属性和
FormsAuthentication
静态类实际注销并放弃会话。@Matthew2468-说真的,跳过WebForms,改用MVC。MVC真的是ASP.NET的未来,如果你刚开始使用MVC,你就不必忘记在WebForms中养成的任何坏习惯。感谢你提供的所有信息,我现在对如何构建我的项目有了更好的想法。另外,我将研究MVC。谢谢你的建议。