如何在 .ASPX 文件中声明和使用 C# 对象?

发布于 2024-07-27 00:22:05 字数 171 浏览 0 评论 0原文

我正在使用 asp.net 和 C# 制作一个网站,但是我陷入了第一个障碍,我发现要使用代码,您需要使用 <% %>; 使用 asp 但我不知道如何创建我的类的对象以在 aspx 文件中使用?

我认为它的语法比我似乎无法工作的任何东西都重要。

谢谢,

阿什

i am making a website using asp.net and C# and well i got stuck at the first hurdle, i found out that to use code you use the <% %> with asp but i dont get how i would create an object of my class to use in the aspx file?

I think its syntax more than anything i cant seem to get to work.

Thanks,

Ash

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

失退 2024-08-03 00:22:05

如果您需要声明一个全局对象以便在页面中的任何位置都可以访问:

<script runat="server">
   // ObjectType: Your class name
   // Name: Your instance (variable) name.
   ObjectType Name = new ObjectType();
</script>

如果您只需要一个局部变量:

<%
   ObjectType name = new ObjectType();
   name.SomeMethod();
%>

顺便说一句,您应该有充分的理由在 ASP.NET 中使用这些类型的东西。 通常有更好的方法将用户界面元素封装在用户控件和母版页中。

旁注:您不能在 .aspx 文件中使用 using 指令。 如果您需要在代码中导入某些命名空间,则应在 <%@ Page %>< 之后添加 <%@ Imports Namespace="SomeNamespace" %> 指令/code> 指令。

If you need to declare a global object to be accessible everywhere in your page:

<script runat="server">
   // ObjectType: Your class name
   // Name: Your instance (variable) name.
   ObjectType Name = new ObjectType();
</script>

If you just need a local variable:

<%
   ObjectType name = new ObjectType();
   name.SomeMethod();
%>

By the way, you should have good reasons for using these kind of things in ASP.NET. There are usually better ways to encapsulate user interface elements in user controls and master pages.

Side note: You can't use using directives in .aspx files. If you need to import some namespace in your code, you should add <%@ Imports Namespace="SomeNamespace" %> directives right after your <%@ Page %> directive.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文