我正在尝试合并我在原始代码中找到的.net代码背后的代码。 Ultimatley,我正在尝试创建一个允许文件附件的表单。我所做的一切都会产生错误,我找不到能够识别问题的网站。这是表单的代码......
<form id="form1" 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>
这是背后的代码。
protected void UploadButton_Click(object sender, EventArgs e)
{
if(FileUploadControl.HasFile)
{
try
{
string filename = Path.GetFileName(FileUploadControl.FileName);
FileUploadControl.SaveAs(Server.MapPath("~/") + filename);
StatusLabel.Text = "Upload status: File uploaded!";
}
catch(Exception ex)
{
StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
}
}
}
如何将这些合并到一个页面上?
答案 0 :(得分:1)
在文件的开头,添加此...
<%@ Page Language="C#" %>
<script runat="server">
[code goes here]
</script>
<html>
[page continues here]
我个人不建议在同一页面上使用代码和标记 - 我认为它们应该在单独的文件中