C# 是否将web控件的后端添加到控件本身?
目前我有一个web控件和该控件的后端代码(以及设计器等) 要将其作为宏上传到Umbraco,我需要将其压缩为一个文件。我要怎么做才能不弄坏东西?当前,当我复制时,将代码与标记一起粘贴到所有其他内容中,如果有中断 网络表单C# 是否将web控件的后端添加到控件本身?,c#,asp.net,.net,web-user-controls,web-control,C#,Asp.net,.net,Web User Controls,Web Control,目前我有一个web控件和该控件的后端代码(以及设计器等) 要将其作为宏上传到Umbraco,我需要将其压缩为一个文件。我要怎么做才能不弄坏东西?当前,当我复制时,将代码与标记一起粘贴到所有其他内容中,如果有中断 网络表单 <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="UploadFileControl.ascx.cs" Inherits="FileUpload.UploadFileControl" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="UploadFileControl.ascx.cs" Inherits="FileUpload.UploadFileControl" %>
<form id="UploadForm" runat="server">
<asp:FileUpload id="FileUploadControl" runat="server" />
<asp:Button runat="server" id="UploadButton" text="Upload" onclick="UploadButton_Click" />
<br /><br />
<asp:Label runat="server" id="StatusLabel" text="Upload status: " />
</form>
和用户控制后端
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace FileUpload
{
//this backend for the web control will be used to upload a file that will have it's XML tags pulled and displayed on a page.
//this code checks if the fileupload control has input inside it, then proceeds to the next page with the document saved.
public partial class UploadFileControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
//names the script manager which will be used when the user attempts to upload a form / gives an error if they incorrectly attempt to upload
protected void UploadButton_Click(object sender, EventArgs e)
{
//if file is located
if (FileUploadControl.HasFile)
{
try
{
//allow content type of document / docx
if (FileUploadControl.PostedFile.ContentType == "document/docx")
{
//if the file is is less than 51mb
if (FileUploadControl.PostedFile.ContentLength < 2000)
{
//name the filename, find the path of the name
string filename = Path.GetFileName(FileUploadControl.FileName);
//path of server upload (we just need to save it as a variable to be found on the next page, as it will be made / deleted
FileUploadControl.SaveAs(Server.MapPath("~/") + filename);
//update the label with file uploaded
StatusLabel.Text = "Upload status: File uploaded!";
}
else
//display the size the file needs to be less than
StatusLabel.Text = "Upload status: The file has to be less than 2mb!";
}
else
//tell the user only docx files are accepted
StatusLabel.Text = "Upload status: Only DOCX files are accepted!";
}
catch (Exception ex)
{
//display the exception message, in which case it would be either size / type / if it's present
StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
}
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
命名空间文件上载
{
//这个web控件的后端将用于上载一个文件,该文件将在页面上提取并显示其XML标记。
//此代码检查fileupload控件中是否有输入,然后进入保存文档的下一页。
公共部分类UploadFileControl:System.Web.UI.UserControl
{
受保护的无效页面加载(对象发送方、事件参数e)
{
}
//命名当用户尝试上载表单时将使用的脚本管理器/如果他们错误地尝试上载,则给出错误
受保护的无效上载按钮\u单击(对象发送者,事件参数e)
{
//如果文件位于
if(FileUploadControl.HasFile)
{
尝试
{
//允许文档/docx的内容类型
if(FileUploadControl.PostedFile.ContentType==“document/docx”)
{
//如果文件大小小于51mb
if(FileUploadControl.PostedFile.ContentLength<2000)
{
//命名文件名,找到名称的路径
字符串filename=Path.GetFileName(FileUploadControl.filename);
//服务器上传路径(我们只需要将其保存为变量,以便在下一页中找到,因为它将被创建/删除
FileUploadControl.SaveAs(Server.MapPath(“~/”)+文件名);
//使用上载的文件更新标签
StatusLabel.Text=“上载状态:文件已上载!”;
}
其他的
//显示文件需要小于的大小
StatusLabel.Text=“上传状态:文件必须小于2mb!”;
}
其他的
//告诉用户仅接受docx文件
StatusLabel.Text=“上载状态:仅接受DOCX文件!”;
}
捕获(例外情况除外)
{
//显示异常消息,在这种情况下,它可能是size/type/如果存在
StatusLabel.Text=“上载状态:无法上载文件。出现以下错误:“+ex.Message;
}
}
}
}
}
一种可能是使用
标记将codebehind内容移动到页面本身。如下所示:
SamplePage.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SamplePage.aspx.cs" Inherits="Test.SamplePage" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
<%@ Import Namespace="System.Web"%>
<%@ Import Namespace="System.Web.UI"%>
<%@ Import Namespace="System.Web.UI.WebControls"%>
<%@ Page Language="C#" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
//Sample code.
}
</script>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
这将转化为:
SamplePage.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SamplePage.aspx.cs" Inherits="Test.SamplePage" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
<%@ Import Namespace="System.Web"%>
<%@ Import Namespace="System.Web.UI"%>
<%@ Import Namespace="System.Web.UI.WebControls"%>
<%@ Page Language="C#" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
//Sample code.
}
</script>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
受保护的无效页面加载(对象发送方、事件参数e)
{
//示例代码。
}
您可以尝试检查它是否也适用于
.ascx
内容。一种可能性是使用
标记将codebehind内容移动到页面本身。如下所示:
SamplePage.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SamplePage.aspx.cs" Inherits="Test.SamplePage" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
<%@ Import Namespace="System.Web"%>
<%@ Import Namespace="System.Web.UI"%>
<%@ Import Namespace="System.Web.UI.WebControls"%>
<%@ Page Language="C#" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
//Sample code.
}
</script>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
这将转化为:
SamplePage.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SamplePage.aspx.cs" Inherits="Test.SamplePage" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
<%@ Import Namespace="System.Web"%>
<%@ Import Namespace="System.Web.UI"%>
<%@ Import Namespace="System.Web.UI.WebControls"%>
<%@ Page Language="C#" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
//Sample code.
}
</script>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
受保护的无效页面加载(对象发送方、事件参数e)
{
//示例代码。
}
您可以尝试检查它是否也适用于
.ascx
内容。一种可能性是使用
标记将codebehind内容移动到页面本身。如下所示:
SamplePage.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SamplePage.aspx.cs" Inherits="Test.SamplePage" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
<%@ Import Namespace="System.Web"%>
<%@ Import Namespace="System.Web.UI"%>
<%@ Import Namespace="System.Web.UI.WebControls"%>
<%@ Page Language="C#" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
//Sample code.
}
</script>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
这将转化为:
SamplePage.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SamplePage.aspx.cs" Inherits="Test.SamplePage" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
<%@ Import Namespace="System.Web"%>
<%@ Import Namespace="System.Web.UI"%>
<%@ Import Namespace="System.Web.UI.WebControls"%>
<%@ Page Language="C#" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
//Sample code.
}
</script>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
受保护的无效页面加载(对象发送方、事件参数e)
{
//示例代码。
}
您可以尝试检查它是否也适用于
.ascx
内容。一种可能性是使用
标记将codebehind内容移动到页面本身。如下所示:
SamplePage.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SamplePage.aspx.cs" Inherits="Test.SamplePage" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
<%@ Import Namespace="System.Web"%>
<%@ Import Namespace="System.Web.UI"%>
<%@ Import Namespace="System.Web.UI.WebControls"%>
<%@ Page Language="C#" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
//Sample code.
}
</script>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
这将转化为:
SamplePage.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SamplePage.aspx.cs" Inherits="Test.SamplePage" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
<%@ Import Namespace="System.Web"%>
<%@ Import Namespace="System.Web.UI"%>
<%@ Import Namespace="System.Web.UI.WebControls"%>
<%@ Page Language="C#" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
//Sample code.
}
</script>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
受保护的无效页面加载(对象发送方、事件参数e)
{
//示例代码。
}
您可以尝试检查它是否与
.ascx
内容一起工作。确实有效,但唯一的问题是将使用名称空间添加到服务器。目前,我只是从目录中的两个文件相互调用,以便工作,但下次我必须进一步查看名称空间。啊,我明白了-我添加了pertinent关键字,Import,到示例中。希望它对您有效,@Alex。这确实有效,但唯一的问题是将using名称空间添加到服务器中。目前,我只是从目录中的两个文件相互调用,这样就可以了,但我下次必须进一步研究名称空间。啊,我明白了-我添加了相关的keyword、 导入,到示例中。希望它对您有效,@Alex。确实有效,但唯一的问题是将using名称空间添加到服务器中。目前,我只是从目录中的两个文件相互调用,这样就可以了,但下次我必须更深入地研究名称空间。啊,我明白了-我添加了相关的关键字Import,到示例。希望对您有效,@Alex。确实有效,但唯一的问题是添加