2012-06-28 60 views
1

我正在尝试合并一些代码,用于.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] 

我个人不建议有代码和标记在同一页上 - 我认为他们应该在单独的文件