C# 没有代码的 ASP.net 页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/563585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 08:19:56  来源:igfitidea点击:

ASP.net page without a code behind

c#.netasp.netsharepointcode-behind

提问by Sophia

I have an ASP.Net page with a C# code behind.

我有一个带有 C# 代码的 ASP.Net 页面。

However, I've been asked to not use a code behind - so that it will be easier to deploy in SharePoint.

但是,我被要求不要使用隐藏代码 - 以便在 SharePoint 中部署会更容易。

Is there a way to include the C# code in the ASP.Net page, without using a separate code behind file?

有没有办法在 ASP.Net 页面中包含 C# 代码,而不使用单独的代码隐藏文件?

采纳答案by Rex M

By default Sharepoint does not allow server-side code to be executed in ASPX files. See thisfor how to resolve that.

默认情况下,Sharepoint 不允许在 ASPX 文件中执行服务器端代码。请参阅此了解如何解决该问题。

However, I would raise that having a code-behind is not necessarily difficult to deploy in Sharepoint (we do it extensively) - just compile your code-behind classes into an assembly and deploy it using a solution.

但是,我想指出在 Sharepoint 中部署代码隐藏不一定很难(我们广泛部署)——只需将代码隐藏类编译成程序集并使用解决方案部署它。

If still no, you can include all the code you'd normally place in a codebehind like so:

如果仍然没有,您可以将通常放置在代码隐藏中的所有代码包含在内,如下所示:

<script language="c#" runat="server">
public void Page_Load(object sender, EventArgs e)
{
  //hello, world!
}
</script>

回答by achinda99

You can actually have all the code in the aspx page. As explained here.

您实际上可以在 aspx 页面中拥有所有代码。正如这里所解释的。

Sample from here:

来自这里的示例:

<%@ Language=C# %>
<HTML>
   <script runat="server" language="C#">
   void MyButton_OnClick(Object sender, EventArgs e)
   {
      MyLabel.Text = MyTextbox.Text.ToString();
   }
   </script>
   <body>
      <form id="MyForm" runat="server">
         <asp:textbox id="MyTextbox" text="Hello World" runat="server"></asp:textbox>
         <asp:button id="MyButton" text="Echo Input" OnClick="MyButton_OnClick" runat="server"></asp:button>
         <asp:label id="MyLabel" runat="server"></asp:label>
      </form>
   </body>
</HTML>

回答by bendewey

yes on your aspx page include a script tag with runat=server

是在您的 aspx 页面上包含一个带有 runat=server 的脚本标记

<script language="c#" runat="server">

public void Page_Load(object sender, EventArgs e)
{
  // some load code
}
</script>

You can also use classic ASP Syntax

您还可以使用经典的 ASP 语法

<% if (this.MyTextBox.Visible) { %>
<span>Only show when myTextBox is visible</span>
<% } %>

回答by IEnumerator

I thought you could deploy just your .aspx page without the .aspx.cs so long as the DLL was in your bin. Part of the issue here is how visual studio .net works with .aspx pages.

我认为只要 DLL 在您的 bin 中,您就可以只部署 .aspx 页面而无需 .aspx.cs。这里的部分问题是visual studio .net 如何处理.aspx 页面。

Check it out here: Working with Single-File Web Forms Pages in Visual Studio .NET

在此处查看: 在 Visual Studio .NET 中使用单文件 Web 窗体页面

I know for sure that VS2008 with asp.net MVC RC you don't have code-behind files for your views.

我确信 VS2008 和 asp.net MVC RC 没有用于视图的代码隐藏文件。

回答by Tudor Olariu

There are two very different types of pages in SharePoint: Application Pages and Site Pages.

SharePoint 中有两种截然不同的页面类型:应用程序页面和网站页面。

If you are going to use your page as an Application Page, you can safely use inline code or code behind in your page, as Application pages live on the file system.

如果您打算将您的页面用作应用程序页面,您可以安全地在您的页面中使用内联代码或代码,因为应用程序页面存在于文件系统中。

If it's going to be a Site page, you can safely write inline code as long as you have it like that in the initial deployment. However if your site page is going to be customized at some point in the future, the inline code will no longer work because customized site pages live in the database and are executed in asp.net's "no compile" mode.

如果它将是一个站点页面,您可以安全地编写内联代码,只要您在初始部署中拥有它。但是,如果您的站点页面将在将来的某个时候进行自定义,则内联代码将不再起作用,因为自定义的站点页面存在于数据库中并在 asp.net 的“无编译”模式下执行。

Bottom line is - you can write aspx pages with inline code. The only problem is with customized Site pages... which will no longer care for your inline code.

底线是 - 您可以使用内联代码编写 aspx 页面。唯一的问题是定制的站点页面……它不再关心您的内联代码。

回答by access_granted

File: logdate.aspx

文件:logdate.aspx

<%@ Page Language="c#" %>
<%@ Import namespace="System.IO"%>
<%

StreamWriter tsw = File.AppendText(@Server.MapPath("./test.txt"));
tsw.WriteLine("--------------------------------");
tsw.WriteLine(DateTime.Now.ToString());

tsw.Close();
%>

Done